Skip to content

Commit

Permalink
feat: implement Paper Find API (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored May 11, 2024
1 parent 4e4cb6e commit eb9f0c3
Show file tree
Hide file tree
Showing 27 changed files with 1,168 additions and 23 deletions.
4 changes: 4 additions & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func main() {

projectUseCase := usecase.NewProjectUseCase(projectService)
chapterUseCase := usecase.NewChapterUseCase(chapterService, paperService)
paperUseCase := usecase.NewPaperUseCase(paperService)

projectApi := api.NewProjectApi(projectUseCase)
router.POST("/api/projects/list", projectApi.HandleList)
Expand All @@ -76,6 +77,9 @@ func main() {
router.POST("/api/chapters/create", chapterApi.HandleCreate)
router.POST("/api/chapters/update", chapterApi.HandleUpdate)

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

err = router.Run(":8080")
if err != nil {
log.Fatalf("Failed to run gin server: %v", err)
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 @@ -12,3 +12,5 @@
$ref: ./chapters/create.yaml
/api/chapters/update:
$ref: ./chapters/update.yaml
/api/papers/find:
$ref: ./papers/find.yaml
2 changes: 1 addition & 1 deletion docs/openapi/paths/chapters/list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ post:
content:
application/json:
schema:
$ref: ../../schemas/interface/chapters/create/ChapterCreateErrorResponse.yaml
$ref: ../../schemas/interface/chapters/list/ChapterListErrorResponse.yaml
"500":
description: Internal Server Error - Server error
content:
Expand Down
35 changes: 35 additions & 0 deletions docs/openapi/paths/papers/find.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
post:
tags:
- Papers
operationId: papers-find
summary: Find paper
requestBody:
content:
application/json:
schema:
$ref: ../../schemas/interface/papers/find/PaperFindRequest.yaml
responses:
"200":
description: OK - Returns found paper
content:
application/json:
schema:
$ref: ../../schemas/interface/papers/find/PaperFindResponse.yaml
"400":
description: Bad Request - Invalid request
content:
application/json:
schema:
$ref: ../../schemas/interface/papers/find/PaperFindErrorResponse.yaml
"404":
description: Not Found - Paper not found or not authorized
content:
application/json:
schema:
$ref: ../../schemas/interface/papers/find/PaperFindErrorResponse.yaml
"500":
description: Internal Server Error - Server error
content:
application/json:
schema:
$ref: ../../schemas/interface/app/ApplicationErrorResponse.yaml
9 changes: 9 additions & 0 deletions docs/openapi/schemas/entity/chapter/ChapterOnlyId.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type: object
description: Chapter object with only ID
properties:
id:
type: string
description: Auto-generated chapter ID
example: 123e4567-e89b-12d3-a456-426614174000
required:
- id
7 changes: 7 additions & 0 deletions docs/openapi/schemas/entity/chapter/ChapterOnlyIdError.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: object
description: Error Message for ChapterOnlyId object
properties:
id:
type: string
description: Error message for project ID
example: "project id is required, but got ''"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type: object
description: Request Body for Paper Find 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
chapter:
$ref: ../../../entity/chapter/ChapterOnlyIdError.yaml
13 changes: 13 additions & 0 deletions docs/openapi/schemas/interface/papers/find/PaperFindRequest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type: object
description: Request Body for Paper Find API
properties:
user:
$ref: ../../../entity/user/UserOnlyId.yaml
project:
$ref: ../../../entity/project/ProjectOnlyId.yaml
chapter:
$ref: ../../../entity/chapter/ChapterOnlyId.yaml
required:
- user
- project
- chapter
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: object
description: Request Body for Paper Find API
properties:
paper:
$ref: ../../../entity/paper/Paper.yaml
required:
- paper
4 changes: 2 additions & 2 deletions fixtures/firebase-export-metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "13.7.5",
"version": "13.8.2",
"firestore": {
"version": "1.19.5",
"version": "1.19.6",
"path": "firestore_export",
"metadata_file": "firestore_export/firestore_export.overall_export_metadata"
}
Expand Down
Binary file not shown.
Binary file modified fixtures/firestore_export/all_namespaces/all_kinds/output-0
Binary file not shown.
Binary file not shown.
60 changes: 60 additions & 0 deletions internal/api/paper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package api

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kumachan-mis/knodeledge-api/internal/model"
"github.com/kumachan-mis/knodeledge-api/internal/usecase"
)

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

type paperApi struct {
usecase usecase.PaperUseCase
}

func NewPaperApi(usecase usecase.PaperUseCase) PaperApi {
return paperApi{usecase: usecase}
}

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

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

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

if ucErr != nil && ucErr.Code() == usecase.NotFoundError {
c.JSON(http.StatusNotFound, model.PaperFindErrorResponse{
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 eb9f0c3

Please sign in to comment.