This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"kontor-api-gin/internal/helpers"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Book struct {
|
||||
ID string `gorm:"type:string;primary_key;" json:"id"`
|
||||
CreatedAt time.Time `gorm:"column:created_date" json:"created_date"`
|
||||
UpdatedAt time.Time `gorm:"column:last_modified_date" json:"last_modified_date"`
|
||||
DeletedAt *time.Time `gorm:"column:deleted_date" json:"deleted_date"`
|
||||
Version int `gorm:"default:0" json:"version"`
|
||||
ISBN string `gorm:"column:isbn;uniqueIndex" json:"isbn"`
|
||||
Title string `gorm:"column:title" json:"title"`
|
||||
Year int `gorm:"column:year" json:"year"`
|
||||
PublisherID string `gorm:"column:publisher_id" json:"publisher_id"`
|
||||
Publisher BookshelfPublisher `gorm:"foreignKey:PublisherID" json:"publisher"`
|
||||
BookAuthors []BookAuthor `gorm:"foreignKey:BookID" json:"book_authors"`
|
||||
}
|
||||
|
||||
func (book *Book) TableName() string {
|
||||
return "book"
|
||||
}
|
||||
|
||||
type BookDTO struct {
|
||||
ID string `json:"id" binding:"required"`
|
||||
CreatedAt helpers.KontorTime `json:"created_date"`
|
||||
UpdatedAt helpers.KontorTime `json:"last_modified_date"`
|
||||
DeletedAt *time.Time `json:"deleted_date,omitempty"`
|
||||
Version int `json:"version"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
func ConvertBooksToDtos(books []Book) []BookDTO {
|
||||
dtos := make([]BookDTO, len(books))
|
||||
for i, book := range books {
|
||||
dtos[i] = BookDTO{
|
||||
ID: book.ID,
|
||||
CreatedAt: helpers.KontorTime{Time: book.CreatedAt},
|
||||
UpdatedAt: helpers.KontorTime{Time: book.UpdatedAt},
|
||||
DeletedAt: book.DeletedAt,
|
||||
Version: book.Version,
|
||||
Title: book.Title,
|
||||
}
|
||||
}
|
||||
return dtos
|
||||
}
|
||||
Reference in New Issue
Block a user