From 6358c16412602997d5d5a153da8c4bfca62baf92 Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Fri, 7 Feb 2025 04:14:25 +0700 Subject: [PATCH] feat: update user levels, articles, and magazine files --- app/database/entity/articles.entity.go | 2 ++ app/database/entity/user_levels.entity.go | 2 +- .../controller/articles.controller.go | 1 + .../repository/articles.repository.go | 3 +++ .../articles/request/articles.request.go | 18 +++++++++++++++ .../articles/service/articles.service.go | 20 ++++++++++++++++ .../mapper/magazine_files.mapper.go | 9 ++++++-- .../service/magazine_files.service.go | 14 ++++++----- .../magazines/mapper/magazines.mapper.go | 2 +- .../request/user_levels.request.go | 4 ++-- .../response/user_levels.response.go | 2 +- config/toml/config.toml | 8 +++---- docs/swagger/docs.go | 23 +++++++++++++++---- docs/swagger/swagger.json | 23 +++++++++++++++---- docs/swagger/swagger.yaml | 13 +++++++++-- 15 files changed, 117 insertions(+), 27 deletions(-) diff --git a/app/database/entity/articles.entity.go b/app/database/entity/articles.entity.go index 67705b8..d356175 100644 --- a/app/database/entity/articles.entity.go +++ b/app/database/entity/articles.entity.go @@ -23,6 +23,8 @@ type Articles struct { OldId *uint `json:"old_id" gorm:"type:int4"` IsPublish *bool `json:"is_publish" gorm:"type:bool;default:false"` PublishedAt *time.Time `json:"published_at" gorm:"type:timestamp"` + IsDraft *bool `json:"is_draft" gorm:"type:bool;default:false"` + DraftedAt *time.Time `json:"drafted_at" gorm:"type:timestamp"` 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/database/entity/user_levels.entity.go b/app/database/entity/user_levels.entity.go index 5fcb844..f8476b6 100644 --- a/app/database/entity/user_levels.entity.go +++ b/app/database/entity/user_levels.entity.go @@ -7,7 +7,7 @@ type UserLevels struct { Name string `json:"name" gorm:"type:varchar"` AliasName string `json:"alias_name" gorm:"type:varchar"` LevelNumber int `json:"level_number" gorm:"type:int4"` - ParentLevelId int `json:"parent_level_id" gorm:"type:int4"` + ParentLevelId *int `json:"parent_level_id" gorm:"type:int4"` ProvinceId *int `json:"province_id" gorm:"type:int4"` Group *string `json:"group" gorm:"type:varchar"` IsActive *bool `json:"is_active" gorm:"type:bool"` diff --git a/app/module/articles/controller/articles.controller.go b/app/module/articles/controller/articles.controller.go index f630562..9832f0e 100644 --- a/app/module/articles/controller/articles.controller.go +++ b/app/module/articles/controller/articles.controller.go @@ -57,6 +57,7 @@ func (_i *articlesController) All(c *fiber.Ctx) error { TypeId: c.Query("typeId"), StatusId: c.Query("statusId"), IsPublish: c.Query("isPublish"), + IsDraft: c.Query("isDraft"), } req := reqContext.ToParamRequest() req.Pagination = paginate diff --git a/app/module/articles/repository/articles.repository.go b/app/module/articles/repository/articles.repository.go index b491608..d3cda6c 100644 --- a/app/module/articles/repository/articles.repository.go +++ b/app/module/articles/repository/articles.repository.go @@ -60,6 +60,9 @@ func (_i *articlesRepository) GetAll(req request.ArticlesQueryRequest) (articles if req.IsPublish != nil { query = query.Where("is_publish = ?", req.IsPublish) } + if req.IsDraft != nil { + query = query.Where("is_draft = ?", req.IsDraft) + } if req.StatusId != nil { query = query.Where("status_id = ?", req.StatusId) } diff --git a/app/module/articles/request/articles.request.go b/app/module/articles/request/articles.request.go index d39dc79..95f34f4 100644 --- a/app/module/articles/request/articles.request.go +++ b/app/module/articles/request/articles.request.go @@ -20,6 +20,7 @@ type ArticlesQueryRequest struct { CreatedById *int `json:"createdById"` StatusId *int `json:"statusId"` IsPublish *bool `json:"isPublish"` + IsDraft *bool `json:"isDraft"` Pagination *paginator.Pagination `json:"pagination"` } @@ -32,6 +33,8 @@ type ArticlesCreateRequest struct { TypeId int `json:"typeId" validate:"required"` Tags string `json:"tags" validate:"required"` AiArticleId *int `json:"aiArticleId"` + IsPublish *bool `json:"isPublish"` + IsDraft *bool `json:"isDraft"` OldId *uint `json:"oldId"` } @@ -44,6 +47,8 @@ func (req ArticlesCreateRequest) ToEntity() *entity.Articles { TypeId: req.TypeId, Tags: req.Tags, AiArticleId: req.AiArticleId, + IsPublish: req.IsPublish, + IsDraft: req.IsDraft, OldId: req.OldId, } } @@ -58,6 +63,8 @@ type ArticlesUpdateRequest struct { Tags string `json:"tags" validate:"required"` CreatedById *uint `json:"createdById"` AiArticleId *int `json:"aiArticleId"` + IsPublish *bool `json:"isPublish"` + IsDraft *bool `json:"isDraft"` StatusId *int `json:"statusId"` } @@ -72,6 +79,8 @@ func (req ArticlesUpdateRequest) ToEntity() *entity.Articles { Tags: req.Tags, StatusId: req.StatusId, AiArticleId: req.AiArticleId, + IsPublish: req.IsPublish, + IsDraft: req.IsDraft, UpdatedAt: time.Now(), } } else { @@ -85,6 +94,8 @@ func (req ArticlesUpdateRequest) ToEntity() *entity.Articles { StatusId: req.StatusId, CreatedById: req.CreatedById, AiArticleId: req.AiArticleId, + IsPublish: req.IsPublish, + IsDraft: req.IsDraft, UpdatedAt: time.Now(), } } @@ -98,6 +109,7 @@ type ArticlesQueryRequestContext struct { Tags string `json:"tags"` CreatedById string `json:"createdById"` IsPublish string `json:"isPublish"` + IsDraft string `json:"isDraft"` StatusId string `json:"statusId"` } @@ -131,6 +143,12 @@ func (req ArticlesQueryRequestContext) ToParamRequest() ArticlesQueryRequest { request.IsPublish = &isPublish } } + if isDraftStr := req.IsDraft; isDraftStr != "" { + isDraft, err := strconv.ParseBool(isDraftStr) + if err == nil { + request.IsDraft = &isDraft + } + } if statusIdStr := req.StatusId; statusIdStr != "" { statusId, err := strconv.Atoi(statusIdStr) if err == nil { diff --git a/app/module/articles/service/articles.service.go b/app/module/articles/service/articles.service.go index b45e22d..4a34189 100644 --- a/app/module/articles/service/articles.service.go +++ b/app/module/articles/service/articles.service.go @@ -116,6 +116,26 @@ func (_i *articlesService) Save(req request.ArticlesCreateRequest, authToken str createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken) newReq.CreatedById = &createdBy.ID + isDraft := true + if req.IsDraft == &isDraft { + draftedAt := time.Now() + newReq.IsDraft = &isDraft + newReq.DraftedAt = &draftedAt + isPublishFalse := false + newReq.IsPublish = &isPublishFalse + newReq.PublishedAt = nil + } + + isPublish := true + if req.IsPublish == &isPublish { + publishedAt := time.Now() + newReq.IsPublish = &isPublish + newReq.PublishedAt = &publishedAt + isDraftFalse := false + newReq.IsDraft = &isDraftFalse + newReq.DraftedAt = nil + } + saveArticleRes, err := _i.Repo.Create(newReq) if err != nil { return nil, err diff --git a/app/module/magazine_files/mapper/magazine_files.mapper.go b/app/module/magazine_files/mapper/magazine_files.mapper.go index 613b6f6..989b4c0 100644 --- a/app/module/magazine_files/mapper/magazine_files.mapper.go +++ b/app/module/magazine_files/mapper/magazine_files.mapper.go @@ -5,7 +5,12 @@ import ( res "go-humas-be/app/module/magazine_files/response" ) -func MagazineFilesResponseMapper(magazineFilesReq *entity.MagazineFiles) (magazineFilesRes *res.MagazineFilesResponse) { +func MagazineFilesResponseMapper(magazineFilesReq *entity.MagazineFiles, host string) (magazineFilesRes *res.MagazineFilesResponse) { + fileUrl := host + "/magazine-files/viewer/" + if magazineFilesReq.FileName != nil { + fileUrl += *magazineFilesReq.FileName + } + if magazineFilesReq != nil { magazineFilesRes = &res.MagazineFilesResponse{ ID: magazineFilesReq.ID, @@ -14,7 +19,7 @@ func MagazineFilesResponseMapper(magazineFilesReq *entity.MagazineFiles) (magazi MagazineId: magazineFilesReq.MagazineId, DownloadCount: magazineFilesReq.DownloadCount, FilePath: magazineFilesReq.FilePath, - FileUrl: magazineFilesReq.FileUrl, + FileUrl: &fileUrl, FileName: magazineFilesReq.FileName, FileAlt: magazineFilesReq.FileAlt, WidthPixel: magazineFilesReq.WidthPixel, diff --git a/app/module/magazine_files/service/magazine_files.service.go b/app/module/magazine_files/service/magazine_files.service.go index 280aeb7..60ea8e3 100644 --- a/app/module/magazine_files/service/magazine_files.service.go +++ b/app/module/magazine_files/service/magazine_files.service.go @@ -9,6 +9,7 @@ import ( "go-humas-be/app/module/magazine_files/repository" "go-humas-be/app/module/magazine_files/request" "go-humas-be/app/module/magazine_files/response" + config "go-humas-be/config/config" minioStorage "go-humas-be/config/config" "go-humas-be/utils/paginator" "io" @@ -25,6 +26,7 @@ import ( type magazineFilesService struct { Repo repository.MagazineFilesRepository Log zerolog.Logger + Cfg *config.Config MinioStorage *minioStorage.MinioStorage } @@ -39,11 +41,12 @@ type MagazineFilesService interface { } // NewMagazineFilesService init MagazineFilesService -func NewMagazineFilesService(repo repository.MagazineFilesRepository, log zerolog.Logger, minioStorage *minioStorage.MinioStorage) MagazineFilesService { +func NewMagazineFilesService(repo repository.MagazineFilesRepository, log zerolog.Logger, cfg *config.Config, minioStorage *minioStorage.MinioStorage) MagazineFilesService { return &magazineFilesService{ Repo: repo, Log: log, + Cfg: cfg, MinioStorage: minioStorage, } } @@ -54,11 +57,10 @@ func (_i *magazineFilesService) All(req request.MagazineFilesQueryRequest) (maga if err != nil { return } - + host := _i.Cfg.App.Domain for _, result := range results { - magazineFiless = append(magazineFiless, mapper.MagazineFilesResponseMapper(result)) + magazineFiless = append(magazineFiless, mapper.MagazineFilesResponseMapper(result, host)) } - return } @@ -67,8 +69,8 @@ func (_i *magazineFilesService) Show(id uint) (magazineFiles *response.MagazineF if err != nil { return nil, err } - - return mapper.MagazineFilesResponseMapper(result), nil + host := _i.Cfg.App.Domain + return mapper.MagazineFilesResponseMapper(result, host), nil } func (_i *magazineFilesService) Save(c *fiber.Ctx, id uint, title string, description string) (err error) { diff --git a/app/module/magazines/mapper/magazines.mapper.go b/app/module/magazines/mapper/magazines.mapper.go index 94d4305..e7a0e1f 100644 --- a/app/module/magazines/mapper/magazines.mapper.go +++ b/app/module/magazines/mapper/magazines.mapper.go @@ -13,7 +13,7 @@ func MagazinesResponseMapper(magazinesReq *entity.Magazines, magazineFilesRepo m var magazineFilesArr []*magazineFilesResponse.MagazineFilesResponse if magazineFiles != nil && len(magazineFiles) > 0 { for _, result := range magazineFiles { - magazineFilesArr = append(magazineFilesArr, magazineFilesMapper.MagazineFilesResponseMapper(result)) + magazineFilesArr = append(magazineFilesArr, magazineFilesMapper.MagazineFilesResponseMapper(result, host)) } } diff --git a/app/module/user_levels/request/user_levels.request.go b/app/module/user_levels/request/user_levels.request.go index 74e5e42..204e297 100644 --- a/app/module/user_levels/request/user_levels.request.go +++ b/app/module/user_levels/request/user_levels.request.go @@ -23,7 +23,7 @@ type UserLevelsCreateRequest struct { Name string `json:"name" validate:"required"` AliasName string `json:"aliasName" validate:"required"` LevelNumber int `json:"levelNumber" validate:"required"` - ParentLevelId int `json:"parentLevelId" validate:"required"` + ParentLevelId *int `json:"parentLevelId"` ProvinceId *int `json:"provinceId"` IsActive *bool `json:"isActive"` } @@ -43,7 +43,7 @@ type UserLevelsUpdateRequest struct { Name string `json:"name" validate:"required"` AliasName string `json:"aliasName" validate:"required"` LevelNumber int `json:"levelNumber" validate:"required"` - ParentLevelId int `json:"parentLevelId" validate:"required"` + ParentLevelId *int `json:"parentLevelId"` ProvinceId *int `json:"provinceId"` } diff --git a/app/module/user_levels/response/user_levels.response.go b/app/module/user_levels/response/user_levels.response.go index 0bb8540..da3164a 100644 --- a/app/module/user_levels/response/user_levels.response.go +++ b/app/module/user_levels/response/user_levels.response.go @@ -7,7 +7,7 @@ type UserLevelsResponse struct { Name string `json:"name"` AliasName string `json:"aliasName"` LevelNumber int `json:"levelNumber"` - ParentLevelId int `json:"parentLevelId"` + ParentLevelId *int `json:"parentLevelId"` ProvinceId *int `json:"provinceId"` IsActive *bool `json:"isActive"` CreatedAt time.Time `json:"createdAt"` diff --git a/config/toml/config.toml b/config/toml/config.toml index 8676e77..fce2115 100644 --- a/config/toml/config.toml +++ b/config/toml/config.toml @@ -63,7 +63,7 @@ admin-password = "P@ssw0rd.1" [smtp] host = "smtp-app.polri.go.id" port = 465 -username = "multipool.divhumas@polri.go.id" -password = "wkjs0XzoCQ0axsYW" -from-address = "multipool.divhumas@polri.go.id" -from-name = "APLIKASI MULTIPOOL DIVHUMAS POLRI" \ No newline at end of file +username = "webhumas.divhumas@polri.go.id" +password = "z0VfYLbaghPc" +from-address = "webhumas.divhumas@polri.go.id" +from-name = "APLIKASI WEB HUMAS DIVHUMAS POLRI" \ No newline at end of file diff --git a/docs/swagger/docs.go b/docs/swagger/docs.go index 8e51684..93413d8 100644 --- a/docs/swagger/docs.go +++ b/docs/swagger/docs.go @@ -1819,6 +1819,11 @@ const docTemplate = `{ "name": "description", "in": "query" }, + { + "type": "boolean", + "name": "isDraft", + "in": "query" + }, { "type": "boolean", "name": "isPublish", @@ -7157,6 +7162,12 @@ const docTemplate = `{ "htmlDescription": { "type": "string" }, + "isDraft": { + "type": "boolean" + }, + "isPublish": { + "type": "boolean" + }, "oldId": { "type": "integer" }, @@ -7201,6 +7212,12 @@ const docTemplate = `{ "htmlDescription": { "type": "string" }, + "isDraft": { + "type": "boolean" + }, + "isPublish": { + "type": "boolean" + }, "slug": { "type": "string" }, @@ -7477,8 +7494,7 @@ const docTemplate = `{ "required": [ "aliasName", "levelNumber", - "name", - "parentLevelId" + "name" ], "properties": { "aliasName": { @@ -7506,8 +7522,7 @@ const docTemplate = `{ "required": [ "aliasName", "levelNumber", - "name", - "parentLevelId" + "name" ], "properties": { "aliasName": { diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index aff0e3d..a27778c 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -1808,6 +1808,11 @@ "name": "description", "in": "query" }, + { + "type": "boolean", + "name": "isDraft", + "in": "query" + }, { "type": "boolean", "name": "isPublish", @@ -7146,6 +7151,12 @@ "htmlDescription": { "type": "string" }, + "isDraft": { + "type": "boolean" + }, + "isPublish": { + "type": "boolean" + }, "oldId": { "type": "integer" }, @@ -7190,6 +7201,12 @@ "htmlDescription": { "type": "string" }, + "isDraft": { + "type": "boolean" + }, + "isPublish": { + "type": "boolean" + }, "slug": { "type": "string" }, @@ -7466,8 +7483,7 @@ "required": [ "aliasName", "levelNumber", - "name", - "parentLevelId" + "name" ], "properties": { "aliasName": { @@ -7495,8 +7511,7 @@ "required": [ "aliasName", "levelNumber", - "name", - "parentLevelId" + "name" ], "properties": { "aliasName": { diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index e3a8c10..29b34e6 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -196,6 +196,10 @@ definitions: type: string htmlDescription: type: string + isDraft: + type: boolean + isPublish: + type: boolean oldId: type: integer slug: @@ -227,6 +231,10 @@ definitions: type: string htmlDescription: type: string + isDraft: + type: boolean + isPublish: + type: boolean slug: type: string statusId: @@ -438,7 +446,6 @@ definitions: - aliasName - levelNumber - name - - parentLevelId type: object request.UserLevelsUpdateRequest: properties: @@ -456,7 +463,6 @@ definitions: - aliasName - levelNumber - name - - parentLevelId type: object request.UserLogin: properties: @@ -1876,6 +1882,9 @@ paths: - in: query name: description type: string + - in: query + name: isDraft + type: boolean - in: query name: isPublish type: boolean