211 lines
6.6 KiB
Go
211 lines
6.6 KiB
Go
package request
|
|
|
|
import (
|
|
"errors"
|
|
"netidhub-saas-be/app/database/entity"
|
|
"netidhub-saas-be/utils/paginator"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type ArticlesGeneric interface {
|
|
ToEntity()
|
|
}
|
|
|
|
type ArticlesQueryRequest struct {
|
|
Title *string `json:"title"`
|
|
Description *string `json:"description"`
|
|
CategoryId *uint `json:"categoryId"`
|
|
Category *string `json:"category"`
|
|
TypeId *int `json:"typeId"`
|
|
Tags *string `json:"tags"`
|
|
CreatedById *int `json:"createdById"`
|
|
StatusId *int `json:"statusId"`
|
|
IsBanner *bool `json:"isBanner"`
|
|
IsPublish *bool `json:"isPublish"`
|
|
IsDraft *bool `json:"isDraft"`
|
|
ClientSlug *string `json:"clientSlug"`
|
|
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"`
|
|
CategoryIds string `json:"categoryIds" validate:"required"`
|
|
TypeId int `json:"typeId" validate:"required"`
|
|
Tags string `json:"tags" validate:"required"`
|
|
AiArticleId *int `json:"aiArticleId"`
|
|
CreatedAt *string `json:"createdAt"`
|
|
CreatedById *uint `json:"createdById"`
|
|
IsPublish *bool `json:"isPublish"`
|
|
IsDraft *bool `json:"isDraft"`
|
|
OldId *uint `json:"oldId"`
|
|
PublishedFor *string `json:"publishedFor"`
|
|
}
|
|
|
|
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,
|
|
AiArticleId: req.AiArticleId,
|
|
IsPublish: req.IsPublish,
|
|
IsDraft: req.IsDraft,
|
|
OldId: req.OldId,
|
|
PublishedFor: req.PublishedFor,
|
|
}
|
|
}
|
|
|
|
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"`
|
|
CategoryIds string `json:"categoryIds" validate:"required"`
|
|
TypeId int `json:"typeId" validate:"required"`
|
|
Tags string `json:"tags" validate:"required"`
|
|
CreatedAt *string `json:"createdAt"`
|
|
CreatedById *uint `json:"createdById"`
|
|
AiArticleId *int `json:"aiArticleId"`
|
|
IsPublish *bool `json:"isPublish"`
|
|
IsDraft *bool `json:"isDraft"`
|
|
StatusId *int `json:"statusId"`
|
|
PublishedFor *string `json:"publishedFor"`
|
|
}
|
|
|
|
func (req ArticlesUpdateRequest) ToEntity() *entity.Articles {
|
|
if req.CreatedById == nil {
|
|
return &entity.Articles{
|
|
Title: req.Title,
|
|
Slug: req.Slug,
|
|
Description: req.Description,
|
|
HtmlDescription: req.HtmlDescription,
|
|
TypeId: req.TypeId,
|
|
Tags: req.Tags,
|
|
StatusId: req.StatusId,
|
|
AiArticleId: req.AiArticleId,
|
|
IsPublish: req.IsPublish,
|
|
IsDraft: req.IsDraft,
|
|
UpdatedAt: time.Now(),
|
|
PublishedFor: req.PublishedFor,
|
|
}
|
|
} else {
|
|
return &entity.Articles{
|
|
Title: req.Title,
|
|
Slug: req.Slug,
|
|
Description: req.Description,
|
|
HtmlDescription: req.HtmlDescription,
|
|
TypeId: req.TypeId,
|
|
Tags: req.Tags,
|
|
StatusId: req.StatusId,
|
|
CreatedById: req.CreatedById,
|
|
AiArticleId: req.AiArticleId,
|
|
IsPublish: req.IsPublish,
|
|
IsDraft: req.IsDraft,
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
}
|
|
}
|
|
|
|
type ArticlesQueryRequestContext struct {
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
CategoryId string `json:"categoryId"`
|
|
Category string `json:"category"`
|
|
TypeId string `json:"typeId"`
|
|
Tags string `json:"tags"`
|
|
CreatedById string `json:"createdById"`
|
|
IsBanner string `json:"isBanner"`
|
|
IsPublish string `json:"isPublish"`
|
|
IsDraft string `json:"isDraft"`
|
|
StatusId string `json:"statusId"`
|
|
ClientSlug string `json:"clientSlug"`
|
|
}
|
|
|
|
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 category := req.Category; category != "" {
|
|
request.Category = &category
|
|
}
|
|
if categoryIdStr := req.CategoryId; categoryIdStr != "" {
|
|
categoryId, err := strconv.Atoi(categoryIdStr)
|
|
if err == nil {
|
|
categoryIdUint := uint(categoryId)
|
|
request.CategoryId = &categoryIdUint
|
|
}
|
|
}
|
|
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 isBannerStr := req.IsBanner; isBannerStr != "" {
|
|
isBanner, err := strconv.ParseBool(isBannerStr)
|
|
if err == nil {
|
|
request.IsBanner = &isBanner
|
|
}
|
|
}
|
|
if isDraftStr := req.IsDraft; isDraftStr != "" {
|
|
isDraft, err := strconv.ParseBool(isDraftStr)
|
|
if err == nil {
|
|
request.IsDraft = &isDraft
|
|
}
|
|
}
|
|
if statusIdStr := req.StatusId; statusIdStr != "" {
|
|
statusId, err := strconv.Atoi(statusIdStr)
|
|
if err == nil {
|
|
request.StatusId = &statusId
|
|
}
|
|
}
|
|
if createdByIdStr := req.CreatedById; createdByIdStr != "" {
|
|
createdById, err := strconv.Atoi(createdByIdStr)
|
|
if err == nil {
|
|
request.CreatedById = &createdById
|
|
}
|
|
}
|
|
if clientSlug := req.ClientSlug; clientSlug != "" {
|
|
request.ClientSlug = &clientSlug
|
|
}
|
|
|
|
return request
|
|
}
|
|
|
|
// SubmitForApprovalRequest represents the request for submitting an article for approval
|
|
type SubmitForApprovalRequest struct {
|
|
WorkflowId *uint `json:"workflow_id" validate:"omitempty,min=1"`
|
|
Message *string `json:"message" validate:"omitempty,max=500"`
|
|
}
|
|
|
|
// Validate validates the SubmitForApprovalRequest
|
|
func (req *SubmitForApprovalRequest) Validate() error {
|
|
if req.WorkflowId != nil && *req.WorkflowId == 0 {
|
|
return errors.New("workflow_id must be greater than 0")
|
|
}
|
|
if req.Message != nil && len(*req.Message) > 500 {
|
|
return errors.New("message must be less than 500 characters")
|
|
}
|
|
return nil
|
|
}
|