From b81d812623cd666beaf9459cd911c20a67ecd700 Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Tue, 7 May 2024 18:08:38 +0700 Subject: [PATCH] feat: fixing article response --- app/module/articles/mapper/articles.mapper.go | 17 ++++++++++------- .../articles/response/articles.response.go | 2 +- app/module/articles/service/articles.service.go | 5 +++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/module/articles/mapper/articles.mapper.go b/app/module/articles/mapper/articles.mapper.go index 370a31f..f641ab7 100644 --- a/app/module/articles/mapper/articles.mapper.go +++ b/app/module/articles/mapper/articles.mapper.go @@ -1,6 +1,7 @@ package mapper import ( + "github.com/rs/zerolog" "go-humas-be/app/database/entity" articleFilesMapper "go-humas-be/app/module/article_files/mapper" articleFilesRepository "go-humas-be/app/module/article_files/repository" @@ -10,6 +11,7 @@ import ( ) func ArticlesResponseMapper( + log zerolog.Logger, articlesReq *entity.Articles, articleFilesRepo articleFilesRepository.ArticleFilesRepository, usersRepo usersRepository.UsersRepository, @@ -19,16 +21,17 @@ func ArticlesResponseMapper( thumbnailUrl += *articlesReq.ThumbnailName } - findUser, _ := usersRepo.FindOne(*articlesReq.CreatedById) createdByName := "" - if findUser != nil { - createdByName = findUser.Fullname + if articlesReq.CreatedById != nil { + findUser, _ := usersRepo.FindOne(*articlesReq.CreatedById) + if findUser != nil { + createdByName = findUser.Fullname + } } articleFiles, _ := articleFilesRepo.FindByArticle(articlesReq.ID) - var articleFilesArr []*articleFilesResponse.ArticleFilesResponse - if len(articleFiles) > 0 { + if articleFiles != nil && len(articleFiles) > 0 { for _, result := range articleFiles { articleFilesArr = append(articleFilesArr, articleFilesMapper.ArticleFilesResponseMapper(result)) } @@ -56,9 +59,9 @@ func ArticlesResponseMapper( IsActive: articlesReq.IsActive, CreatedAt: articlesReq.CreatedAt, UpdatedAt: articlesReq.UpdatedAt, - - ArticleFiles: articleFilesArr, + ArticleFiles: articleFilesArr, } } + return articlesRes } diff --git a/app/module/articles/response/articles.response.go b/app/module/articles/response/articles.response.go index 71d09b2..80fa8db 100644 --- a/app/module/articles/response/articles.response.go +++ b/app/module/articles/response/articles.response.go @@ -19,7 +19,7 @@ type ArticlesResponse struct { PageUrl *string `json:"pageUrl"` CreatedById *uint `json:"createdById"` CreatedByName *string `json:"createdByName"` - ShareCount *int `json:"shareSount"` + ShareCount *int `json:"shareCount"` ViewCount *int `json:"viewCount"` DownloadCount *int `json:"downloadCount"` StatusId *int `json:"statusId"` diff --git a/app/module/articles/service/articles.service.go b/app/module/articles/service/articles.service.go index 0782cc3..4acb391 100644 --- a/app/module/articles/service/articles.service.go +++ b/app/module/articles/service/articles.service.go @@ -75,7 +75,8 @@ func (_i *articlesService) All(req request.ArticlesQueryRequest) (articless []*r Interface("results", results).Msg("") for _, result := range results { - articless = append(articless, mapper.ArticlesResponseMapper(result, _i.ArticleFilesRepo, _i.UsersRepo)) + articleRes := mapper.ArticlesResponseMapper(_i.Log, result, _i.ArticleFilesRepo, _i.UsersRepo) + articless = append(articless, articleRes) } return @@ -87,7 +88,7 @@ func (_i *articlesService) Show(id uint) (articles *response.ArticlesResponse, e return nil, err } - return mapper.ArticlesResponseMapper(result, _i.ArticleFilesRepo, _i.UsersRepo), nil + return mapper.ArticlesResponseMapper(_i.Log, result, _i.ArticleFilesRepo, _i.UsersRepo), nil } func (_i *articlesService) Save(req request.ArticlesCreateRequest, authToken string) (articles *entity.Articles, err error) {