feat: update article, article category

This commit is contained in:
hanif salafi 2025-01-20 16:30:39 +07:00
parent 8ed282b171
commit 21cb8aa74e
14 changed files with 116 additions and 100 deletions

View File

@ -1,12 +0,0 @@
package entity
import "time"
type ArticleCategoryDetails struct {
ID uint `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
ArticleId uint `json:"article_id" gorm:"type:int4"`
CategoryId int `json:"category_id" gorm:"type:int4"`
IsActive bool `json:"is_active" gorm:"type:bool"`
CreatedAt time.Time `json:"created_at" gorm:"default:now()"`
UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"`
}

View File

@ -0,0 +1,16 @@
package article_category_details
import (
entity "go-humas-be/app/database/entity"
"time"
)
type ArticleCategoryDetails struct {
ID uint `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
ArticleId uint `json:"article_id" gorm:"type:int4"`
CategoryId int `json:"category_id" gorm:"type:int4"`
Category *entity.ArticleCategories `json:"category" gorm:"foreignKey:CategoryId;references:ID"`
IsActive bool `json:"is_active" gorm:"type:bool"`
CreatedAt time.Time `json:"created_at" gorm:"default:now()"`
UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"`
}

View File

@ -3,6 +3,7 @@ package database
import (
"github.com/rs/zerolog"
"go-humas-be/app/database/entity"
"go-humas-be/app/database/entity/article_category_details"
"go-humas-be/config/config"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@ -69,7 +70,7 @@ func Models() []interface{} {
return []interface{}{
entity.Articles{},
entity.ArticleCategories{},
entity.ArticleCategoryDetails{},
article_category_details.ArticleCategoryDetails{},
entity.ArticleFiles{},
entity.ArticleNulisAI{},
entity.Cities{},

View File

@ -1,19 +1,20 @@
package mapper
import (
"go-humas-be/app/database/entity"
"go-humas-be/app/database/entity/article_category_details"
res "go-humas-be/app/module/article_category_details/response"
)
func ArticleCategoryDetailsResponseMapper(articleCategoryDetailsReq *entity.ArticleCategoryDetails) (articleCategoryDetailsRes *res.ArticleCategoryDetailsResponse) {
func ArticleCategoryDetailsResponseMapper(articleCategoryDetailsReq *article_category_details.ArticleCategoryDetails) (articleCategoryDetailsRes *res.ArticleCategoryDetailsResponse) {
if articleCategoryDetailsReq != nil {
articleCategoryDetailsRes = &res.ArticleCategoryDetailsResponse{
ID: articleCategoryDetailsReq.ID,
ArticleId: articleCategoryDetailsReq.ArticleId,
CategoryId: articleCategoryDetailsReq.CategoryId,
IsActive: articleCategoryDetailsReq.IsActive,
CreatedAt: articleCategoryDetailsReq.CreatedAt,
UpdatedAt: articleCategoryDetailsReq.UpdatedAt,
ID: articleCategoryDetailsReq.ID,
ArticleId: articleCategoryDetailsReq.ArticleId,
CategoryId: articleCategoryDetailsReq.CategoryId,
IsActive: articleCategoryDetailsReq.IsActive,
CreatedAt: articleCategoryDetailsReq.CreatedAt,
UpdatedAt: articleCategoryDetailsReq.UpdatedAt,
}
}
return articleCategoryDetailsRes

View File

@ -2,7 +2,7 @@ package repository
import (
"go-humas-be/app/database"
"go-humas-be/app/database/entity"
"go-humas-be/app/database/entity/article_category_details"
"go-humas-be/app/module/article_category_details/request"
"go-humas-be/utils/paginator"
)
@ -13,10 +13,11 @@ type articleCategoryDetailsRepository struct {
// ArticleCategoryDetailsRepository define interface of IArticleCategoryDetailsRepository
type ArticleCategoryDetailsRepository interface {
GetAll(req request.ArticleCategoryDetailsQueryRequest) (articleCategoryDetailss []*entity.ArticleCategoryDetails, paging paginator.Pagination, err error)
FindOne(id uint) (articleCategoryDetails *entity.ArticleCategoryDetails, err error)
Create(articleCategoryDetails *entity.ArticleCategoryDetails) (err error)
Update(id uint, articleCategoryDetails *entity.ArticleCategoryDetails) (err error)
GetAll(req request.ArticleCategoryDetailsQueryRequest) (articleCategoryDetailss []*article_category_details.ArticleCategoryDetails, paging paginator.Pagination, err error)
FindOne(id uint) (articleCategoryDetails *article_category_details.ArticleCategoryDetails, err error)
FindByArticleId(articleId uint) (articleCategoryDetailss []*article_category_details.ArticleCategoryDetails, err error)
Create(articleCategoryDetails *article_category_details.ArticleCategoryDetails) (err error)
Update(id uint, articleCategoryDetails *article_category_details.ArticleCategoryDetails) (err error)
Delete(id uint) (err error)
}
@ -27,10 +28,10 @@ func NewArticleCategoryDetailsRepository(db *database.Database) ArticleCategoryD
}
// implement interface of IArticleCategoryDetailsRepository
func (_i *articleCategoryDetailsRepository) GetAll(req request.ArticleCategoryDetailsQueryRequest) (articleCategoryDetailss []*entity.ArticleCategoryDetails, paging paginator.Pagination, err error) {
func (_i *articleCategoryDetailsRepository) GetAll(req request.ArticleCategoryDetailsQueryRequest) (articleCategoryDetailss []*article_category_details.ArticleCategoryDetails, paging paginator.Pagination, err error) {
var count int64
query := _i.DB.DB.Model(&entity.ArticleCategoryDetails{})
query := _i.DB.DB.Model(&article_category_details.ArticleCategoryDetails{})
query.Count(&count)
req.Pagination.Count = count
@ -46,7 +47,7 @@ func (_i *articleCategoryDetailsRepository) GetAll(req request.ArticleCategoryDe
return
}
func (_i *articleCategoryDetailsRepository) FindOne(id uint) (articleCategoryDetails *entity.ArticleCategoryDetails, err error) {
func (_i *articleCategoryDetailsRepository) FindOne(id uint) (articleCategoryDetails *article_category_details.ArticleCategoryDetails, err error) {
if err := _i.DB.DB.First(&articleCategoryDetails, id).Error; err != nil {
return nil, err
}
@ -54,16 +55,24 @@ func (_i *articleCategoryDetailsRepository) FindOne(id uint) (articleCategoryDet
return articleCategoryDetails, nil
}
func (_i *articleCategoryDetailsRepository) Create(articleCategoryDetails *entity.ArticleCategoryDetails) (err error) {
func (_i *articleCategoryDetailsRepository) FindByArticleId(articleId uint) (articleCategoryDetailss []*article_category_details.ArticleCategoryDetails, err error) {
if err := _i.DB.DB.Where("article_id = ?", articleId).Preload("Category").Find(&articleCategoryDetailss).Error; err != nil {
return nil, err
}
return articleCategoryDetailss, nil
}
func (_i *articleCategoryDetailsRepository) Create(articleCategoryDetails *article_category_details.ArticleCategoryDetails) (err error) {
return _i.DB.DB.Create(articleCategoryDetails).Error
}
func (_i *articleCategoryDetailsRepository) Update(id uint, articleCategoryDetails *entity.ArticleCategoryDetails) (err error) {
return _i.DB.DB.Model(&entity.ArticleCategoryDetails{}).
Where(&entity.ArticleCategoryDetails{ID: id}).
func (_i *articleCategoryDetailsRepository) Update(id uint, articleCategoryDetails *article_category_details.ArticleCategoryDetails) (err error) {
return _i.DB.DB.Model(&article_category_details.ArticleCategoryDetails{}).
Where(&article_category_details.ArticleCategoryDetails{ID: id}).
Updates(articleCategoryDetails).Error
}
func (_i *articleCategoryDetailsRepository) Delete(id uint) error {
return _i.DB.DB.Delete(&entity.ArticleCategoryDetails{}, id).Error
return _i.DB.DB.Delete(&article_category_details.ArticleCategoryDetails{}, id).Error
}

View File

@ -1,7 +1,7 @@
package request
import (
"go-humas-be/app/database/entity"
"go-humas-be/app/database/entity/article_category_details"
"go-humas-be/utils/paginator"
"time"
)
@ -23,8 +23,8 @@ type ArticleCategoryDetailsCreateRequest struct {
IsActive bool `json:"is_active" validate:"required"`
}
func (req ArticleCategoryDetailsCreateRequest) ToEntity() *entity.ArticleCategoryDetails {
return &entity.ArticleCategoryDetails{
func (req ArticleCategoryDetailsCreateRequest) ToEntity() *article_category_details.ArticleCategoryDetails {
return &article_category_details.ArticleCategoryDetails{
ArticleId: req.ArticleId,
CategoryId: req.CategoryId,
IsActive: req.IsActive,
@ -40,8 +40,8 @@ type ArticleCategoryDetailsUpdateRequest struct {
UpdatedAt time.Time `json:"updated_at"`
}
func (req ArticleCategoryDetailsUpdateRequest) ToEntity() *entity.ArticleCategoryDetails {
return &entity.ArticleCategoryDetails{
func (req ArticleCategoryDetailsUpdateRequest) ToEntity() *article_category_details.ArticleCategoryDetails {
return &article_category_details.ArticleCategoryDetails{
ID: req.ID,
ArticleId: req.ArticleId,
CategoryId: req.CategoryId,

View File

@ -85,7 +85,6 @@ func (_i *articleNulisAIService) Publish(req request.ArticleNulisAICreateRequest
articleReq := articles.ArticlesCreateRequest{
Title: newReq.Title,
Slug: utilSvc.MakeSlug(req.Title),
CategoryId: newReq.CategoryId,
Tags: newReq.Tags,
TypeId: 1,
Description: newReq.Description,

View File

@ -3,7 +3,10 @@ package mapper
import (
"github.com/rs/zerolog"
"go-humas-be/app/database/entity"
articleCategoriesMapper "go-humas-be/app/module/article_categories/mapper"
articleCategoriesRepository "go-humas-be/app/module/article_categories/repository"
articleCategoriesResponse "go-humas-be/app/module/article_categories/response"
articleCategoryDetailsRepository "go-humas-be/app/module/article_category_details/repository"
articleFilesMapper "go-humas-be/app/module/article_files/mapper"
articleFilesRepository "go-humas-be/app/module/article_files/repository"
articleFilesResponse "go-humas-be/app/module/article_files/response"
@ -16,6 +19,7 @@ func ArticlesResponseMapper(
host string,
articlesReq *entity.Articles,
articleCategoriesRepo articleCategoriesRepository.ArticleCategoriesRepository,
articleCategoryDetailsRepo articleCategoryDetailsRepository.ArticleCategoryDetailsRepository,
articleFilesRepo articleFilesRepository.ArticleFilesRepository,
usersRepo usersRepository.UsersRepository,
) (articlesRes *res.ArticlesResponse) {
@ -29,11 +33,13 @@ func ArticlesResponseMapper(
}
categoryName := ""
if articlesReq.CategoryId != 0 {
findCategory, _ := articleCategoriesRepo.FindOne(uint(articlesReq.CategoryId))
if findCategory != nil {
categoryName = findCategory.Title
articleCategories, _ := articleCategoryDetailsRepo.FindByArticleId(articlesReq.ID)
var articleCategoriesArr []*articleCategoriesResponse.ArticleCategoriesResponse
if articleCategories != nil && len(articleCategories) > 0 {
for _, result := range articleCategories {
articleCategoriesArr = append(articleCategoriesArr, articleCategoriesMapper.ArticleCategoriesResponseMapper(result.Category, ""))
}
log.Info().Interface("articleCategoriesArr", articleCategoriesArr).Msg("")
}
articleFiles, _ := articleFilesRepo.FindByArticle(articlesReq.ID)
@ -46,28 +52,29 @@ func ArticlesResponseMapper(
if articlesReq != nil {
articlesRes = &res.ArticlesResponse{
ID: articlesReq.ID,
Title: articlesReq.Title,
Slug: articlesReq.Slug,
Description: articlesReq.Description,
HtmlDescription: articlesReq.HtmlDescription,
TypeId: articlesReq.TypeId,
Tags: articlesReq.Tags,
CategoryId: articlesReq.CategoryId,
CategoryName: categoryName,
PageUrl: articlesReq.PageUrl,
CreatedById: articlesReq.CreatedById,
CreatedByName: &createdByName,
ShareCount: articlesReq.ShareCount,
ViewCount: articlesReq.ViewCount,
DownloadCount: articlesReq.DownloadCount,
StatusId: articlesReq.StatusId,
IsPublish: articlesReq.IsPublish,
PublishedAt: articlesReq.PublishedAt,
IsActive: articlesReq.IsActive,
CreatedAt: articlesReq.CreatedAt,
UpdatedAt: articlesReq.UpdatedAt,
ArticleFiles: articleFilesArr,
ID: articlesReq.ID,
Title: articlesReq.Title,
Slug: articlesReq.Slug,
Description: articlesReq.Description,
HtmlDescription: articlesReq.HtmlDescription,
TypeId: articlesReq.TypeId,
Tags: articlesReq.Tags,
CategoryId: articlesReq.CategoryId,
CategoryName: categoryName,
PageUrl: articlesReq.PageUrl,
CreatedById: articlesReq.CreatedById,
CreatedByName: &createdByName,
ShareCount: articlesReq.ShareCount,
ViewCount: articlesReq.ViewCount,
DownloadCount: articlesReq.DownloadCount,
StatusId: articlesReq.StatusId,
IsPublish: articlesReq.IsPublish,
PublishedAt: articlesReq.PublishedAt,
IsActive: articlesReq.IsActive,
CreatedAt: articlesReq.CreatedAt,
UpdatedAt: articlesReq.UpdatedAt,
ArticleFiles: articleFilesArr,
ArticleCategories: articleCategoriesArr,
}
if articlesReq.ThumbnailName != nil {

View File

@ -28,7 +28,6 @@ type ArticlesCreateRequest struct {
Slug string `json:"slug" validate:"required"`
Description string `json:"description" validate:"required"`
HtmlDescription string `json:"htmlDescription" validate:"required"`
CategoryId int `json:"categoryId" validate:"required"`
CategoryIds string `json:"categoryIds" validate:"required"`
TypeId int `json:"typeId" validate:"required"`
Tags string `json:"tags" validate:"required"`
@ -43,7 +42,6 @@ func (req ArticlesCreateRequest) ToEntity() *entity.Articles {
HtmlDescription: req.HtmlDescription,
TypeId: req.TypeId,
Tags: req.Tags,
CategoryId: req.CategoryId,
OldId: req.OldId,
}
}
@ -53,7 +51,7 @@ type ArticlesUpdateRequest struct {
Slug string `json:"slug" validate:"required"`
Description string `json:"description" validate:"required"`
HtmlDescription string `json:"htmlDescription" validate:"required"`
CategoryId int `json:"categoryId" validate:"required"`
CategoryIds string `json:"categoryIds" validate:"required"`
TypeId int `json:"typeId" validate:"required"`
Tags string `json:"tags" validate:"required"`
CreatedById *uint `json:"createdById"`
@ -67,7 +65,6 @@ func (req ArticlesUpdateRequest) ToEntity() *entity.Articles {
Slug: req.Slug,
Description: req.Description,
HtmlDescription: req.HtmlDescription,
CategoryId: req.CategoryId,
TypeId: req.TypeId,
Tags: req.Tags,
StatusId: req.StatusId,
@ -79,7 +76,6 @@ func (req ArticlesUpdateRequest) ToEntity() *entity.Articles {
Slug: req.Slug,
Description: req.Description,
HtmlDescription: req.HtmlDescription,
CategoryId: req.CategoryId,
TypeId: req.TypeId,
Tags: req.Tags,
StatusId: req.StatusId,

View File

@ -1,6 +1,7 @@
package response
import (
articleCategoriesResponse "go-humas-be/app/module/article_categories/response"
articleFilesResponse "go-humas-be/app/module/article_files/response"
"time"
)
@ -29,5 +30,6 @@ type ArticlesResponse struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ArticleFiles []*articleFilesResponse.ArticleFilesResponse `json:"files"`
ArticleFiles []*articleFilesResponse.ArticleFilesResponse `json:"files"`
ArticleCategories []*articleCategoriesResponse.ArticleCategoriesResponse `json:"categories"`
}

View File

@ -92,7 +92,7 @@ func (_i *articlesService) All(req request.ArticlesQueryRequest) (articless []*r
port := _i.Cfg.App.ExternalPort
for _, result := range results {
articleRes := mapper.ArticlesResponseMapper(_i.Log, host+port, result, _i.ArticleCategoriesRepo, _i.ArticleFilesRepo, _i.UsersRepo)
articleRes := mapper.ArticlesResponseMapper(_i.Log, host+port, result, _i.ArticleCategoriesRepo, _i.ArticleCategoryDetailsRepo, _i.ArticleFilesRepo, _i.UsersRepo)
articless = append(articless, articleRes)
}
@ -108,7 +108,7 @@ func (_i *articlesService) Show(id uint) (articles *response.ArticlesResponse, e
host := _i.Cfg.App.Domain
port := _i.Cfg.App.ExternalPort
return mapper.ArticlesResponseMapper(_i.Log, host+port, result, _i.ArticleCategoriesRepo, _i.ArticleFilesRepo, _i.UsersRepo), nil
return mapper.ArticlesResponseMapper(_i.Log, host+port, result, _i.ArticleCategoriesRepo, _i.ArticleCategoryDetailsRepo, _i.ArticleFilesRepo, _i.UsersRepo), nil
}
func (_i *articlesService) Save(req request.ArticlesCreateRequest, authToken string) (articles *entity.Articles, err error) {
@ -128,20 +128,28 @@ func (_i *articlesService) Save(req request.ArticlesCreateRequest, authToken str
categoryIds = strings.Split(req.CategoryIds, ",")
}
for categoryId := range categoryIds {
findCategory, err := _i.ArticleCategoriesRepo.FindOne(uint(categoryId))
_i.Log.Info().Interface("categoryIds", categoryIds).Msg("")
for _, categoryId := range categoryIds {
categoryIdInt, _ := strconv.Atoi(categoryId)
_i.Log.Info().Interface("categoryIdUint", uint(categoryIdInt)).Msg("")
findCategory, err := _i.ArticleCategoriesRepo.FindOne(uint(categoryIdInt))
_i.Log.Info().Interface("findCategory", findCategory).Msg("")
if err != nil {
return nil, err
}
if findCategory != nil {
if findCategory == nil {
return nil, errors.New("category not found")
}
categoryReq := articleCategoryDetailsReq.ArticleCategoryDetailsCreateRequest{
ArticleId: saveArticleRepo.ID,
CategoryId: categoryId,
CategoryId: categoryIdInt,
IsActive: true,
}
newCategoryReq := categoryReq.ToEntity()

View File

@ -6307,7 +6307,6 @@ const docTemplate = `{
"request.ArticlesCreateRequest": {
"type": "object",
"required": [
"categoryId",
"categoryIds",
"description",
"htmlDescription",
@ -6317,9 +6316,6 @@ const docTemplate = `{
"typeId"
],
"properties": {
"categoryId": {
"type": "integer"
},
"categoryIds": {
"type": "string"
},
@ -6349,7 +6345,7 @@ const docTemplate = `{
"request.ArticlesUpdateRequest": {
"type": "object",
"required": [
"categoryId",
"categoryIds",
"description",
"htmlDescription",
"slug",
@ -6358,8 +6354,8 @@ const docTemplate = `{
"typeId"
],
"properties": {
"categoryId": {
"type": "integer"
"categoryIds": {
"type": "string"
},
"createdById": {
"type": "integer"

View File

@ -6296,7 +6296,6 @@
"request.ArticlesCreateRequest": {
"type": "object",
"required": [
"categoryId",
"categoryIds",
"description",
"htmlDescription",
@ -6306,9 +6305,6 @@
"typeId"
],
"properties": {
"categoryId": {
"type": "integer"
},
"categoryIds": {
"type": "string"
},
@ -6338,7 +6334,7 @@
"request.ArticlesUpdateRequest": {
"type": "object",
"required": [
"categoryId",
"categoryIds",
"description",
"htmlDescription",
"slug",
@ -6347,8 +6343,8 @@
"typeId"
],
"properties": {
"categoryId": {
"type": "integer"
"categoryIds": {
"type": "string"
},
"createdById": {
"type": "integer"

View File

@ -157,8 +157,6 @@ definitions:
type: object
request.ArticlesCreateRequest:
properties:
categoryId:
type: integer
categoryIds:
type: string
description:
@ -176,7 +174,6 @@ definitions:
typeId:
type: integer
required:
- categoryId
- categoryIds
- description
- htmlDescription
@ -187,8 +184,8 @@ definitions:
type: object
request.ArticlesUpdateRequest:
properties:
categoryId:
type: integer
categoryIds:
type: string
createdById:
type: integer
description:
@ -206,7 +203,7 @@ definitions:
typeId:
type: integer
required:
- categoryId
- categoryIds
- description
- htmlDescription
- slug