125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
package request
|
|
|
|
import (
|
|
"go-humas-be/app/database/entity"
|
|
"go-humas-be/utils/paginator"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type ArticlesGeneric interface {
|
|
ToEntity()
|
|
}
|
|
|
|
type ArticlesQueryRequest struct {
|
|
Title *string `json:"title"`
|
|
Description *string `json:"description"`
|
|
TypeId *int `json:"typeId"`
|
|
Tags *string `json:"tags"`
|
|
StatusId *int `json:"statusId"`
|
|
IsPublish *bool `json:"isPublish"`
|
|
Pagination *paginator.Pagination `json:"pagination"`
|
|
}
|
|
|
|
type ArticlesCreateRequest struct {
|
|
Title string `json:"title" validate:"required"`
|
|
Slug string `json:"slug" validate:"required"`
|
|
Description string `json:"description" validate:"required"`
|
|
HtmlDescription string `json:"htmlDescription" validate:"required"`
|
|
TypeId int `json:"typeId" validate:"required"`
|
|
Tags string `json:"tags" validate:"required"`
|
|
ThumbnailPath *string `json:"thumbnailPath"`
|
|
ThumbnailUrl *string `json:"thumbnailUrl"`
|
|
PageUrl *string `json:"pageUrl"`
|
|
CreatedById *int `json:"createdById"`
|
|
StatusId *int `json:"status_id"`
|
|
IsPublish *bool `json:"is_publish"`
|
|
PublishedAt *time.Time `json:"published_at"`
|
|
IsActive *bool `json:"is_active"`
|
|
}
|
|
|
|
func (req ArticlesCreateRequest) ToEntity() *entity.Articles {
|
|
return &entity.Articles{
|
|
Title: req.Title,
|
|
Slug: req.Slug,
|
|
Description: req.Description,
|
|
HtmlDescription: req.HtmlDescription,
|
|
TypeId: req.TypeId,
|
|
Tags: req.Tags,
|
|
ThumbnailPath: req.ThumbnailPath,
|
|
ThumbnailUrl: req.ThumbnailUrl,
|
|
PageUrl: req.PageUrl,
|
|
CreatedById: req.CreatedById,
|
|
StatusId: req.StatusId,
|
|
IsPublish: req.IsPublish,
|
|
PublishedAt: req.PublishedAt,
|
|
IsActive: req.IsActive,
|
|
}
|
|
}
|
|
|
|
type ArticlesUpdateRequest struct {
|
|
Title string `json:"title" validate:"required"`
|
|
Slug string `json:"slug" validate:"required"`
|
|
Description string `json:"description" validate:"required"`
|
|
HtmlDescription string `json:"htmlDescription" validate:"required"`
|
|
TypeId int `json:"typeId" validate:"required"`
|
|
Tags string `json:"tags" validate:"required"`
|
|
StatusId *int `json:"statusId"`
|
|
}
|
|
|
|
func (req ArticlesUpdateRequest) ToEntity() *entity.Articles {
|
|
return &entity.Articles{
|
|
Title: req.Title,
|
|
Slug: req.Slug,
|
|
Description: req.Description,
|
|
HtmlDescription: req.HtmlDescription,
|
|
TypeId: req.TypeId,
|
|
Tags: req.Tags,
|
|
StatusId: req.StatusId,
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
type ArticlesQueryRequestContext struct {
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
TypeId string `json:"typeId"`
|
|
Tags string `json:"tags"`
|
|
IsPublish string `json:"isPublish"`
|
|
StatusId string `json:"statusId"`
|
|
}
|
|
|
|
func (req ArticlesQueryRequestContext) ToParamRequest() ArticlesQueryRequest {
|
|
var request ArticlesQueryRequest
|
|
|
|
if title := req.Title; title != "" {
|
|
request.Title = &title
|
|
}
|
|
if description := req.Description; description != "" {
|
|
request.Description = &description
|
|
}
|
|
if typeIdStr := req.TypeId; typeIdStr != "" {
|
|
typeId, err := strconv.Atoi(typeIdStr)
|
|
if err == nil {
|
|
request.TypeId = &typeId
|
|
}
|
|
}
|
|
if tags := req.Tags; tags != "" {
|
|
request.Tags = &tags
|
|
}
|
|
if isPublishStr := req.IsPublish; isPublishStr != "" {
|
|
isPublish, err := strconv.ParseBool(isPublishStr)
|
|
if err == nil {
|
|
request.IsPublish = &isPublish
|
|
}
|
|
}
|
|
if statusIdStr := req.StatusId; statusIdStr != "" {
|
|
statusId, err := strconv.Atoi(statusIdStr)
|
|
if err == nil {
|
|
request.StatusId = &statusId
|
|
}
|
|
}
|
|
|
|
return request
|
|
}
|