feat: update article categories
This commit is contained in:
parent
4fdd354ea1
commit
3796ae4943
|
|
@ -37,6 +37,7 @@ func NewArticleCategoriesController(articleCategoriesService service.ArticleCate
|
|||
// @Description API for getting all ArticleCategories
|
||||
// @Tags Article Categories
|
||||
// @Security Bearer
|
||||
// @Param Authorization header string true "Insert your access token" default(Bearer <Add access token here>)
|
||||
// @Param req query request.ArticleCategoriesQueryRequest false "query parameters"
|
||||
// @Param req query paginator.Pagination false "pagination parameters"
|
||||
// @Success 200 {object} response.Response
|
||||
|
|
@ -50,6 +51,8 @@ func (_i *articleCategoriesController) All(c *fiber.Ctx) error {
|
|||
return err
|
||||
}
|
||||
|
||||
authToken := c.Get("Authorization")
|
||||
|
||||
reqContext := request.ArticleCategoriesQueryRequestContext{
|
||||
Title: c.Query("title"),
|
||||
Description: c.Query("description"),
|
||||
|
|
@ -60,7 +63,7 @@ func (_i *articleCategoriesController) All(c *fiber.Ctx) error {
|
|||
req := reqContext.ToParamRequest()
|
||||
req.Pagination = paginate
|
||||
|
||||
articleCategoriesData, paging, err := _i.articleCategoriesService.All(req)
|
||||
articleCategoriesData, paging, err := _i.articleCategoriesService.All(req, authToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,24 +38,32 @@ func (_i *articleCategoriesRepository) GetAll(req request.ArticleCategoriesQuery
|
|||
var count int64
|
||||
|
||||
query := _i.DB.DB.Model(&entity.ArticleCategories{})
|
||||
query = query.Where("is_active = ?", true)
|
||||
|
||||
if req.UserLevelId != nil {
|
||||
query = _i.DB.DB.Model(&entity.ArticleCategories{}).
|
||||
Joins("LEFT JOIN users ON article_categories.created_by_id = users.id").
|
||||
Joins("LEFT JOIN user_levels ON users.user_level_id = user_levels.id").
|
||||
Where("user_levels.id = ? or user_levels.parent_level_id = ?", *req.UserLevelId, *req.UserLevelId)
|
||||
}
|
||||
|
||||
query = query.Where("article_categories.is_active = ?", true)
|
||||
|
||||
if req.Title != nil && *req.Title != "" {
|
||||
title := strings.ToLower(*req.Title)
|
||||
query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%")
|
||||
query = query.Where("LOWER(article_categories.title) LIKE ?", "%"+strings.ToLower(title)+"%")
|
||||
}
|
||||
if req.Description != nil && *req.Description != "" {
|
||||
description := strings.ToLower(*req.Description)
|
||||
query = query.Where("LOWER(description) LIKE ?", "%"+strings.ToLower(description)+"%")
|
||||
query = query.Where("LOWER(article_categories.description) LIKE ?", "%"+strings.ToLower(description)+"%")
|
||||
}
|
||||
if req.ParentId != nil {
|
||||
query = query.Where("parent_id = ?", req.ParentId)
|
||||
query = query.Where("article_categories.parent_id = ?", req.ParentId)
|
||||
}
|
||||
if req.IsPublish != nil {
|
||||
query = query.Where("is_publish = ?", req.IsPublish)
|
||||
query = query.Where("article_categories.is_publish = ?", req.IsPublish)
|
||||
}
|
||||
if req.StatusId != nil {
|
||||
query = query.Where("status_id = ?", req.StatusId)
|
||||
query = query.Where("article_categories.status_id = ?", req.StatusId)
|
||||
}
|
||||
query.Count(&count)
|
||||
|
||||
|
|
|
|||
|
|
@ -12,12 +12,14 @@ type ArticleCategoriesGeneric interface {
|
|||
}
|
||||
|
||||
type ArticleCategoriesQueryRequest struct {
|
||||
Title *string `json:"title"`
|
||||
Description *string `json:"description"`
|
||||
ParentId *int `json:"parentId"`
|
||||
StatusId *int `json:"statusId"`
|
||||
IsPublish *bool `json:"isPublish"`
|
||||
Pagination *paginator.Pagination `json:"pagination"`
|
||||
Title *string `json:"title"`
|
||||
Description *string `json:"description"`
|
||||
UserLevelId *uint `json:"UserLevelId"`
|
||||
UserLevelNumber *int `json:"UserLevelNumber"`
|
||||
ParentId *int `json:"parentId"`
|
||||
StatusId *int `json:"statusId"`
|
||||
IsPublish *bool `json:"isPublish"`
|
||||
Pagination *paginator.Pagination `json:"pagination"`
|
||||
}
|
||||
|
||||
type ArticleCategoriesCreateRequest struct {
|
||||
|
|
@ -26,6 +28,7 @@ type ArticleCategoriesCreateRequest struct {
|
|||
StatusId int `json:"statusId" validate:"required"`
|
||||
Tags *string `json:"tags"`
|
||||
Slug *string `json:"slug"`
|
||||
CreatedById *uint `json:"createdById"`
|
||||
ParentId *int `json:"parentId"`
|
||||
OldCategoryId *uint `json:"oldCategoryId"`
|
||||
}
|
||||
|
|
@ -50,6 +53,7 @@ type ArticleCategoriesUpdateRequest struct {
|
|||
Tags *string `json:"tags"`
|
||||
Slug *string `json:"slug"`
|
||||
ParentId *int `json:"parentId"`
|
||||
CreatedById *uint `json:"createdById"`
|
||||
IsPublish *bool `json:"isPublish"`
|
||||
PublishedAt *time.Time `json:"publishedAt"`
|
||||
}
|
||||
|
|
@ -70,11 +74,13 @@ func (req ArticleCategoriesUpdateRequest) ToEntity() *entity.ArticleCategories {
|
|||
}
|
||||
|
||||
type ArticleCategoriesQueryRequestContext struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
ParentId string `json:"parentId"`
|
||||
StatusId string `json:"statusId"`
|
||||
IsPublish string `json:"isPublish"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
ParentId string `json:"parentId"`
|
||||
StatusId string `json:"statusId"`
|
||||
IsPublish string `json:"isPublish"`
|
||||
UserLevelId string `json:"UserLevelId"`
|
||||
UserLevelNumber string `json:"UserLevelNumber"`
|
||||
}
|
||||
|
||||
func (req ArticleCategoriesQueryRequestContext) ToParamRequest() ArticleCategoriesQueryRequest {
|
||||
|
|
@ -104,6 +110,19 @@ func (req ArticleCategoriesQueryRequestContext) ToParamRequest() ArticleCategori
|
|||
request.StatusId = &statusId
|
||||
}
|
||||
}
|
||||
if userLevelIdStr := req.UserLevelId; userLevelIdStr != "" {
|
||||
userLevelId, err := strconv.Atoi(userLevelIdStr)
|
||||
if err == nil {
|
||||
userLevelIdUint := uint(userLevelId)
|
||||
request.UserLevelId = &userLevelIdUint
|
||||
}
|
||||
}
|
||||
if userLevelNumberStr := req.UserLevelNumber; userLevelNumberStr != "" {
|
||||
userLevelNumber, err := strconv.Atoi(userLevelNumberStr)
|
||||
if err == nil {
|
||||
request.UserLevelNumber = &userLevelNumber
|
||||
}
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ type articleCategoriesService struct {
|
|||
|
||||
// ArticleCategoriesService define interface of IArticleCategoriesService
|
||||
type ArticleCategoriesService interface {
|
||||
All(req request.ArticleCategoriesQueryRequest) (articleCategories []*response.ArticleCategoriesResponse, paging paginator.Pagination, err error)
|
||||
All(req request.ArticleCategoriesQueryRequest, authToken string) (articleCategories []*response.ArticleCategoriesResponse, paging paginator.Pagination, err error)
|
||||
Show(id uint) (articleCategories *response.ArticleCategoriesResponse, err error)
|
||||
ShowByOldId(id uint) (articleCategories *response.ArticleCategoriesResponse, err error)
|
||||
Save(req request.ArticleCategoriesCreateRequest, authToken string) (articleCategories *entity.ArticleCategories, err error)
|
||||
|
|
@ -59,14 +59,20 @@ func NewArticleCategoriesService(repo repository.ArticleCategoriesRepository, us
|
|||
}
|
||||
|
||||
// All implement interface of ArticleCategoriesService
|
||||
func (_i *articleCategoriesService) All(req request.ArticleCategoriesQueryRequest) (articleCategoriess []*response.ArticleCategoriesResponse, paging paginator.Pagination, err error) {
|
||||
func (_i *articleCategoriesService) All(req request.ArticleCategoriesQueryRequest, authToken string) (articleCategoriess []*response.ArticleCategoriesResponse, paging paginator.Pagination, err error) {
|
||||
createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
|
||||
if createdBy != nil {
|
||||
if createdBy.UserLevel.LevelNumber > 1 {
|
||||
req.UserLevelId = &createdBy.UserLevelId
|
||||
}
|
||||
}
|
||||
|
||||
results, paging, err := _i.Repo.GetAll(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
host := _i.Cfg.App.Domain
|
||||
|
||||
for _, result := range results {
|
||||
articleCategoriess = append(articleCategoriess, mapper.ArticleCategoriesResponseMapper(result, host))
|
||||
}
|
||||
|
|
@ -96,8 +102,16 @@ func (_i *articleCategoriesService) Save(req request.ArticleCategoriesCreateRequ
|
|||
_i.Log.Info().Interface("data", req).Msg("")
|
||||
newReq := req.ToEntity()
|
||||
|
||||
createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
|
||||
newReq.CreatedById = &createdBy.ID
|
||||
if req.CreatedById != nil {
|
||||
createdBy, err := _i.UsersRepo.FindOne(*req.CreatedById)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newReq.CreatedById = &createdBy.ID
|
||||
} else {
|
||||
createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
|
||||
newReq.CreatedById = &createdBy.ID
|
||||
}
|
||||
|
||||
return _i.Repo.Create(newReq)
|
||||
}
|
||||
|
|
@ -170,7 +184,17 @@ func (_i *articleCategoriesService) SaveThumbnail(c *fiber.Ctx) (err error) {
|
|||
|
||||
func (_i *articleCategoriesService) Update(id uint, req request.ArticleCategoriesUpdateRequest) (err error) {
|
||||
_i.Log.Info().Interface("data", req).Msg("")
|
||||
return _i.Repo.Update(id, req.ToEntity())
|
||||
|
||||
newReq := req.ToEntity()
|
||||
if req.CreatedById != nil {
|
||||
createdBy, err := _i.UsersRepo.FindOne(*req.CreatedById)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newReq.CreatedById = &createdBy.ID
|
||||
}
|
||||
|
||||
return _i.Repo.Update(id, newReq)
|
||||
}
|
||||
|
||||
func (_i *articleCategoriesService) Delete(id uint) error {
|
||||
|
|
|
|||
|
|
@ -654,6 +654,24 @@ const docTemplate = `{
|
|||
],
|
||||
"summary": "Get all ArticleCategories",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"default": "Bearer \u003cAdd access token here\u003e",
|
||||
"description": "Insert your access token",
|
||||
"name": "Authorization",
|
||||
"in": "header",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"name": "UserLevelId",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"name": "UserLevelNumber",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"name": "description",
|
||||
|
|
@ -7938,6 +7956,9 @@ const docTemplate = `{
|
|||
"title"
|
||||
],
|
||||
"properties": {
|
||||
"createdById": {
|
||||
"type": "integer"
|
||||
},
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
|
|
@ -7970,6 +7991,9 @@ const docTemplate = `{
|
|||
"title"
|
||||
],
|
||||
"properties": {
|
||||
"createdById": {
|
||||
"type": "integer"
|
||||
},
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -643,6 +643,24 @@
|
|||
],
|
||||
"summary": "Get all ArticleCategories",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"default": "Bearer \u003cAdd access token here\u003e",
|
||||
"description": "Insert your access token",
|
||||
"name": "Authorization",
|
||||
"in": "header",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"name": "UserLevelId",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"name": "UserLevelNumber",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"name": "description",
|
||||
|
|
@ -7927,6 +7945,9 @@
|
|||
"title"
|
||||
],
|
||||
"properties": {
|
||||
"createdById": {
|
||||
"type": "integer"
|
||||
},
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
|
|
@ -7959,6 +7980,9 @@
|
|||
"title"
|
||||
],
|
||||
"properties": {
|
||||
"createdById": {
|
||||
"type": "integer"
|
||||
},
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -80,6 +80,8 @@ definitions:
|
|||
type: object
|
||||
request.ArticleCategoriesCreateRequest:
|
||||
properties:
|
||||
createdById:
|
||||
type: integer
|
||||
description:
|
||||
type: string
|
||||
oldCategoryId:
|
||||
|
|
@ -101,6 +103,8 @@ definitions:
|
|||
type: object
|
||||
request.ArticleCategoriesUpdateRequest:
|
||||
properties:
|
||||
createdById:
|
||||
type: integer
|
||||
description:
|
||||
type: string
|
||||
id:
|
||||
|
|
@ -1221,6 +1225,18 @@ paths:
|
|||
get:
|
||||
description: API for getting all ArticleCategories
|
||||
parameters:
|
||||
- default: Bearer <Add access token here>
|
||||
description: Insert your access token
|
||||
in: header
|
||||
name: Authorization
|
||||
required: true
|
||||
type: string
|
||||
- in: query
|
||||
name: UserLevelId
|
||||
type: integer
|
||||
- in: query
|
||||
name: UserLevelNumber
|
||||
type: integer
|
||||
- in: query
|
||||
name: description
|
||||
type: string
|
||||
|
|
|
|||
Loading…
Reference in New Issue