feat: update article save

This commit is contained in:
hanif salafi 2025-02-13 22:32:59 +07:00
parent 461e79a481
commit dcf7ec8971
7 changed files with 41 additions and 15 deletions

View File

@ -1,13 +1,16 @@
package controller package controller
import "go-humas-be/app/module/articles/service" import (
"github.com/rs/zerolog"
"go-humas-be/app/module/articles/service"
)
type Controller struct { type Controller struct {
Articles ArticlesController Articles ArticlesController
} }
func NewController(ArticlesService service.ArticlesService) *Controller { func NewController(ArticlesService service.ArticlesService, log zerolog.Logger) *Controller {
return &Controller{ return &Controller{
Articles: NewArticlesController(ArticlesService), Articles: NewArticlesController(ArticlesService, log),
} }
} }

View File

@ -77,6 +77,10 @@ func (_i *articlesRepository) GetAll(req request.ArticlesQueryRequest) (articles
direction = "DESC" direction = "DESC"
} }
query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction)) query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction))
} else {
direction := "DESC"
sortBy := "created_at"
query.Order(fmt.Sprintf("%s %s", sortBy, direction))
} }
req.Pagination.Count = count req.Pagination.Count = count

View File

@ -25,17 +25,18 @@ type ArticlesQueryRequest struct {
} }
type ArticlesCreateRequest struct { type ArticlesCreateRequest struct {
Title string `json:"title" validate:"required"` Title string `json:"title" validate:"required"`
Slug string `json:"slug" validate:"required"` Slug string `json:"slug" validate:"required"`
Description string `json:"description" validate:"required"` Description string `json:"description" validate:"required"`
HtmlDescription string `json:"htmlDescription" validate:"required"` HtmlDescription string `json:"htmlDescription" validate:"required"`
CategoryIds string `json:"categoryIds" validate:"required"` CategoryIds string `json:"categoryIds" validate:"required"`
TypeId int `json:"typeId" validate:"required"` TypeId int `json:"typeId" validate:"required"`
Tags string `json:"tags" validate:"required"` Tags string `json:"tags" validate:"required"`
AiArticleId *int `json:"aiArticleId"` AiArticleId *int `json:"aiArticleId"`
IsPublish *bool `json:"isPublish"` CreatedAt *string `json:"createdAt"`
IsDraft *bool `json:"isDraft"` IsPublish *bool `json:"isPublish"`
OldId *uint `json:"oldId"` IsDraft *bool `json:"isDraft"`
OldId *uint `json:"oldId"`
} }
func (req ArticlesCreateRequest) ToEntity() *entity.Articles { func (req ArticlesCreateRequest) ToEntity() *entity.Articles {

View File

@ -3,6 +3,7 @@ package service
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -136,6 +137,15 @@ func (_i *articlesService) Save(req request.ArticlesCreateRequest, authToken str
newReq.DraftedAt = nil newReq.DraftedAt = nil
} }
if req.CreatedAt != nil {
layout := "2006-01-02 15:04:05"
parsedTime, err := time.Parse(layout, *req.CreatedAt)
if err != nil {
return nil, fmt.Errorf("Error parsing time:", err)
}
newReq.CreatedAt = parsedTime
}
saveArticleRes, err := _i.Repo.Create(newReq) saveArticleRes, err := _i.Repo.Create(newReq)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -7564,6 +7564,9 @@ const docTemplate = `{
"categoryIds": { "categoryIds": {
"type": "string" "type": "string"
}, },
"createdAt": {
"type": "string"
},
"description": { "description": {
"type": "string" "type": "string"
}, },

View File

@ -7553,6 +7553,9 @@
"categoryIds": { "categoryIds": {
"type": "string" "type": "string"
}, },
"createdAt": {
"type": "string"
},
"description": { "description": {
"type": "string" "type": "string"
}, },

View File

@ -225,6 +225,8 @@ definitions:
type: integer type: integer
categoryIds: categoryIds:
type: string type: string
createdAt:
type: string
description: description:
type: string type: string
htmlDescription: htmlDescription: