From af72ce00b6c1dfa27e8a046ce474b6b33a4677a7 Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Fri, 31 Jan 2025 18:34:25 +0700 Subject: [PATCH] feat: update article comments response --- .../mapper/article_comments.mapper.go | 31 +++++++++++++------ .../response/article_comments.response.go | 19 ++++++------ .../service/article_comments.service.go | 4 +-- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/app/module/article_comments/mapper/article_comments.mapper.go b/app/module/article_comments/mapper/article_comments.mapper.go index 247238d..f833d23 100644 --- a/app/module/article_comments/mapper/article_comments.mapper.go +++ b/app/module/article_comments/mapper/article_comments.mapper.go @@ -3,20 +3,31 @@ package mapper import ( "go-humas-be/app/database/entity" res "go-humas-be/app/module/article_comments/response" + usersRepository "go-humas-be/app/module/users/repository" ) -func ArticleCommentsResponseMapper(articleCommentsReq *entity.ArticleComments) (articleCommentsRes *res.ArticleCommentsResponse) { +func ArticleCommentsResponseMapper(articleCommentsReq *entity.ArticleComments, usersRepo usersRepository.UsersRepository) (articleCommentsRes *res.ArticleCommentsResponse) { if articleCommentsReq != nil { + + commentFromName := "" + if articleCommentsReq.CommentFrom != nil { + findUser, _ := usersRepo.FindOne(*articleCommentsReq.CommentFrom) + if findUser != nil { + commentFromName = findUser.Fullname + } + } + articleCommentsRes = &res.ArticleCommentsResponse{ - ID: articleCommentsReq.ID, - Message: articleCommentsReq.Message, - ArticleId: articleCommentsReq.ArticleId, - CommentFrom: articleCommentsReq.CommentFrom, - ParentId: articleCommentsReq.ParentId, - IsPublic: articleCommentsReq.IsPublic, - IsActive: articleCommentsReq.IsActive, - CreatedAt: articleCommentsReq.CreatedAt, - UpdatedAt: articleCommentsReq.UpdatedAt, + ID: articleCommentsReq.ID, + Message: articleCommentsReq.Message, + ArticleId: articleCommentsReq.ArticleId, + CommentFromId: articleCommentsReq.CommentFrom, + CommentFromName: &commentFromName, + ParentId: articleCommentsReq.ParentId, + IsPublic: articleCommentsReq.IsPublic, + IsActive: articleCommentsReq.IsActive, + CreatedAt: articleCommentsReq.CreatedAt, + UpdatedAt: articleCommentsReq.UpdatedAt, } } return articleCommentsRes diff --git a/app/module/article_comments/response/article_comments.response.go b/app/module/article_comments/response/article_comments.response.go index 3e1959b..668dbbd 100644 --- a/app/module/article_comments/response/article_comments.response.go +++ b/app/module/article_comments/response/article_comments.response.go @@ -3,13 +3,14 @@ package response import "time" type ArticleCommentsResponse struct { - ID uint `json:"id"` - Message string `json:"message"` - ArticleId uint `json:"articleId"` - CommentFrom *uint `json:"commentFrom"` - ParentId *int `json:"parentId"` - IsPublic bool `json:"isPublic"` - IsActive bool `json:"isActive"` - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` + ID uint `json:"id"` + Message string `json:"message"` + ArticleId uint `json:"articleId"` + CommentFromId *uint `json:"commentFromId"` + CommentFromName *string `json:"commentFromName"` + ParentId *int `json:"parentId"` + IsPublic bool `json:"isPublic"` + IsActive bool `json:"isActive"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` } diff --git a/app/module/article_comments/service/article_comments.service.go b/app/module/article_comments/service/article_comments.service.go index 75dc7e2..a2569e9 100644 --- a/app/module/article_comments/service/article_comments.service.go +++ b/app/module/article_comments/service/article_comments.service.go @@ -47,7 +47,7 @@ func (_i *articleCommentsService) All(req request.ArticleCommentsQueryRequest) ( } for _, result := range results { - articleCommentss = append(articleCommentss, mapper.ArticleCommentsResponseMapper(result)) + articleCommentss = append(articleCommentss, mapper.ArticleCommentsResponseMapper(result, _i.UsersRepo)) } return @@ -59,7 +59,7 @@ func (_i *articleCommentsService) Show(id uint) (articleComments *response.Artic return nil, err } - return mapper.ArticleCommentsResponseMapper(result), nil + return mapper.ArticleCommentsResponseMapper(result, _i.UsersRepo), nil } func (_i *articleCommentsService) Save(req request.ArticleCommentsCreateRequest, authToken string) (articleComments *entity.ArticleComments, err error) {