Skip to content

Commit

Permalink
feat: implement Paper Update API (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored May 12, 2024
1 parent eb9f0c3 commit d4de519
Show file tree
Hide file tree
Showing 25 changed files with 1,121 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {

paperApi := api.NewPaperApi(paperUseCase)
router.POST("/api/papers/find", paperApi.HandleFind)
router.POST("/api/papers/update", paperApi.HandleUpdate)

err = router.Run(":8080")
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions docs/openapi/paths/_index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
$ref: ./chapters/update.yaml
/api/papers/find:
$ref: ./papers/find.yaml
/api/papers/update:
$ref: ./papers/update.yaml
35 changes: 35 additions & 0 deletions docs/openapi/paths/papers/update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
post:
tags:
- Papers
operationId: papers-update
summary: Update paper
requestBody:
content:
application/json:
schema:
$ref: ../../schemas/interface/papers/update/PaperUpdateRequest.yaml
responses:
"200":
description: OK - Returns updated paper
content:
application/json:
schema:
$ref: ../../schemas/interface/papers/update/PaperUpdateResponse.yaml
"400":
description: Bad Request - Invalid request
content:
application/json:
schema:
$ref: ../../schemas/interface/papers/update/PaperUpdateErrorResponse.yaml
"404":
description: Not Found - Paper not found or not authorized
content:
application/json:
schema:
$ref: ../../schemas/interface/papers/update/PaperUpdateErrorResponse.yaml
"500":
description: Internal Server Error - Server error
content:
application/json:
schema:
$ref: ../../schemas/interface/app/ApplicationErrorResponse.yaml
11 changes: 11 additions & 0 deletions docs/openapi/schemas/entity/paper/PaperError.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: object
description: Error Message for Paper object
properties:
id:
type: string
description: Error message for paper id
example: paper id is required, but got ''
content:
type: string
description: Error message for paper content
example: paper content must be less than or equal to 40000 bytes
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type: object
description: Paper object
description: Paper object without auto-generated fields
properties:
content:
type: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type: object
description: Error Message for Paper object
description: Error Message for PaperWithoutAutofield object
properties:
content:
type: string
description: Error message for paper content
example: paper content must be less than or equal to 40000 bytes
example: paper content must be less than or equal to 40000 bytes, but got 43210 bytes
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type: object
description: Request Body for Paper Find API
description: Error Response Body for Paper Find API
properties:
message:
type: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type: object
description: Error Response Body for Paper Update API
properties:
message:
type: string
description: Error message when request body format is invalid
example: unexpected EOF
user:
$ref: ../../../entity/user/UserOnlyIdError.yaml
project:
$ref: ../../../entity/project/ProjectOnlyIdError.yaml
paper:
$ref: ../../../entity/paper/PaperError.yaml
required:
- user
- project
- paper
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type: object
description: Request Body for Paper Update API
properties:
user:
$ref: ../../../entity/user/UserOnlyId.yaml
project:
$ref: ../../../entity/project/ProjectOnlyId.yaml
paper:
$ref: ../../../entity/paper/Paper.yaml
required:
- user
- project
- paper
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: object
description: Response Body for Paper Update API
properties:
paper:
$ref: ../../../entity/paper/Paper.yaml
required:
- paper
40 changes: 40 additions & 0 deletions internal/api/paper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type PaperApi interface {
HandleFind(c *gin.Context)
HandleUpdate(c *gin.Context)
}

type paperApi struct {
Expand Down Expand Up @@ -58,3 +59,42 @@ func (api paperApi) HandleFind(c *gin.Context) {

c.JSON(http.StatusOK, res)
}

func (api paperApi) HandleUpdate(c *gin.Context) {
var request model.PaperUpdateRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, model.PaperUpdateErrorResponse{
Message: JsonBindErrorToMessage(err),
})
return
}

res, ucErr := api.usecase.UpdatePaper(request)

if ucErr != nil && ucErr.Code() == usecase.DomainValidationError {
resErr := UseCaseErrorToResponse(ucErr)
c.JSON(http.StatusBadRequest, model.PaperUpdateErrorResponse{
Message: UseCaseErrorToMessage(ucErr),
User: resErr.User,
Project: resErr.Project,
Paper: resErr.Paper,
})
return
}

if ucErr != nil && ucErr.Code() == usecase.NotFoundError {
c.JSON(http.StatusNotFound, model.PaperUpdateErrorResponse{
Message: UseCaseErrorToMessage(ucErr),
})
return
}

if ucErr != nil {
c.JSON(http.StatusInternalServerError, model.ApplicationErrorResponse{
Message: UseCaseErrorToMessage(ucErr),
})
return
}

c.JSON(http.StatusOK, res)
}
Loading

0 comments on commit d4de519

Please sign in to comment.