feat: update article comments response

This commit is contained in:
hanif salafi 2025-01-31 18:34:25 +07:00
parent 17d19d6be4
commit 5869c11b24
3 changed files with 33 additions and 21 deletions

View File

@ -3,20 +3,31 @@ package mapper
import ( import (
"go-humas-be/app/database/entity" "go-humas-be/app/database/entity"
res "go-humas-be/app/module/article_comments/response" 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 { if articleCommentsReq != nil {
commentFromName := ""
if articleCommentsReq.CommentFrom != nil {
findUser, _ := usersRepo.FindOne(*articleCommentsReq.CommentFrom)
if findUser != nil {
commentFromName = findUser.Fullname
}
}
articleCommentsRes = &res.ArticleCommentsResponse{ articleCommentsRes = &res.ArticleCommentsResponse{
ID: articleCommentsReq.ID, ID: articleCommentsReq.ID,
Message: articleCommentsReq.Message, Message: articleCommentsReq.Message,
ArticleId: articleCommentsReq.ArticleId, ArticleId: articleCommentsReq.ArticleId,
CommentFrom: articleCommentsReq.CommentFrom, CommentFromId: articleCommentsReq.CommentFrom,
ParentId: articleCommentsReq.ParentId, CommentFromName: &commentFromName,
IsPublic: articleCommentsReq.IsPublic, ParentId: articleCommentsReq.ParentId,
IsActive: articleCommentsReq.IsActive, IsPublic: articleCommentsReq.IsPublic,
CreatedAt: articleCommentsReq.CreatedAt, IsActive: articleCommentsReq.IsActive,
UpdatedAt: articleCommentsReq.UpdatedAt, CreatedAt: articleCommentsReq.CreatedAt,
UpdatedAt: articleCommentsReq.UpdatedAt,
} }
} }
return articleCommentsRes return articleCommentsRes

View File

@ -3,13 +3,14 @@ package response
import "time" import "time"
type ArticleCommentsResponse struct { type ArticleCommentsResponse struct {
ID uint `json:"id"` ID uint `json:"id"`
Message string `json:"message"` Message string `json:"message"`
ArticleId uint `json:"articleId"` ArticleId uint `json:"articleId"`
CommentFrom *uint `json:"commentFrom"` CommentFromId *uint `json:"commentFromId"`
ParentId *int `json:"parentId"` CommentFromName *string `json:"commentFromName"`
IsPublic bool `json:"isPublic"` ParentId *int `json:"parentId"`
IsActive bool `json:"isActive"` IsPublic bool `json:"isPublic"`
CreatedAt time.Time `json:"createdAt"` IsActive bool `json:"isActive"`
UpdatedAt time.Time `json:"updatedAt"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
} }

View File

@ -47,7 +47,7 @@ func (_i *articleCommentsService) All(req request.ArticleCommentsQueryRequest) (
} }
for _, result := range results { for _, result := range results {
articleCommentss = append(articleCommentss, mapper.ArticleCommentsResponseMapper(result)) articleCommentss = append(articleCommentss, mapper.ArticleCommentsResponseMapper(result, _i.UsersRepo))
} }
return return
@ -59,7 +59,7 @@ func (_i *articleCommentsService) Show(id uint) (articleComments *response.Artic
return nil, err 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) { func (_i *articleCommentsService) Save(req request.ArticleCommentsCreateRequest, authToken string) (articleComments *entity.ArticleComments, err error) {