diff --git a/app/database/entity/ppid_datas.entity.go b/app/database/entity/ppid_datas.entity.go index d1a42c4..ba56bf4 100644 --- a/app/database/entity/ppid_datas.entity.go +++ b/app/database/entity/ppid_datas.entity.go @@ -6,6 +6,7 @@ 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 *int `json:"level_group_id" gorm:"type:int4"` diff --git a/app/module/ppid_datas/controller/ppid_datas.controller.go b/app/module/ppid_datas/controller/ppid_datas.controller.go index 0ee5ab3..31e7198 100644 --- a/app/module/ppid_datas/controller/ppid_datas.controller.go +++ b/app/module/ppid_datas/controller/ppid_datas.controller.go @@ -77,19 +77,15 @@ func (_i *ppidDatasController) All(c *fiber.Ctx) error { // @Description API for getting one PpidDatas // @Tags PPID Data // @Security Bearer -// @Param id path int true "PpidDatas ID" +// @Param id path string true "PpidDatas [ ID / Slug ]" // @Success 200 {object} response.Response // @Failure 400 {object} response.BadRequestError // @Failure 401 {object} response.UnauthorizedError // @Failure 500 {object} response.InternalServerError // @Router /ppid-datas/{id} [get] func (_i *ppidDatasController) Show(c *fiber.Ctx) error { - id, err := strconv.ParseUint(c.Params("id"), 10, 0) - if err != nil { - return err - } - - ppidDatasData, err := _i.ppidDatasService.Show(uint(id)) + id := c.Params("id") + ppidDatasData, err := _i.ppidDatasService.Show(id) if err != nil { return err } diff --git a/app/module/ppid_datas/mapper/ppid_datas.mapper.go b/app/module/ppid_datas/mapper/ppid_datas.mapper.go index 92a9edf..d598c13 100644 --- a/app/module/ppid_datas/mapper/ppid_datas.mapper.go +++ b/app/module/ppid_datas/mapper/ppid_datas.mapper.go @@ -48,6 +48,7 @@ func PpidDatasResponseMapper( ID: ppidDatasReq.ID, Title: ppidDatasReq.Title, Description: ppidDatasReq.Description, + Slug: ppidDatasReq.Slug, CategoryId: ppidDatasReq.CategoryId, CategoryName: &categoryName, CreatedById: ppidDatasReq.CreatedById, diff --git a/app/module/ppid_datas/repository/ppid_datas.repository.go b/app/module/ppid_datas/repository/ppid_datas.repository.go index 598ba1e..b23ed2c 100644 --- a/app/module/ppid_datas/repository/ppid_datas.repository.go +++ b/app/module/ppid_datas/repository/ppid_datas.repository.go @@ -20,6 +20,7 @@ type ppidDatasRepository struct { type PpidDatasRepository interface { GetAll(req request.PpidDatasQueryRequest) (ppidDatass []*entity.PpidDatas, paging paginator.Pagination, err error) FindOne(id uint) (ppidDatas *entity.PpidDatas, err error) + FindOneUsingSlug(slug string) (ppidDatas *entity.PpidDatas, err error) Create(ppidDatas *entity.PpidDatas) (ppidDataReturn *entity.PpidDatas, err error) Update(id uint, ppidDatas *entity.PpidDatas) (err error) Delete(id uint) (err error) @@ -94,6 +95,14 @@ func (_i *ppidDatasRepository) FindOne(id uint) (ppidDatas *entity.PpidDatas, er return ppidDatas, nil } +func (_i *ppidDatasRepository) FindOneUsingSlug(slug string) (ppidDatas *entity.PpidDatas, err error) { + if err := _i.DB.DB.Where("slug = ?", slug).First(&ppidDatas).Error; err != nil { + return nil, err + } + + return ppidDatas, nil +} + func (_i *ppidDatasRepository) Create(ppidDatas *entity.PpidDatas) (ppidDataReturn *entity.PpidDatas, err error) { result := _i.DB.DB.Create(ppidDatas) diff --git a/app/module/ppid_datas/request/ppid_datas.request.go b/app/module/ppid_datas/request/ppid_datas.request.go index 705246e..b4b3102 100644 --- a/app/module/ppid_datas/request/ppid_datas.request.go +++ b/app/module/ppid_datas/request/ppid_datas.request.go @@ -26,6 +26,7 @@ type PpidDatasQueryRequest struct { type PpidDatasCreateRequest struct { Title string `json:"title" validate:"required"` Description string `json:"description" validate:"required"` + Slug string `json:"slug" validate:"required"` CategoryId uint `json:"categoryId" validate:"required"` StatusId int `json:"statusId" validate:"required"` CreatedById *uint `json:"createdById"` @@ -37,6 +38,7 @@ func (req PpidDatasCreateRequest) ToEntity() *entity.PpidDatas { return &entity.PpidDatas{ Title: req.Title, Description: req.Description, + Slug: req.Slug, CategoryId: req.CategoryId, CreatedById: req.CreatedById, StatusId: req.StatusId, @@ -49,6 +51,7 @@ type PpidDatasUpdateRequest struct { ID uint `json:"id" validate:"required"` Title string `json:"title" validate:"required"` Description string `json:"description" validate:"required"` + Slug string `json:"slug" validate:"required"` CategoryId uint `json:"category_id" validate:"required"` StatusId int `json:"status_id" validate:"required"` CreatedById *uint `json:"created_by_id"` @@ -63,6 +66,7 @@ func (req PpidDatasUpdateRequest) ToEntity() *entity.PpidDatas { ID: req.ID, Title: req.Title, Description: req.Description, + Slug: req.Slug, CategoryId: req.CategoryId, CreatedById: req.CreatedById, StatusId: req.StatusId, diff --git a/app/module/ppid_datas/response/ppid_datas.response.go b/app/module/ppid_datas/response/ppid_datas.response.go index bf876b0..66c27c9 100644 --- a/app/module/ppid_datas/response/ppid_datas.response.go +++ b/app/module/ppid_datas/response/ppid_datas.response.go @@ -9,6 +9,7 @@ type PpidDatasResponse struct { ID uint `json:"id"` Title string `json:"title"` Description string `json:"description"` + Slug string `json:"slug"` CategoryId uint `json:"categoryId"` CategoryName *string `json:"categoryName"` CreatedById *uint `json:"createdById"` diff --git a/app/module/ppid_datas/service/ppid_datas.service.go b/app/module/ppid_datas/service/ppid_datas.service.go index 0d76978..927d5c7 100644 --- a/app/module/ppid_datas/service/ppid_datas.service.go +++ b/app/module/ppid_datas/service/ppid_datas.service.go @@ -13,6 +13,7 @@ import ( usersRepository "go-humas-be/app/module/users/repository" "go-humas-be/utils/paginator" utilSvc "go-humas-be/utils/service" + "strconv" ) // PpidDatasService @@ -28,7 +29,7 @@ type ppidDatasService struct { // PpidDatasService define interface of IPpidDatasService type PpidDatasService interface { All(req request.PpidDatasQueryRequest) (ppidDatas []*response.PpidDatasResponse, paging paginator.Pagination, err error) - Show(id uint) (ppidDatas *response.PpidDatasResponse, err error) + Show(id string) (ppidDatas *response.PpidDatasResponse, err error) Save(req request.PpidDatasCreateRequest, authToken string) (ppidDatas *entity.PpidDatas, err error) Update(id uint, req request.PpidDatasUpdateRequest) (err error) Delete(id uint) error @@ -68,10 +69,21 @@ func (_i *ppidDatasService) All(req request.PpidDatasQueryRequest) (ppidDatass [ return } -func (_i *ppidDatasService) Show(id uint) (ppidDatas *response.PpidDatasResponse, err error) { - result, err := _i.Repo.FindOne(id) - if err != nil { - return nil, err +func (_i *ppidDatasService) Show(id string) (ppidDatas *response.PpidDatasResponse, err error) { + + var result *entity.PpidDatas + + if utilSvc.IsNumeric(id) { + idInt, _ := strconv.Atoi(id) + result, err = _i.Repo.FindOne(uint(idInt)) + if err != nil { + return nil, err + } + } else { + result, err = _i.Repo.FindOneUsingSlug(id) + if err != nil { + return nil, err + } } return mapper.PpidDatasResponseMapper(_i.Log, _i.PpidDataCategoriesRepo, _i.PpidDataFilesRepo, _i.UsersRepo, result), nil diff --git a/config/toml/config.toml b/config/toml/config.toml index 45ce2f3..cbe86be 100644 --- a/config/toml/config.toml +++ b/config/toml/config.toml @@ -9,7 +9,7 @@ production = false [db.postgres] dsn = "postgresql://humas_polri:P@ssw0rd.1@103.82.242.92:5432/humas_polri" # ://:@:/ -migrate = false +migrate = true seed = false [logger] diff --git a/docs/swagger/docs.go b/docs/swagger/docs.go index 6b28669..2f35dca 100644 --- a/docs/swagger/docs.go +++ b/docs/swagger/docs.go @@ -4011,8 +4011,8 @@ const docTemplate = `{ "summary": "Get one PpidDatas", "parameters": [ { - "type": "integer", - "description": "PpidDatas ID", + "type": "string", + "description": "PpidDatas [ ID / Slug ]", "name": "id", "in": "path", "required": true @@ -6046,6 +6046,7 @@ const docTemplate = `{ "required": [ "categoryId", "description", + "slug", "statusId", "title" ], @@ -6065,6 +6066,9 @@ const docTemplate = `{ "levelGroupId": { "type": "integer" }, + "slug": { + "type": "string" + }, "statusId": { "type": "integer" }, diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index 5e79acb..d7292d5 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -4000,8 +4000,8 @@ "summary": "Get one PpidDatas", "parameters": [ { - "type": "integer", - "description": "PpidDatas ID", + "type": "string", + "description": "PpidDatas [ ID / Slug ]", "name": "id", "in": "path", "required": true @@ -6035,6 +6035,7 @@ "required": [ "categoryId", "description", + "slug", "statusId", "title" ], @@ -6054,6 +6055,9 @@ "levelGroupId": { "type": "integer" }, + "slug": { + "type": "string" + }, "statusId": { "type": "integer" }, diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index 30dc031..58f8065 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -309,6 +309,8 @@ definitions: type: string levelGroupId: type: integer + slug: + type: string statusId: type: integer title: @@ -316,6 +318,7 @@ definitions: required: - categoryId - description + - slug - statusId - title type: object @@ -3144,11 +3147,11 @@ paths: get: description: API for getting one PpidDatas parameters: - - description: PpidDatas ID + - description: PpidDatas [ ID / Slug ] in: path name: id required: true - type: integer + type: string responses: "200": description: OK diff --git a/utils/service/string.service.go b/utils/service/string.service.go new file mode 100644 index 0000000..74ec275 --- /dev/null +++ b/utils/service/string.service.go @@ -0,0 +1,12 @@ +package service + +import "unicode" + +func IsNumeric(str string) bool { + for _, char := range str { + if !unicode.IsDigit(char) { + return false + } + } + return true +}