49 lines
1.7 KiB
Go
49 lines
1.7 KiB
Go
package models
|
|
|
|
import (
|
|
"kontor-api-gin/internal/helpers"
|
|
"time"
|
|
)
|
|
|
|
type BookAuthor 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"`
|
|
BookID *string `gorm:"column:book_id" json:"book_id"`
|
|
Book *Book `gorm:"foreignKey:BookID" json:"book"`
|
|
AuthorID *string `form:"column:author_id" json:"author_id"`
|
|
Author *Author `gorm:"foreignKey:AuthorID" json:"author"`
|
|
}
|
|
|
|
func (book_author *BookAuthor) TableName() string {
|
|
return "book_author"
|
|
}
|
|
|
|
type BookAuthorDTO 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"`
|
|
Version int `json:"version"`
|
|
BookID *string `json:"book_id"`
|
|
AuthorID *string `json:"author_id"`
|
|
}
|
|
|
|
func ConvertBookAuthorsToDtos(book_authors []BookAuthor) []BookAuthorDTO {
|
|
dtos := make([]BookAuthorDTO, len(book_authors))
|
|
for i, book_author := range book_authors {
|
|
dtos[i] = BookAuthorDTO{
|
|
ID: book_author.ID,
|
|
CreatedAt: helpers.KontorTime{Time: book_author.CreatedAt},
|
|
UpdatedAt: helpers.KontorTime{Time: book_author.UpdatedAt},
|
|
DeletedAt: book_author.DeletedAt,
|
|
Version: book_author.Version,
|
|
BookID: book_author.BookID,
|
|
AuthorID: book_author.AuthorID,
|
|
}
|
|
}
|
|
return dtos
|
|
}
|