feat: update article filter by category

This commit is contained in:
hanif salafi 2025-02-14 17:42:06 +07:00
parent a28c0f0bf0
commit 8c7cb22687
9 changed files with 60 additions and 2 deletions

View File

@ -7,6 +7,7 @@ type ArticleCategories struct {
Title string `json:"title" gorm:"type:varchar"`
Description string `json:"description" gorm:"type:varchar"`
ThumbnailPath *string `json:"thumbnail_path" gorm:"type:varchar"`
Slug *string `json:"slug" gorm:"type:varchar"`
ParentId *int `json:"parent_id" gorm:"type:int4"`
Tags *string `json:"tags" gorm:"type:varchar"`
Position *int `json:"position" gorm:"type:int4"`

View File

@ -20,6 +20,7 @@ type ArticleCategoriesRepository interface {
GetAll(req request.ArticleCategoriesQueryRequest) (articleCategoriess []*entity.ArticleCategories, paging paginator.Pagination, err error)
FindOne(id uint) (articleCategories *entity.ArticleCategories, err error)
FindOneByOldId(id uint) (articleCategories *entity.ArticleCategories, err error)
FindOneBySlug(slug string) (articleCategories *entity.ArticleCategories, err error)
Create(articleCategories *entity.ArticleCategories) (articleCategoriesReturn *entity.ArticleCategories, err error)
Update(id uint, articleCategories *entity.ArticleCategories) (err error)
Delete(id uint) (err error)
@ -96,6 +97,14 @@ func (_i *articleCategoriesRepository) FindOneByOldId(id uint) (articleCategorie
return articleCategories, nil
}
func (_i *articleCategoriesRepository) FindOneBySlug(slug string) (articleCategories *entity.ArticleCategories, err error) {
if err := _i.DB.DB.Where("slug = ?", slug).First(&articleCategories).Error; err != nil {
return nil, err
}
return articleCategories, nil
}
func (_i *articleCategoriesRepository) Create(articleCategories *entity.ArticleCategories) (articleCategoriesReturn *entity.ArticleCategories, err error) {
result := _i.DB.DB.Create(articleCategories)
return articleCategories, result.Error

View File

@ -25,6 +25,7 @@ type ArticleCategoriesCreateRequest struct {
Description string `json:"description" validate:"required"`
StatusId int `json:"statusId" validate:"required"`
Tags *string `json:"tags"`
Slug *string `json:"slug"`
ParentId *int `json:"parentId"`
OldCategoryId *uint `json:"oldCategoryId"`
}
@ -35,6 +36,7 @@ func (req ArticleCategoriesCreateRequest) ToEntity() *entity.ArticleCategories {
Description: req.Description,
Tags: req.Tags,
ParentId: req.ParentId,
Slug: req.Slug,
OldCategoryId: req.OldCategoryId,
StatusId: req.StatusId,
}
@ -46,6 +48,7 @@ type ArticleCategoriesUpdateRequest struct {
Description string `json:"description" validate:"required"`
StatusId int `json:"statusId" validate:"required"`
Tags *string `json:"tags"`
Slug *string `json:"slug"`
ParentId *int `json:"parentId"`
IsPublish *bool `json:"isPublish"`
PublishedAt *time.Time `json:"publishedAt"`
@ -57,6 +60,7 @@ func (req ArticleCategoriesUpdateRequest) ToEntity() *entity.ArticleCategories {
Title: req.Title,
Description: req.Description,
ParentId: req.ParentId,
Slug: req.Slug,
Tags: req.Tags,
StatusId: req.StatusId,
IsPublish: req.IsPublish,

View File

@ -53,6 +53,7 @@ func (_i *articlesController) All(c *fiber.Ctx) error {
Title: c.Query("title"),
Description: c.Query("description"),
Tags: c.Query("tags"),
Category: c.Query("category"),
CategoryId: c.Query("categoryId"),
TypeId: c.Query("typeId"),
StatusId: c.Query("statusId"),

View File

@ -14,7 +14,8 @@ type ArticlesGeneric interface {
type ArticlesQueryRequest struct {
Title *string `json:"title"`
Description *string `json:"description"`
CategoryId *int `json:"categoryId"`
CategoryId *uint `json:"categoryId"`
Category *string `json:"category"`
TypeId *int `json:"typeId"`
Tags *string `json:"tags"`
CreatedById *int `json:"createdById"`
@ -107,6 +108,7 @@ type ArticlesQueryRequestContext struct {
Title string `json:"title"`
Description string `json:"description"`
CategoryId string `json:"categoryId"`
Category string `json:"category"`
TypeId string `json:"typeId"`
Tags string `json:"tags"`
CreatedById string `json:"createdById"`
@ -124,10 +126,14 @@ func (req ArticlesQueryRequestContext) ToParamRequest() ArticlesQueryRequest {
if description := req.Description; description != "" {
request.Description = &description
}
if category := req.Category; category != "" {
request.Category = &category
}
if categoryIdStr := req.CategoryId; categoryIdStr != "" {
categoryId, err := strconv.Atoi(categoryIdStr)
if err == nil {
request.CategoryId = &categoryId
categoryIdUint := uint(categoryId)
request.CategoryId = &categoryIdUint
}
}
if typeIdStr := req.TypeId; typeIdStr != "" {

View File

@ -80,6 +80,14 @@ func NewArticlesService(
// All implement interface of ArticlesService
func (_i *articlesService) All(req request.ArticlesQueryRequest) (articless []*response.ArticlesResponse, paging paginator.Pagination, err error) {
if req.Category != nil {
findCategory, err := _i.ArticleCategoriesRepo.FindOneBySlug(*req.Category)
if err != nil {
return nil, paging, err
}
req.CategoryId = &findCategory.ID
}
results, paging, err := _i.Repo.GetAll(req)
if err != nil {
return

View File

@ -2163,6 +2163,11 @@ const docTemplate = `{
],
"summary": "Get all Articles",
"parameters": [
{
"type": "string",
"name": "category",
"in": "query"
},
{
"type": "integer",
"name": "categoryId",
@ -7416,6 +7421,9 @@ const docTemplate = `{
"parentId": {
"type": "integer"
},
"slug": {
"type": "string"
},
"statusId": {
"type": "integer"
},
@ -7451,6 +7459,9 @@ const docTemplate = `{
"publishedAt": {
"type": "string"
},
"slug": {
"type": "string"
},
"statusId": {
"type": "integer"
},

View File

@ -2152,6 +2152,11 @@
],
"summary": "Get all Articles",
"parameters": [
{
"type": "string",
"name": "category",
"in": "query"
},
{
"type": "integer",
"name": "categoryId",
@ -7405,6 +7410,9 @@
"parentId": {
"type": "integer"
},
"slug": {
"type": "string"
},
"statusId": {
"type": "integer"
},
@ -7440,6 +7448,9 @@
"publishedAt": {
"type": "string"
},
"slug": {
"type": "string"
},
"statusId": {
"type": "integer"
},

View File

@ -57,6 +57,8 @@ definitions:
type: integer
parentId:
type: integer
slug:
type: string
statusId:
type: integer
tags:
@ -80,6 +82,8 @@ definitions:
type: integer
publishedAt:
type: string
slug:
type: string
statusId:
type: integer
tags:
@ -2140,6 +2144,9 @@ paths:
get:
description: API for getting all Articles
parameters:
- in: query
name: category
type: string
- in: query
name: categoryId
type: integer