From 84a678e499ef5e66df8d2128f730e374a7a3639e Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Thu, 13 Feb 2025 22:32:59 +0700 Subject: [PATCH] feat: update article save --- app/module/articles/controller/controller.go | 11 +++++---- .../repository/articles.repository.go | 4 ++++ .../articles/request/articles.request.go | 23 ++++++++++--------- .../articles/service/articles.service.go | 10 ++++++++ docs/swagger/docs.go | 3 +++ docs/swagger/swagger.json | 3 +++ docs/swagger/swagger.yaml | 2 ++ 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/app/module/articles/controller/controller.go b/app/module/articles/controller/controller.go index 129662b..c64cbe5 100644 --- a/app/module/articles/controller/controller.go +++ b/app/module/articles/controller/controller.go @@ -1,13 +1,16 @@ 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 { Articles ArticlesController } -func NewController(ArticlesService service.ArticlesService) *Controller { +func NewController(ArticlesService service.ArticlesService, log zerolog.Logger) *Controller { return &Controller{ - Articles: NewArticlesController(ArticlesService), + Articles: NewArticlesController(ArticlesService, log), } -} \ No newline at end of file +} diff --git a/app/module/articles/repository/articles.repository.go b/app/module/articles/repository/articles.repository.go index d3cda6c..f193d1e 100644 --- a/app/module/articles/repository/articles.repository.go +++ b/app/module/articles/repository/articles.repository.go @@ -77,6 +77,10 @@ func (_i *articlesRepository) GetAll(req request.ArticlesQueryRequest) (articles direction = "DESC" } 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 diff --git a/app/module/articles/request/articles.request.go b/app/module/articles/request/articles.request.go index 95f34f4..ae506a7 100644 --- a/app/module/articles/request/articles.request.go +++ b/app/module/articles/request/articles.request.go @@ -25,17 +25,18 @@ type ArticlesQueryRequest struct { } 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"` - IsPublish *bool `json:"isPublish"` - IsDraft *bool `json:"isDraft"` - OldId *uint `json:"oldId"` + 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"` + IsPublish *bool `json:"isPublish"` + IsDraft *bool `json:"isDraft"` + OldId *uint `json:"oldId"` } func (req ArticlesCreateRequest) ToEntity() *entity.Articles { diff --git a/app/module/articles/service/articles.service.go b/app/module/articles/service/articles.service.go index 4a34189..545f900 100644 --- a/app/module/articles/service/articles.service.go +++ b/app/module/articles/service/articles.service.go @@ -3,6 +3,7 @@ package service import ( "context" "errors" + "fmt" "github.com/gofiber/fiber/v2" "github.com/minio/minio-go/v7" "github.com/rs/zerolog" @@ -136,6 +137,15 @@ func (_i *articlesService) Save(req request.ArticlesCreateRequest, authToken str 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) if err != nil { return nil, err diff --git a/docs/swagger/docs.go b/docs/swagger/docs.go index 5633c63..ed8a5f6 100644 --- a/docs/swagger/docs.go +++ b/docs/swagger/docs.go @@ -7564,6 +7564,9 @@ const docTemplate = `{ "categoryIds": { "type": "string" }, + "createdAt": { + "type": "string" + }, "description": { "type": "string" }, diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index b8d1369..94e2bab 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -7553,6 +7553,9 @@ "categoryIds": { "type": "string" }, + "createdAt": { + "type": "string" + }, "description": { "type": "string" }, diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index e9d3a13..b2c7b2e 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -225,6 +225,8 @@ definitions: type: integer categoryIds: type: string + createdAt: + type: string description: type: string htmlDescription: