From 0d7d08ce6b95c1bff015cd837184c5993524b821 Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Wed, 14 May 2025 23:54:21 +0700 Subject: [PATCH] feat: update articles --- .idea/vcs.xml | 6 --- app/module/articles/articles.module.go | 1 + .../controller/articles.controller.go | 30 ++++++++++++ app/module/articles/mapper/articles.mapper.go | 1 + .../repository/articles.repository.go | 10 +++- .../articles/response/articles.response.go | 1 + .../articles/service/articles.service.go | 12 +++++ docs/swagger/docs.go | 49 +++++++++++++++++++ docs/swagger/swagger.json | 49 +++++++++++++++++++ docs/swagger/swagger.yaml | 31 ++++++++++++ 10 files changed, 183 insertions(+), 7 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/module/articles/articles.module.go b/app/module/articles/articles.module.go index 0ccb93e..55313df 100644 --- a/app/module/articles/articles.module.go +++ b/app/module/articles/articles.module.go @@ -45,6 +45,7 @@ func (_i *ArticlesRouter) RegisterArticlesRoutes() { // define routes _i.App.Route("/articles", func(router fiber.Router) { router.Get("/", articlesController.All) + router.Get("/old-id/:id", articlesController.ShowByOldId) router.Get("/:id", articlesController.Show) router.Post("/", articlesController.Save) router.Put("/:id", articlesController.Update) diff --git a/app/module/articles/controller/articles.controller.go b/app/module/articles/controller/articles.controller.go index 5004945..3a041f8 100644 --- a/app/module/articles/controller/articles.controller.go +++ b/app/module/articles/controller/articles.controller.go @@ -18,6 +18,7 @@ type articlesController struct { type ArticlesController interface { All(c *fiber.Ctx) error Show(c *fiber.Ctx) error + ShowByOldId(c *fiber.Ctx) error Save(c *fiber.Ctx) error SaveThumbnail(c *fiber.Ctx) error Update(c *fiber.Ctx) error @@ -111,6 +112,35 @@ func (_i *articlesController) Show(c *fiber.Ctx) error { }) } +// ShowByOldId Articles +// @Summary Get one Articles +// @Description API for getting one Articles +// @Tags Articles +// @Security Bearer +// @Param id path int true "Articles Old ID" +// @Success 200 {object} response.Response +// @Failure 400 {object} response.BadRequestError +// @Failure 401 {object} response.UnauthorizedError +// @Failure 500 {object} response.InternalServerError +// @Router /articles/old-id/{id} [get] +func (_i *articlesController) ShowByOldId(c *fiber.Ctx) error { + id, err := strconv.ParseUint(c.Params("id"), 10, 0) + if err != nil { + return err + } + + articlesData, err := _i.articlesService.ShowByOldId(uint(id)) + if err != nil { + return err + } + + return utilRes.Resp(c, utilRes.Response{ + Success: true, + Messages: utilRes.Messages{"Articles successfully retrieved"}, + Data: articlesData, + }) +} + // Save Articles // @Summary Create Articles // @Description API for create Articles diff --git a/app/module/articles/mapper/articles.mapper.go b/app/module/articles/mapper/articles.mapper.go index c00f924..38e5dff 100644 --- a/app/module/articles/mapper/articles.mapper.go +++ b/app/module/articles/mapper/articles.mapper.go @@ -68,6 +68,7 @@ func ArticlesResponseMapper( ShareCount: articlesReq.ShareCount, ViewCount: articlesReq.ViewCount, CommentCount: articlesReq.CommentCount, + OldId: articlesReq.OldId, StatusId: articlesReq.StatusId, IsBanner: articlesReq.IsBanner, IsPublish: articlesReq.IsPublish, diff --git a/app/module/articles/repository/articles.repository.go b/app/module/articles/repository/articles.repository.go index 0a35a63..c449acb 100644 --- a/app/module/articles/repository/articles.repository.go +++ b/app/module/articles/repository/articles.repository.go @@ -24,6 +24,7 @@ type ArticlesRepository interface { GetAllPublishSchedule() (articless []*entity.Articles, err error) FindOne(id uint) (articles *entity.Articles, err error) FindByFilename(thumbnailName string) (articleReturn *entity.Articles, err error) + FindByOldId(oldId uint) (articles *entity.Articles, err error) Create(articles *entity.Articles) (articleReturn *entity.Articles, err error) Update(id uint, articles *entity.Articles) (err error) UpdateSkipNull(id uint, articles *entity.Articles) (err error) @@ -125,7 +126,6 @@ func (_i *articlesRepository) FindOne(id uint) (articles *entity.Articles, err e } func (_i *articlesRepository) FindByFilename(thumbnailName string) (articles *entity.Articles, err error) { - if err := _i.DB.DB.Where("thumbnail_name = ?", thumbnailName).First(&articles).Error; err != nil { return nil, err } @@ -133,6 +133,14 @@ func (_i *articlesRepository) FindByFilename(thumbnailName string) (articles *en return articles, nil } +func (_i *articlesRepository) FindByOldId(oldId uint) (articles *entity.Articles, err error) { + if err := _i.DB.DB.Where("old_id = ?", oldId).First(&articles).Error; err != nil { + return nil, err + } + + return articles, nil +} + func (_i *articlesRepository) Create(articles *entity.Articles) (articleReturn *entity.Articles, err error) { result := _i.DB.DB.Create(articles) return articles, result.Error diff --git a/app/module/articles/response/articles.response.go b/app/module/articles/response/articles.response.go index 4f864e9..b1e6075 100644 --- a/app/module/articles/response/articles.response.go +++ b/app/module/articles/response/articles.response.go @@ -24,6 +24,7 @@ type ArticlesResponse struct { ViewCount *int `json:"viewCount"` CommentCount *int `json:"commentCount"` AiArticleId *int `json:"aiArticleId"` + OldId *uint `json:"oldId"` StatusId *int `json:"statusId"` IsBanner *bool `json:"isBanner"` IsPublish *bool `json:"isPublish"` diff --git a/app/module/articles/service/articles.service.go b/app/module/articles/service/articles.service.go index 1e82c0c..c486296 100644 --- a/app/module/articles/service/articles.service.go +++ b/app/module/articles/service/articles.service.go @@ -49,6 +49,7 @@ type articlesService struct { type ArticlesService interface { All(req request.ArticlesQueryRequest) (articles []*response.ArticlesResponse, paging paginator.Pagination, err error) Show(id uint) (articles *response.ArticlesResponse, err error) + ShowByOldId(oldId uint) (articles *response.ArticlesResponse, err error) Save(req request.ArticlesCreateRequest, authToken string) (articles *entity.Articles, err error) SaveThumbnail(c *fiber.Ctx) (err error) Update(id uint, req request.ArticlesUpdateRequest) (err error) @@ -130,6 +131,17 @@ func (_i *articlesService) Show(id uint) (articles *response.ArticlesResponse, e return mapper.ArticlesResponseMapper(_i.Log, host, result, _i.ArticleCategoriesRepo, _i.ArticleCategoryDetailsRepo, _i.ArticleFilesRepo, _i.UsersRepo), nil } +func (_i *articlesService) ShowByOldId(oldId uint) (articles *response.ArticlesResponse, err error) { + result, err := _i.Repo.FindByOldId(oldId) + if err != nil { + return nil, err + } + + host := _i.Cfg.App.Domain + + return mapper.ArticlesResponseMapper(_i.Log, host, result, _i.ArticleCategoriesRepo, _i.ArticleCategoryDetailsRepo, _i.ArticleFilesRepo, _i.UsersRepo), nil +} + func (_i *articlesService) Save(req request.ArticlesCreateRequest, authToken string) (articles *entity.Articles, err error) { _i.Log.Info().Interface("data", req).Msg("") newReq := req.ToEntity() diff --git a/docs/swagger/docs.go b/docs/swagger/docs.go index e5abe74..99d9ee3 100644 --- a/docs/swagger/docs.go +++ b/docs/swagger/docs.go @@ -3588,6 +3588,55 @@ const docTemplate = `{ } } }, + "/articles/old-id/{id}": { + "get": { + "security": [ + { + "Bearer": [] + } + ], + "description": "API for getting one Articles", + "tags": [ + "Articles" + ], + "summary": "Get one Articles", + "parameters": [ + { + "type": "integer", + "description": "Articles Old ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/response.Response" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/response.BadRequestError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/response.UnauthorizedError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/response.InternalServerError" + } + } + } + } + }, "/articles/publish-scheduling": { "post": { "security": [ diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index f1128bb..a8a9ad8 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -3577,6 +3577,55 @@ } } }, + "/articles/old-id/{id}": { + "get": { + "security": [ + { + "Bearer": [] + } + ], + "description": "API for getting one Articles", + "tags": [ + "Articles" + ], + "summary": "Get one Articles", + "parameters": [ + { + "type": "integer", + "description": "Articles Old ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/response.Response" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/response.BadRequestError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/response.UnauthorizedError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/response.InternalServerError" + } + } + } + } + }, "/articles/publish-scheduling": { "post": { "security": [ diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index d3064dc..e1bc8ad 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -3327,6 +3327,37 @@ paths: summary: Update Banner Articles tags: - Articles + /articles/old-id/{id}: + get: + description: API for getting one Articles + parameters: + - description: Articles Old ID + in: path + name: id + required: true + type: integer + responses: + "200": + description: OK + schema: + $ref: '#/definitions/response.Response' + "400": + description: Bad Request + schema: + $ref: '#/definitions/response.BadRequestError' + "401": + description: Unauthorized + schema: + $ref: '#/definitions/response.UnauthorizedError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/response.InternalServerError' + security: + - Bearer: [] + summary: Get one Articles + tags: + - Articles /articles/publish-scheduling: post: description: API for Publish Schedule of Article