Files
kontor/kontor-gin/internal/models/article.go
T
Thomas Peetz 8706b3880f
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
Gitea Actions Demo / build-and-test (push) Failing after 13m11s
add methods in Repository an dModel for importing data
2026-10-30 00:19:25 +01:00

67 lines
2.0 KiB
Go

package models
import (
"kontor-api-gin/internal/helpers"
"time"
)
type Article 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"`
Title string `gorm:"uniqueIndex" json:"title"`
ArticleAuthors []ArticleAuthor `gorm:"foreignKey:ArticleID" json:"article_authors"`
}
func (article *Article) TableName() string {
return "article"
}
type ArticleDTO 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 (dto ArticleDTO) ConvertDTOToModel() Article {
model := Article{
ID: dto.ID,
CreatedAt: dto.CreatedAt.Time,
UpdatedAt: dto.UpdatedAt.Time,
DeletedAt: dto.DeletedAt,
Version: dto.Version,
Title: dto.Title,
}
return model
}
func ConvertArticlesToDtos(articles []Article) []ArticleDTO {
dtos := make([]ArticleDTO, len(articles))
for i, article := range articles {
dtos[i] = ArticleDTO{
ID: article.ID,
CreatedAt: helpers.KontorTime{Time: article.CreatedAt},
UpdatedAt: helpers.KontorTime{Time: article.UpdatedAt},
DeletedAt: article.DeletedAt,
Version: article.Version,
Title: article.Title,
}
}
return dtos
}
type ArticleDTOList []ArticleDTO
type CreateArticleDTO struct {
Title string `json:"title" binding:"required"`
}
type UpdateArticleDTO struct {
Title *string `json:"title,omitempty" binding:"omitempty"`
}