fix: add createdBy fullname in detail categories
This commit is contained in:
parent
4b814cbdc5
commit
575c271d27
|
|
@ -7,23 +7,30 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func ArticleCategoriesResponseMapper(articleCategoriesReq *entity.ArticleCategories, host string) (articleCategoriesRes *res.ArticleCategoriesResponse) {
|
||||
// func ArticleCategoriesResponseMapper(articleCategoriesReq *entity.ArticleCategories, host string) (articleCategoriesRes *res.ArticleCategoriesResponse) {
|
||||
func ArticleCategoriesResponseMapper(
|
||||
articleCategoriesReq *entity.ArticleCategories,
|
||||
createdByFullname *string,
|
||||
host string,
|
||||
) (articleCategoriesRes *res.ArticleCategoriesResponse) {
|
||||
|
||||
if articleCategoriesReq != nil {
|
||||
articleCategoriesRes = &res.ArticleCategoriesResponse{
|
||||
ID: articleCategoriesReq.ID,
|
||||
Title: articleCategoriesReq.Title,
|
||||
Description: articleCategoriesReq.Description,
|
||||
Slug: articleCategoriesReq.Slug,
|
||||
ThumbnailPath: articleCategoriesReq.ThumbnailPath,
|
||||
ParentId: articleCategoriesReq.ParentId,
|
||||
OldCategoryId: articleCategoriesReq.OldCategoryId,
|
||||
CreatedById: articleCategoriesReq.CreatedById,
|
||||
StatusId: articleCategoriesReq.StatusId,
|
||||
IsPublish: articleCategoriesReq.IsPublish,
|
||||
PublishedAt: articleCategoriesReq.PublishedAt,
|
||||
IsActive: articleCategoriesReq.IsActive,
|
||||
CreatedAt: articleCategoriesReq.CreatedAt,
|
||||
UpdatedAt: articleCategoriesReq.UpdatedAt,
|
||||
ID: articleCategoriesReq.ID,
|
||||
Title: articleCategoriesReq.Title,
|
||||
Description: articleCategoriesReq.Description,
|
||||
Slug: articleCategoriesReq.Slug,
|
||||
ThumbnailPath: articleCategoriesReq.ThumbnailPath,
|
||||
ParentId: articleCategoriesReq.ParentId,
|
||||
OldCategoryId: articleCategoriesReq.OldCategoryId,
|
||||
CreatedById: articleCategoriesReq.CreatedById,
|
||||
CreatedByFullname: createdByFullname,
|
||||
StatusId: articleCategoriesReq.StatusId,
|
||||
IsPublish: articleCategoriesReq.IsPublish,
|
||||
PublishedAt: articleCategoriesReq.PublishedAt,
|
||||
IsActive: articleCategoriesReq.IsActive,
|
||||
CreatedAt: articleCategoriesReq.CreatedAt,
|
||||
UpdatedAt: articleCategoriesReq.UpdatedAt,
|
||||
}
|
||||
|
||||
if articleCategoriesReq.Tags != nil {
|
||||
|
|
|
|||
|
|
@ -20,10 +20,16 @@ type articleCategoriesRepository struct {
|
|||
Cfg *config.Config
|
||||
}
|
||||
|
||||
type ArticleCategoryWithCreator struct {
|
||||
entity.ArticleCategories
|
||||
CreatedByFullname *string `gorm:"column:created_by_fullname"`
|
||||
}
|
||||
|
||||
// ArticleCategoriesRepository define interface of IArticleCategoriesRepository
|
||||
type ArticleCategoriesRepository interface {
|
||||
GetAll(clientId *uuid.UUID, req request.ArticleCategoriesQueryRequest) (articleCategoriess []*entity.ArticleCategories, paging paginator.Pagination, err error)
|
||||
FindOne(clientId *uuid.UUID, id uint) (articleCategories *entity.ArticleCategories, err error)
|
||||
FindOneWithCreator(clientId *uuid.UUID, id uint) (*ArticleCategoryWithCreator, error)
|
||||
FindOneByOldId(clientId *uuid.UUID, id uint) (articleCategories *entity.ArticleCategories, err error)
|
||||
FindOneBySlug(clientId *uuid.UUID, slug string) (articleCategories *entity.ArticleCategories, err error)
|
||||
Create(articleCategories *entity.ArticleCategories) (articleCategoriesReturn *entity.ArticleCategories, err error)
|
||||
|
|
@ -174,3 +180,29 @@ func (_i *articleCategoriesRepository) Delete(clientId *uuid.UUID, id uint) erro
|
|||
}
|
||||
return query.Delete(&entity.ArticleCategories{}).Error
|
||||
}
|
||||
|
||||
func (_i *articleCategoriesRepository) FindOneWithCreator(
|
||||
clientId *uuid.UUID,
|
||||
id uint,
|
||||
) (*ArticleCategoryWithCreator, error) {
|
||||
|
||||
var result ArticleCategoryWithCreator
|
||||
|
||||
query := _i.DB.DB.Table("article_categories ac").
|
||||
Select(`
|
||||
ac.*,
|
||||
u.fullname AS created_by_fullname
|
||||
`).
|
||||
Joins("LEFT JOIN users u ON u.id = ac.created_by_id").
|
||||
Where("ac.id = ?", id)
|
||||
|
||||
if clientId != nil {
|
||||
query = query.Where("ac.client_id = ?", clientId)
|
||||
}
|
||||
|
||||
if err := query.Scan(&result).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,21 +3,22 @@ package response
|
|||
import "time"
|
||||
|
||||
type ArticleCategoriesResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
ThumbnailUrl string `json:"thumbnailUrl"`
|
||||
Slug *string `json:"slug"`
|
||||
Tags []string `json:"tags"`
|
||||
ThumbnailPath *string `json:"thumbnailPath"`
|
||||
ParentId *int `json:"parentId"`
|
||||
OldCategoryId *uint `json:"oldCategoryId"`
|
||||
CreatedById *uint `json:"createdById"`
|
||||
StatusId int `json:"statusId"`
|
||||
IsPublish *bool `json:"isPublish"`
|
||||
PublishedAt *time.Time `json:"publishedAt"`
|
||||
IsEnabled *bool `json:"isEnabled"`
|
||||
IsActive *bool `json:"isActive"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
ID uint `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
ThumbnailUrl string `json:"thumbnailUrl"`
|
||||
Slug *string `json:"slug"`
|
||||
Tags []string `json:"tags"`
|
||||
ThumbnailPath *string `json:"thumbnailPath"`
|
||||
ParentId *int `json:"parentId"`
|
||||
OldCategoryId *uint `json:"oldCategoryId"`
|
||||
CreatedById *uint `json:"createdById"`
|
||||
CreatedByFullname *string `json:"createdByFullname"`
|
||||
StatusId int `json:"statusId"`
|
||||
IsPublish *bool `json:"isPublish"`
|
||||
PublishedAt *time.Time `json:"publishedAt"`
|
||||
IsEnabled *bool `json:"isEnabled"`
|
||||
IsActive *bool `json:"isActive"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -87,9 +87,16 @@ func (_i *articleCategoriesService) All(authToken string, req request.ArticleCat
|
|||
|
||||
host := _i.Cfg.App.Domain
|
||||
for _, result := range results {
|
||||
articleCategoriess = append(articleCategoriess, mapper.ArticleCategoriesResponseMapper(result, host))
|
||||
articleCategoriess = append(
|
||||
articleCategoriess,
|
||||
mapper.ArticleCategoriesResponseMapper(result, nil, host),
|
||||
)
|
||||
}
|
||||
|
||||
// for _, result := range results {
|
||||
// articleCategoriess = append(articleCategoriess, mapper.ArticleCategoriesResponseMapper(result, host))
|
||||
// }
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -104,12 +111,25 @@ func (_i *articleCategoriesService) Show(authToken string, id uint) (articleCate
|
|||
}
|
||||
}
|
||||
|
||||
result, err := _i.Repo.FindOne(clientId, id)
|
||||
result, err := _i.Repo.FindOneWithCreator(clientId, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
host := _i.Cfg.App.Domain
|
||||
return mapper.ArticleCategoriesResponseMapper(result, host), nil
|
||||
|
||||
return mapper.ArticleCategoriesResponseMapper(
|
||||
&result.ArticleCategories,
|
||||
result.CreatedByFullname,
|
||||
host,
|
||||
), nil
|
||||
|
||||
// result, err := _i.Repo.FindOne(clientId, id)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// host := _i.Cfg.App.Domain
|
||||
// return mapper.ArticleCategoriesResponseMapper(result, host), nil
|
||||
}
|
||||
|
||||
func (_i *articleCategoriesService) ShowByOldId(authToken string, id uint) (articleCategories *response.ArticleCategoriesResponse, err error) {
|
||||
|
|
@ -128,7 +148,8 @@ func (_i *articleCategoriesService) ShowByOldId(authToken string, id uint) (arti
|
|||
return nil, err
|
||||
}
|
||||
host := _i.Cfg.App.Domain
|
||||
return mapper.ArticleCategoriesResponseMapper(result, host), nil
|
||||
return mapper.ArticleCategoriesResponseMapper(result, nil, host), nil
|
||||
// return mapper.ArticleCategoriesResponseMapper(result, host), nil
|
||||
}
|
||||
|
||||
func (_i *articleCategoriesService) ShowBySlug(authToken string, slug string) (articleCategories *response.ArticleCategoriesResponse, err error) {
|
||||
|
|
@ -147,7 +168,8 @@ func (_i *articleCategoriesService) ShowBySlug(authToken string, slug string) (a
|
|||
return nil, err
|
||||
}
|
||||
host := _i.Cfg.App.Domain
|
||||
return mapper.ArticleCategoriesResponseMapper(result, host), nil
|
||||
return mapper.ArticleCategoriesResponseMapper(result, nil, host), nil
|
||||
// return mapper.ArticleCategoriesResponseMapper(result, host), nil
|
||||
}
|
||||
|
||||
func (_i *articleCategoriesService) Save(authToken string, req request.ArticleCategoriesCreateRequest) (articleCategories *entity.ArticleCategories, err error) {
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ func ArticlesResponseMapper(
|
|||
if len(articleCategories) > 0 {
|
||||
for _, result := range articleCategories {
|
||||
if result.Category != nil {
|
||||
articleCategoriesArr = append(articleCategoriesArr, articleCategoriesMapper.ArticleCategoriesResponseMapper(result.Category, host))
|
||||
articleCategoriesArr = append(articleCategoriesArr, articleCategoriesMapper.ArticleCategoriesResponseMapper(result.Category, nil, host))
|
||||
}
|
||||
}
|
||||
log.Info().Interface("articleCategoriesArr", articleCategoriesArr).Msg("")
|
||||
|
|
|
|||
Loading…
Reference in New Issue