From bfbea4a661a9f5990dccddbec6846a4b02742705 Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Mon, 9 Sep 2024 00:42:09 +0700 Subject: [PATCH] feat: update ppid data --- app/database/entity/ppid_datas.entity.go | 45 ++++----- .../controller/ppid_datas.controller.go | 20 ++-- .../ppid_datas/mapper/ppid_datas.mapper.go | 91 +++++++++++-------- .../repository/ppid_datas.repository.go | 16 +++- .../ppid_datas/request/ppid_datas.request.go | 27 ++++-- docs/swagger/docs.go | 3 +- docs/swagger/swagger.json | 3 +- docs/swagger/swagger.yaml | 1 - 8 files changed, 118 insertions(+), 88 deletions(-) diff --git a/app/database/entity/ppid_datas.entity.go b/app/database/entity/ppid_datas.entity.go index ed9858b..bcf8019 100644 --- a/app/database/entity/ppid_datas.entity.go +++ b/app/database/entity/ppid_datas.entity.go @@ -3,25 +3,28 @@ package entity import "time" type PpidDatas struct { - ID uint `json:"id" gorm:"primaryKey;type:int4;autoIncrement"` - Title string `json:"title" gorm:"type:varchar"` - Description string `json:"description" gorm:"type:varchar"` - Slug string `json:"slug" gorm:"type:varchar"` - CategoryId uint `json:"category_id" gorm:"type:int4"` - CreatedById *uint `json:"created_by_id" gorm:"type:int4"` - LevelGroupId *uint `json:"level_group_id" gorm:"type:int4"` - Group *string `json:"group" gorm:"type:varchar"` - Position *int `json:"position" gorm:"type:int4"` - NeedApprovalFromUserRole *string `json:"need_approval_from_user_role" gorm:"type:varchar"` - NeedApprovalFromUserLevel *string `json:"need_approval_from_user_level" gorm:"type:varchar"` - BackApprovalToUserRole *string `json:"back_approval_to_user_role" gorm:"type:varchar"` - BackApprovalToUserLevel *string `json:"back_approval_to_user_level" gorm:"type:varchar"` - IsPublish *bool `json:"is_publish" gorm:"type:bool;default:false"` - PublishLevel *int `json:"publish_level" gorm:"type:int4"` - PublishedAt *time.Time `json:"published_at" gorm:"type:timestamp"` - ApprovalStatusId int `json:"approval_status_id" gorm:"type:int4"` - StatusId int `json:"status_id" gorm:"type:int4"` - IsActive *bool `json:"is_active" gorm:"type:bool;default:true"` - CreatedAt time.Time `json:"created_at" gorm:"default:now()"` - UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"` + ID uint `json:"id" gorm:"primaryKey;type:int4;autoIncrement"` + Title string `json:"title" gorm:"type:varchar"` + Description string `json:"description" gorm:"type:varchar"` + Slug string `json:"slug" gorm:"type:varchar"` + CategoryId uint `json:"category_id" gorm:"index"` + Category PpidDataCategories `json:"category" gorm:"foreignKey:CategoryId;references:ID"` + CreatedById *uint `json:"created_by_id" gorm:"index"` + CreatedBy Users `json:"created_by" gorm:"foreignKey:CreatedById;references:ID"` + LevelGroupId *uint `json:"level_group_id" gorm:"index"` + LevelGroup UserLevels `json:"level_group" gorm:"foreignKey:LevelGroupId;references:ID"` + Group *string `json:"group" gorm:"type:varchar"` + Position *int `json:"position" gorm:"type:int4"` + NeedApprovalFromUserRole *string `json:"need_approval_from_user_role" gorm:"type:varchar"` + NeedApprovalFromUserLevel *string `json:"need_approval_from_user_level" gorm:"type:varchar"` + BackApprovalToUserRole *string `json:"back_approval_to_user_role" gorm:"type:varchar"` + BackApprovalToUserLevel *string `json:"back_approval_to_user_level" gorm:"type:varchar"` + IsPublish *bool `json:"is_publish" gorm:"type:bool;default:false"` + PublishLevel *int `json:"publish_level" gorm:"type:int4"` + PublishedAt *time.Time `json:"published_at" gorm:"type:timestamp"` + ApprovalStatusId int `json:"approval_status_id" gorm:"type:int4"` + StatusId int `json:"status_id" gorm:"type:int4"` + IsActive *bool `json:"is_active" gorm:"type:bool;default:true"` + CreatedAt time.Time `json:"created_at" gorm:"default:now()"` + UpdatedAt time.Time `json:"updated_at" gorm:"default:now()"` } diff --git a/app/module/ppid_datas/controller/ppid_datas.controller.go b/app/module/ppid_datas/controller/ppid_datas.controller.go index 41d0902..5e05ca6 100644 --- a/app/module/ppid_datas/controller/ppid_datas.controller.go +++ b/app/module/ppid_datas/controller/ppid_datas.controller.go @@ -39,7 +39,7 @@ func NewPpidDatasController(ppidDatasService service.PpidDatasService) PpidDatas // @Description API for getting all PpidDatas // @Tags PPID Data // @Security Bearer -// @Param Authorization header string true "Insert your access token" default(Bearer ) +// @Param Authorization header string false "Insert your access token" default(Bearer ) // @Param req query request.PpidDatasQueryRequest false "query parameters" // @Param req query paginator.Pagination false "pagination parameters" // @Success 200 {object} response.Response @@ -56,14 +56,16 @@ func (_i *ppidDatasController) All(c *fiber.Ctx) error { authToken := c.Get("Authorization") reqContext := request.PpidDatasQueryRequestContext{ - Title: c.Query("title"), - Description: c.Query("description"), - CategoryId: c.Query("categoryId"), - UserId: c.Query("userId"), - UserRoleId: c.Query("userRoleId"), - UserLevelId: c.Query("userLevelId"), - StatusId: c.Query("statusId"), - IsPublish: c.Query("isPublish"), + Title: c.Query("title"), + Description: c.Query("description"), + CategoryId: c.Query("categoryId"), + UserId: c.Query("userId"), + UserRoleId: c.Query("userRoleId"), + UserLevelId: c.Query("userLevelId"), + Group: c.Query("group"), + LevelGroupId: c.Query("levelGroupId"), + StatusId: c.Query("statusId"), + IsPublish: c.Query("isPublish"), } req := reqContext.ToParamRequest() diff --git a/app/module/ppid_datas/mapper/ppid_datas.mapper.go b/app/module/ppid_datas/mapper/ppid_datas.mapper.go index 8390750..ee87c7e 100644 --- a/app/module/ppid_datas/mapper/ppid_datas.mapper.go +++ b/app/module/ppid_datas/mapper/ppid_datas.mapper.go @@ -9,6 +9,7 @@ import ( ppidDataFilesResponse "go-humas-be/app/module/ppid_data_files/response" res "go-humas-be/app/module/ppid_datas/response" usersRepository "go-humas-be/app/module/users/repository" + "time" ) func PpidDatasResponseMapper( @@ -18,51 +19,65 @@ func PpidDatasResponseMapper( usersRepo usersRepository.UsersRepository, ppidDatasReq *entity.PpidDatas, ) (ppidDatasRes *res.PpidDatasResponse) { - if ppidDatasReq != nil { - findCategory, _ := ppidDataCategoriesRepo.FindOne(ppidDatasReq.CategoryId) - categoryName := "" - if findCategory != nil { - categoryName = findCategory.Title - } + if ppidDatasReq == nil { + return nil + } - findUser, _ := usersRepo.FindOne(*ppidDatasReq.CreatedById) - createdByName := "" - if findUser != nil { - createdByName = findUser.Fullname - } + //log.Info().Str("timestamp", time.Now(). + // Format(time.RFC3339)).Str("Service:PpidDatasResponseMapper", "PpidData:Category"). + // Interface("Category", ppidDatasReq.Category).Msg("") - ppidDataFiles, _ := ppidDataFilesRepo.FindByPpidData(ppidDatasReq.ID) + log.Info().Str("timestamp", time.Now(). + Format(time.RFC3339)).Str("Service:PpidDatasResponseMapper", "PpidData:Category"). + Interface("CreatedBy", ppidDatasReq.CreatedBy).Msg("") - var ppidDataFilesArr []*ppidDataFilesResponse.PpidDataFilesResponse - if len(ppidDataFiles) > 0 { - for _, result := range ppidDataFiles { - ppidDataFilesArr = append(ppidDataFilesArr, ppidDataFilesMapper.PpidDataFilesResponseMapper(result)) - } - } + categoryName := "" + if ppidDatasReq.Category.ID != 0 { + categoryName = ppidDatasReq.Category.Title + } - //log.Info().Str("timestamp", time.Now(). - // Format(time.RFC3339)).Str("Service:PpidDatasResponseMapper", "UserInfo:PpidData"). - // Interface("payload", ppidDataFiles).Msg("") + findUser, err := usersRepo.FindOne(*ppidDatasReq.CreatedById) + if err != nil { + log.Error().Err(err).Msg("Error fetching user") + return nil + } + createdByName := "" + if findUser != nil { + createdByName = findUser.Fullname + } - ppidDatasRes = &res.PpidDatasResponse{ - ID: ppidDatasReq.ID, - Title: ppidDatasReq.Title, - Description: ppidDatasReq.Description, - Slug: ppidDatasReq.Slug, - CategoryId: ppidDatasReq.CategoryId, - CategoryName: &categoryName, - CreatedById: ppidDatasReq.CreatedById, - CreatedByName: &createdByName, - Position: ppidDatasReq.Position, - StatusId: ppidDatasReq.StatusId, - IsPublish: ppidDatasReq.IsPublish, - PublishedAt: ppidDatasReq.PublishedAt, - IsActive: ppidDatasReq.IsActive, - CreatedAt: ppidDatasReq.CreatedAt, - UpdatedAt: ppidDatasReq.UpdatedAt, + ppidDataFiles, err := ppidDataFilesRepo.FindByPpidData(ppidDatasReq.ID) + if err != nil { + log.Error().Err(err).Msg("Error fetching files") + return nil + } - PpidDataFiles: ppidDataFilesArr, + var ppidDataFilesArr []*ppidDataFilesResponse.PpidDataFilesResponse + if len(ppidDataFiles) > 0 { + for _, result := range ppidDataFiles { + ppidDataFilesArr = append(ppidDataFilesArr, ppidDataFilesMapper.PpidDataFilesResponseMapper(result)) } } + + ppidDatasRes = &res.PpidDatasResponse{ + ID: ppidDatasReq.ID, + Title: ppidDatasReq.Title, + Description: ppidDatasReq.Description, + Slug: ppidDatasReq.Slug, + CategoryId: ppidDatasReq.CategoryId, + CategoryName: &categoryName, + CreatedById: ppidDatasReq.CreatedById, + CreatedByName: &createdByName, + Position: ppidDatasReq.Position, + StatusId: ppidDatasReq.StatusId, + IsPublish: ppidDatasReq.IsPublish, + PublishedAt: ppidDatasReq.PublishedAt, + IsActive: ppidDatasReq.IsActive, + CreatedAt: ppidDatasReq.CreatedAt, + UpdatedAt: ppidDatasReq.UpdatedAt, + + PpidDataFiles: ppidDataFilesArr, + } + return ppidDatasRes } diff --git a/app/module/ppid_datas/repository/ppid_datas.repository.go b/app/module/ppid_datas/repository/ppid_datas.repository.go index d733d2c..f0f8e33 100644 --- a/app/module/ppid_datas/repository/ppid_datas.repository.go +++ b/app/module/ppid_datas/repository/ppid_datas.repository.go @@ -40,7 +40,7 @@ func NewPpidDatasRepository(db *database.Database, logger zerolog.Logger) PpidDa func (_i *ppidDatasRepository) GetAll(req request.PpidDatasQueryRequest) (ppidDatass []*entity.PpidDatas, paging paginator.Pagination, err error) { var count int64 - query := _i.DB.DB.Model(&entity.PpidDatas{}) + query := _i.DB.DB.Model(&entity.PpidDatas{}).Preload("CreatedBy").Preload("Category").Preload("LevelGroup") query = query.Where("is_active = ?", true) if req.Title != nil && *req.Title != "" { @@ -54,9 +54,11 @@ func (_i *ppidDatasRepository) GetAll(req request.PpidDatasQueryRequest) (ppidDa if req.CategoryId != nil { query = query.Where("category_id = ?", req.CategoryId) } - if req.Group != nil && *req.Group != "" { - title := strings.ToLower(*req.Title) - query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%") + if req.LevelGroupId != nil { + query = query.Where("level_group_id = ?", req.LevelGroupId) + } + if req.LevelGroupId != nil { + query = query.Where("level_group_id = ?", req.LevelGroupId) } if req.IsPublish != nil { query = query.Where("is_publish = ?", req.IsPublish) @@ -76,6 +78,10 @@ func (_i *ppidDatasRepository) GetAll(req request.PpidDatasQueryRequest) (ppidDa } query.Count(&count) + //_i.Log.Info().Str("timestamp", time.Now(). + // Format(time.RFC3339)).Str("ppidDatasRepository:GetAll", "PpidData:CreatedBy"). + // Interface("Query", query.ToSQL).Msg("") + if req.Pagination.SortBy != "" { direction := "ASC" if req.Pagination.Sort == "desc" { @@ -87,7 +93,7 @@ func (_i *ppidDatasRepository) GetAll(req request.PpidDatasQueryRequest) (ppidDa req.Pagination.Count = count req.Pagination = paginator.Paging(req.Pagination) - if req.Pagination.Limit == -1 { + if req.Pagination.Limit != -1 { query.Limit(req.Pagination.Limit) } diff --git a/app/module/ppid_datas/request/ppid_datas.request.go b/app/module/ppid_datas/request/ppid_datas.request.go index 6c31769..5d290fc 100644 --- a/app/module/ppid_datas/request/ppid_datas.request.go +++ b/app/module/ppid_datas/request/ppid_datas.request.go @@ -103,16 +103,17 @@ func (req PpidDatasUpdatePositionRequest) ToEntity() *entity.PpidDatas { } type PpidDatasQueryRequestContext struct { - Title string `json:"title"` - Description string `json:"description"` - CategoryId string `json:"categoryId"` - Group string `json:"group"` - CreatedById string `json:"createdById"` - UserId string `json:"userId"` - UserRoleId string `json:"userRoleId"` - UserLevelId string `json:"userLevelId"` - StatusId string `json:"statusId"` - IsPublish string `json:"isPublish"` + Title string `json:"title"` + Description string `json:"description"` + CategoryId string `json:"categoryId"` + LevelGroupId string `json:"levelGroupId"` + Group string `json:"group"` + CreatedById string `json:"createdById"` + UserId string `json:"userId"` + UserRoleId string `json:"userRoleId"` + UserLevelId string `json:"userLevelId"` + StatusId string `json:"statusId"` + IsPublish string `json:"isPublish"` } func (req PpidDatasQueryRequestContext) ToParamRequest() PpidDatasQueryRequest { @@ -124,6 +125,12 @@ func (req PpidDatasQueryRequestContext) ToParamRequest() PpidDatasQueryRequest { if description := req.Description; description != "" { request.Description = &description } + if levelGroupIdStr := req.LevelGroupId; levelGroupIdStr != "" { + levelGroupId, err := strconv.Atoi(levelGroupIdStr) + if err == nil { + request.LevelGroupId = &levelGroupId + } + } if group := req.Group; group != "" { request.Group = &group } diff --git a/docs/swagger/docs.go b/docs/swagger/docs.go index 840193e..3a94f3e 100644 --- a/docs/swagger/docs.go +++ b/docs/swagger/docs.go @@ -4610,8 +4610,7 @@ const docTemplate = `{ "default": "Bearer \u003cAdd access token here\u003e", "description": "Insert your access token", "name": "Authorization", - "in": "header", - "required": true + "in": "header" }, { "type": "integer", diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index 3b48e26..2a65cbb 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -4599,8 +4599,7 @@ "default": "Bearer \u003cAdd access token here\u003e", "description": "Insert your access token", "name": "Authorization", - "in": "header", - "required": true + "in": "header" }, { "type": "integer", diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index cb593c1..21fcdf9 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -3776,7 +3776,6 @@ paths: description: Insert your access token in: header name: Authorization - required: true type: string - in: query name: categoryId