feat: update article categories
This commit is contained in:
parent
c230d93d76
commit
a458aad9d0
|
|
@ -37,6 +37,7 @@ func NewArticleCategoriesController(articleCategoriesService service.ArticleCate
|
||||||
// @Description API for getting all ArticleCategories
|
// @Description API for getting all ArticleCategories
|
||||||
// @Tags Article Categories
|
// @Tags Article Categories
|
||||||
// @Security Bearer
|
// @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 request.ArticleCategoriesQueryRequest false "query parameters"
|
||||||
// @Param req query paginator.Pagination false "pagination parameters"
|
// @Param req query paginator.Pagination false "pagination parameters"
|
||||||
// @Success 200 {object} response.Response
|
// @Success 200 {object} response.Response
|
||||||
|
|
@ -50,6 +51,8 @@ func (_i *articleCategoriesController) All(c *fiber.Ctx) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
authToken := c.Get("Authorization")
|
||||||
|
|
||||||
reqContext := request.ArticleCategoriesQueryRequestContext{
|
reqContext := request.ArticleCategoriesQueryRequestContext{
|
||||||
Title: c.Query("title"),
|
Title: c.Query("title"),
|
||||||
Description: c.Query("description"),
|
Description: c.Query("description"),
|
||||||
|
|
@ -60,7 +63,7 @@ func (_i *articleCategoriesController) All(c *fiber.Ctx) error {
|
||||||
req := reqContext.ToParamRequest()
|
req := reqContext.ToParamRequest()
|
||||||
req.Pagination = paginate
|
req.Pagination = paginate
|
||||||
|
|
||||||
articleCategoriesData, paging, err := _i.articleCategoriesService.All(req)
|
articleCategoriesData, paging, err := _i.articleCategoriesService.All(req, authToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,24 +38,32 @@ func (_i *articleCategoriesRepository) GetAll(req request.ArticleCategoriesQuery
|
||||||
var count int64
|
var count int64
|
||||||
|
|
||||||
query := _i.DB.DB.Model(&entity.ArticleCategories{})
|
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 != "" {
|
if req.Title != nil && *req.Title != "" {
|
||||||
title := strings.ToLower(*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 != "" {
|
if req.Description != nil && *req.Description != "" {
|
||||||
description := strings.ToLower(*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 {
|
if req.ParentId != nil {
|
||||||
query = query.Where("parent_id = ?", req.ParentId)
|
query = query.Where("article_categories.parent_id = ?", req.ParentId)
|
||||||
}
|
}
|
||||||
if req.IsPublish != nil {
|
if req.IsPublish != nil {
|
||||||
query = query.Where("is_publish = ?", req.IsPublish)
|
query = query.Where("article_categories.is_publish = ?", req.IsPublish)
|
||||||
}
|
}
|
||||||
if req.StatusId != nil {
|
if req.StatusId != nil {
|
||||||
query = query.Where("status_id = ?", req.StatusId)
|
query = query.Where("article_categories.status_id = ?", req.StatusId)
|
||||||
}
|
}
|
||||||
query.Count(&count)
|
query.Count(&count)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,12 +12,14 @@ type ArticleCategoriesGeneric interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArticleCategoriesQueryRequest struct {
|
type ArticleCategoriesQueryRequest struct {
|
||||||
Title *string `json:"title"`
|
Title *string `json:"title"`
|
||||||
Description *string `json:"description"`
|
Description *string `json:"description"`
|
||||||
ParentId *int `json:"parentId"`
|
UserLevelId *uint `json:"UserLevelId"`
|
||||||
StatusId *int `json:"statusId"`
|
UserLevelNumber *int `json:"UserLevelNumber"`
|
||||||
IsPublish *bool `json:"isPublish"`
|
ParentId *int `json:"parentId"`
|
||||||
Pagination *paginator.Pagination `json:"pagination"`
|
StatusId *int `json:"statusId"`
|
||||||
|
IsPublish *bool `json:"isPublish"`
|
||||||
|
Pagination *paginator.Pagination `json:"pagination"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArticleCategoriesCreateRequest struct {
|
type ArticleCategoriesCreateRequest struct {
|
||||||
|
|
@ -26,6 +28,7 @@ type ArticleCategoriesCreateRequest struct {
|
||||||
StatusId int `json:"statusId" validate:"required"`
|
StatusId int `json:"statusId" validate:"required"`
|
||||||
Tags *string `json:"tags"`
|
Tags *string `json:"tags"`
|
||||||
Slug *string `json:"slug"`
|
Slug *string `json:"slug"`
|
||||||
|
CreatedById *uint `json:"createdById"`
|
||||||
ParentId *int `json:"parentId"`
|
ParentId *int `json:"parentId"`
|
||||||
OldCategoryId *uint `json:"oldCategoryId"`
|
OldCategoryId *uint `json:"oldCategoryId"`
|
||||||
}
|
}
|
||||||
|
|
@ -50,6 +53,7 @@ type ArticleCategoriesUpdateRequest struct {
|
||||||
Tags *string `json:"tags"`
|
Tags *string `json:"tags"`
|
||||||
Slug *string `json:"slug"`
|
Slug *string `json:"slug"`
|
||||||
ParentId *int `json:"parentId"`
|
ParentId *int `json:"parentId"`
|
||||||
|
CreatedById *uint `json:"createdById"`
|
||||||
IsPublish *bool `json:"isPublish"`
|
IsPublish *bool `json:"isPublish"`
|
||||||
PublishedAt *time.Time `json:"publishedAt"`
|
PublishedAt *time.Time `json:"publishedAt"`
|
||||||
}
|
}
|
||||||
|
|
@ -70,11 +74,13 @@ func (req ArticleCategoriesUpdateRequest) ToEntity() *entity.ArticleCategories {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArticleCategoriesQueryRequestContext struct {
|
type ArticleCategoriesQueryRequestContext struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
ParentId string `json:"parentId"`
|
ParentId string `json:"parentId"`
|
||||||
StatusId string `json:"statusId"`
|
StatusId string `json:"statusId"`
|
||||||
IsPublish string `json:"isPublish"`
|
IsPublish string `json:"isPublish"`
|
||||||
|
UserLevelId string `json:"UserLevelId"`
|
||||||
|
UserLevelNumber string `json:"UserLevelNumber"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (req ArticleCategoriesQueryRequestContext) ToParamRequest() ArticleCategoriesQueryRequest {
|
func (req ArticleCategoriesQueryRequestContext) ToParamRequest() ArticleCategoriesQueryRequest {
|
||||||
|
|
@ -104,6 +110,19 @@ func (req ArticleCategoriesQueryRequestContext) ToParamRequest() ArticleCategori
|
||||||
request.StatusId = &statusId
|
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
|
return request
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ type articleCategoriesService struct {
|
||||||
|
|
||||||
// ArticleCategoriesService define interface of IArticleCategoriesService
|
// ArticleCategoriesService define interface of IArticleCategoriesService
|
||||||
type ArticleCategoriesService interface {
|
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)
|
Show(id uint) (articleCategories *response.ArticleCategoriesResponse, err error)
|
||||||
ShowByOldId(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)
|
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
|
// 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)
|
results, paging, err := _i.Repo.GetAll(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
host := _i.Cfg.App.Domain
|
host := _i.Cfg.App.Domain
|
||||||
|
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
articleCategoriess = append(articleCategoriess, mapper.ArticleCategoriesResponseMapper(result, host))
|
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("")
|
_i.Log.Info().Interface("data", req).Msg("")
|
||||||
newReq := req.ToEntity()
|
newReq := req.ToEntity()
|
||||||
|
|
||||||
createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
|
if req.CreatedById != nil {
|
||||||
newReq.CreatedById = &createdBy.ID
|
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)
|
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) {
|
func (_i *articleCategoriesService) Update(id uint, req request.ArticleCategoriesUpdateRequest) (err error) {
|
||||||
_i.Log.Info().Interface("data", req).Msg("")
|
_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 {
|
func (_i *articleCategoriesService) Delete(id uint) error {
|
||||||
|
|
|
||||||
|
|
@ -654,6 +654,24 @@ const docTemplate = `{
|
||||||
],
|
],
|
||||||
"summary": "Get all ArticleCategories",
|
"summary": "Get all ArticleCategories",
|
||||||
"parameters": [
|
"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",
|
"type": "string",
|
||||||
"name": "description",
|
"name": "description",
|
||||||
|
|
@ -7938,6 +7956,9 @@ const docTemplate = `{
|
||||||
"title"
|
"title"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"createdById": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
"description": {
|
"description": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
|
@ -7970,6 +7991,9 @@ const docTemplate = `{
|
||||||
"title"
|
"title"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"createdById": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
"description": {
|
"description": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -643,6 +643,24 @@
|
||||||
],
|
],
|
||||||
"summary": "Get all ArticleCategories",
|
"summary": "Get all ArticleCategories",
|
||||||
"parameters": [
|
"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",
|
"type": "string",
|
||||||
"name": "description",
|
"name": "description",
|
||||||
|
|
@ -7927,6 +7945,9 @@
|
||||||
"title"
|
"title"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"createdById": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
"description": {
|
"description": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
|
@ -7959,6 +7980,9 @@
|
||||||
"title"
|
"title"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"createdById": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
"description": {
|
"description": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,8 @@ definitions:
|
||||||
type: object
|
type: object
|
||||||
request.ArticleCategoriesCreateRequest:
|
request.ArticleCategoriesCreateRequest:
|
||||||
properties:
|
properties:
|
||||||
|
createdById:
|
||||||
|
type: integer
|
||||||
description:
|
description:
|
||||||
type: string
|
type: string
|
||||||
oldCategoryId:
|
oldCategoryId:
|
||||||
|
|
@ -101,6 +103,8 @@ definitions:
|
||||||
type: object
|
type: object
|
||||||
request.ArticleCategoriesUpdateRequest:
|
request.ArticleCategoriesUpdateRequest:
|
||||||
properties:
|
properties:
|
||||||
|
createdById:
|
||||||
|
type: integer
|
||||||
description:
|
description:
|
||||||
type: string
|
type: string
|
||||||
id:
|
id:
|
||||||
|
|
@ -1221,6 +1225,18 @@ paths:
|
||||||
get:
|
get:
|
||||||
description: API for getting all ArticleCategories
|
description: API for getting all ArticleCategories
|
||||||
parameters:
|
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
|
- in: query
|
||||||
name: description
|
name: description
|
||||||
type: string
|
type: string
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue