Skip to content

Commit

Permalink
category update api
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza-fa committed Feb 19, 2024
1 parent 59cef3f commit 381ccd3
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 0 deletions.
81 changes: 81 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,74 @@ const docTemplate = `{
}
}
}
},
"patch": {
"security": [
{
"AuthBearer": []
}
],
"description": "Update a category. Only admins can do it",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Categories"
],
"summary": "Update category",
"parameters": [
{
"type": "integer",
"description": "id",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Category update",
"name": "Request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.CategoryUpdate"
}
}
],
"responses": {
"200": {
"description": "updated",
"schema": {
"allOf": [
{
"$ref": "#/definitions/helper.BaseHttpResponse"
},
{
"type": "object",
"properties": {
"result": {
"$ref": "#/definitions/dto.CategoryOutput"
}
}
}
]
}
},
"400": {
"description": "bad request",
"schema": {
"$ref": "#/definitions/helper.BaseHttpResponseWithValidationError"
}
},
"406": {
"description": "not acceptable",
"schema": {
"$ref": "#/definitions/helper.BaseHttpResponseWithError"
}
}
}
}
},
"/api/token/refresh/": {
Expand Down Expand Up @@ -421,6 +489,19 @@ const docTemplate = `{
}
}
},
"dto.CategoryUpdate": {
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string",
"maxLength": 64,
"minLength": 3
}
}
},
"dto.CreateUser": {
"type": "object",
"required": [
Expand Down
81 changes: 81 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,74 @@
}
}
}
},
"patch": {
"security": [
{
"AuthBearer": []
}
],
"description": "Update a category. Only admins can do it",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Categories"
],
"summary": "Update category",
"parameters": [
{
"type": "integer",
"description": "id",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Category update",
"name": "Request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.CategoryUpdate"
}
}
],
"responses": {
"200": {
"description": "updated",
"schema": {
"allOf": [
{
"$ref": "#/definitions/helper.BaseHttpResponse"
},
{
"type": "object",
"properties": {
"result": {
"$ref": "#/definitions/dto.CategoryOutput"
}
}
}
]
}
},
"400": {
"description": "bad request",
"schema": {
"$ref": "#/definitions/helper.BaseHttpResponseWithValidationError"
}
},
"406": {
"description": "not acceptable",
"schema": {
"$ref": "#/definitions/helper.BaseHttpResponseWithError"
}
}
}
}
},
"/api/token/refresh/": {
Expand Down Expand Up @@ -410,6 +478,19 @@
}
}
},
"dto.CategoryUpdate": {
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string",
"maxLength": 64,
"minLength": 3
}
}
},
"dto.CreateUser": {
"type": "object",
"required": [
Expand Down
50 changes: 50 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ definitions:
name:
type: string
type: object
dto.CategoryUpdate:
properties:
name:
maxLength: 64
minLength: 3
type: string
required:
- name
type: object
dto.CreateUser:
properties:
email:
Expand Down Expand Up @@ -140,6 +149,47 @@ info:
contact: {}
paths:
/api/categories/:
patch:
consumes:
- application/json
description: Update a category. Only admins can do it
parameters:
- description: id
in: path
name: id
required: true
type: integer
- description: Category update
in: body
name: Request
required: true
schema:
$ref: '#/definitions/dto.CategoryUpdate'
produces:
- application/json
responses:
"200":
description: updated
schema:
allOf:
- $ref: '#/definitions/helper.BaseHttpResponse'
- properties:
result:
$ref: '#/definitions/dto.CategoryOutput'
type: object
"400":
description: bad request
schema:
$ref: '#/definitions/helper.BaseHttpResponseWithValidationError'
"406":
description: not acceptable
schema:
$ref: '#/definitions/helper.BaseHttpResponseWithError'
security:
- AuthBearer: []
summary: Update category
tags:
- Categories
post:
consumes:
- application/json
Expand Down
43 changes: 43 additions & 0 deletions src/api/handlers/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/alireza-fa/blog-go/src/services"
"github.com/go-playground/validator/v10"
"net/http"
"strconv"
)

type CategoryHandler struct {
Expand Down Expand Up @@ -56,3 +57,45 @@ func (handler *CategoryHandler) Create(w http.ResponseWriter, r *http.Request) {

helper.BaseResponse(w, category, http.StatusCreated)
}

// Update godoc
// @Summary Update category
// @Description Update a category. Only admins can do it
// @Tags Categories
// @Accept json
// @Produce json
// @Param id path int true "id"
// @Param Request body dto.CategoryUpdate true "Category update"
// @Success 200 {object} helper.BaseHttpResponse{result=dto.CategoryOutput} "updated"
// @Failure 400 {object} helper.BaseHttpResponseWithValidationError "bad request"
// @Failure 406 {object} helper.BaseHttpResponseWithError "not acceptable"
// @Router /api/categories/ [patch]
// @Security AuthBearer
func (handler *CategoryHandler) Update(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
helper.BaseResponseWithError(w, nil, http.StatusNotAcceptable, err)
return
}

var categoryUpdate dto.CategoryUpdate
err = json.NewDecoder(r.Body).Decode(&categoryUpdate)
if err != nil {
helper.BaseResponseWithError(w, nil, http.StatusNotAcceptable, err)
return
}

err = handler.validate.Struct(&categoryUpdate)
if err != nil {
helper.BaseResponseWithValidationError(w, nil, http.StatusBadRequest, err)
return
}

category, err := handler.service.Update(&categoryUpdate, id)
if err != nil {
helper.BaseResponseWithError(w, nil, http.StatusNotAcceptable, err)
return
}

helper.BaseResponse(w, category, http.StatusOK)
}
1 change: 1 addition & 0 deletions src/api/routers/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ func CategoryRouter(router chi.Router) {
router.Use(middlewares.Authentication)

router.Post("/", middlewares.Authorization(handler.Create, []string{"admin"}))
router.Patch("/", middlewares.Authorization(handler.Update, []string{"admin"}))
}
1 change: 1 addition & 0 deletions src/pkg/logging/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
Update SubCategory = "Update"
Delete SubCategory = "Delete"
Rollback SubCategory = "Rollback"
Commit SubCategory = "Commit"

RedisSet SubCategory = "RedisSet"
RedisGet SubCategory = "RedisGet"
Expand Down
26 changes: 26 additions & 0 deletions src/services/category_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,29 @@ func (service *CategoryService) checkCategoryExists(name string) (bool, error) {
}
return exists, nil
}

func (service *CategoryService) Update(categoryUpdate *dto.CategoryUpdate, id int) (*dto.CategoryOutput, error) {
tx := service.database.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err := tx.
Model(models.Category{}).
Where("id = ?", id).
Update("name", categoryUpdate.Name).Error; err != nil {
tx.Rollback()
service.logger.Error(logging.Postgres, logging.Rollback, err.Error(), nil)
return nil, err
}

if err := tx.Commit().Error; err != nil {
service.logger.Error(logging.Postgres, logging.Commit, err.Error(), nil)
return nil, err
}

categoryOutput := dto.CategoryOutput{Id: 0, Name: categoryUpdate.Name}
return &categoryOutput, nil
}

0 comments on commit 381ccd3

Please sign in to comment.