Skip to content

Commit

Permalink
feat: implement Graph Sectionalize API (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored Sep 7, 2024
1 parent 5f033b3 commit d1b6fca
Show file tree
Hide file tree
Showing 61 changed files with 2,977 additions and 448 deletions.
6 changes: 6 additions & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ func main() {
projectRepository := repository.NewProjectRepository(*client)
chapterRepository := repository.NewChapterRepository(*client)
paperRepository := repository.NewPaperRepository(*client)
graphRepository := repository.NewGraphRepository(*client)

projectService := service.NewProjectService(projectRepository)
chapterService := service.NewChapterService(chapterRepository, paperRepository)
paperService := service.NewPaperService(paperRepository)
graphService := service.NewGraphService(graphRepository, chapterRepository)

projectUseCase := usecase.NewProjectUseCase(projectService)
chapterUseCase := usecase.NewChapterUseCase(chapterService)
paperUseCase := usecase.NewPaperUseCase(paperService)
graphUseCase := usecase.NewGraphUseCase(graphService)

projectApi := api.NewProjectApi(projectUseCase)
router.POST("/api/projects/list", projectApi.HandleList)
Expand All @@ -81,6 +84,9 @@ func main() {
router.POST("/api/papers/find", paperApi.HandleFind)
router.POST("/api/papers/update", paperApi.HandleUpdate)

graphApi := api.NewGraphApi(graphUseCase)
router.POST("/api/graphs/sectionalize", graphApi.HandleSectionalize)

err = router.Run(":8080")
if err != nil {
log.Fatalf("Failed to run gin server: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions docs/openapi/paths/_index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
$ref: ./papers/find.yaml
/api/papers/update:
$ref: ./papers/update.yaml
/api/papers/sectionalize:
$ref: ./papers/sectionalize.yaml
/api/graphs/sectionalize:
$ref: ./graphs/sectionalize.yaml
35 changes: 35 additions & 0 deletions docs/openapi/paths/graphs/sectionalize.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
post:
tags:
- Graphs
operationId: graphs-sectionalize
summary: Sectionalize into graphs
requestBody:
content:
application/json:
schema:
$ref: ../../schemas/interface/graphs/sectionalize/GraphSectionalizeRequest.yaml
responses:
"200":
description: OK - Returns graphs from sections
content:
application/json:
schema:
$ref: ../../schemas/interface/graphs/sectionalize/GraphSectionalizeResponse.yaml
"400":
description: Bad Request - Invalid request
content:
application/json:
schema:
$ref: ../../schemas/interface/graphs/sectionalize/GraphSectionalizeErrorResponse.yaml
"404":
description: Not Found - Graph not found or not authorized
content:
application/json:
schema:
$ref: ../../schemas/interface/graphs/sectionalize/GraphSectionalizeErrorResponse.yaml
"500":
description: Internal Server Error - Server error
content:
application/json:
schema:
$ref: ../../schemas/interface/app/ApplicationErrorResponse.yaml
35 changes: 0 additions & 35 deletions docs/openapi/paths/papers/sectionalize.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ properties:
sections:
type: array
items:
$ref: ../section/Section.yaml
$ref: ../section/SectionOfChapter.yaml
required:
- id
- name
Expand Down
23 changes: 23 additions & 0 deletions docs/openapi/schemas/entity/graph/Graph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type: object
description: Graph object
properties:
id:
type: string
description: Auto-generated section ID
example: 123e4567-e89b-12d3-a456-426614174000
name:
type: string
maxLength: 100
description: Graph name
example: Overview
paragraph:
type: string
maxLength: 40000
description: Graph paragraph
example: |
## Introduction
This is the introduction of the paper.
required:
- id
- name
- paragraph
9 changes: 0 additions & 9 deletions docs/openapi/schemas/entity/paper/PaperOnlyId.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions docs/openapi/schemas/entity/paper/PaperOnlyIdError.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type: object
description: Section object
description: Section object in a Chapter object
properties:
id:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ properties:
maxLength: 100
description: Section name
example: Overview
content:
type: string
maxLength: 40000
description: Section content
example: |
## Introduction
This is the introduction of the paper.
required:
- name
- content
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ properties:
type: string
description: Error message for section name
example: section name is required, but got ''
content:
type: string
description: Error message for section content
example: section content is required, but got ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: object
description: Error Message for SectionWithoutAutofield list
properties:
message:
type: string
description: Error message for overall of sections
example: sections is required, but got empty list
items:
type: array
items:
$ref: ./SectionWithoutAutofieldError.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type: object
description: Error Response Body for Graph Sectionalize 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
sections:
$ref: ../../../entity/section/SectionWithoutAutofieldListError.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
type: object
description: Request Body for Paper Sectionalize API
description: Request Body for Graph Sectionalize API
properties:
user:
$ref: ../../../entity/user/UserOnlyId.yaml
project:
$ref: ../../../entity/project/ProjectOnlyId.yaml
paper:
$ref: ../../../entity/paper/PaperOnlyId.yaml
chapter:
$ref: ../../../entity/chapter/ChapterOnlyId.yaml
sections:
type: array
items:
$ref: ../../../entity/section/SectionWithoutAutofield.yaml
maxItems: 20
required:
- user
- project
- paper
- chapter
- sections
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type: object
description: Response Body for Graph Sectionalize API
properties:
graphs:
type: array
items:
$ref: ../../../entity/graph/Graph.yaml
maxItems: 20
required:
- graphs

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file modified fixtures/firestore_export/all_namespaces/all_kinds/output-0
Binary file not shown.
Binary file not shown.
68 changes: 68 additions & 0 deletions internal/api/graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 GraphApi interface {
HandleSectionalize(c *gin.Context)
}

type graphApi struct {
usecase usecase.GraphUseCase
}

func NewGraphApi(usecase usecase.GraphUseCase) GraphApi {
return graphApi{usecase: usecase}
}

func (api graphApi) HandleSectionalize(c *gin.Context) {
var request model.GraphSectionalizeRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, model.GraphSectionalizeErrorResponse{
Message: JsonBindErrorToMessage(err),
})
return
}

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

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

if ucErr != nil && ucErr.Code() == usecase.InvalidArgumentError {
c.JSON(http.StatusBadRequest, model.GraphSectionalizeErrorResponse{
Message: UseCaseErrorToMessage(ucErr),
})
return
}

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

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

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

0 comments on commit d1b6fca

Please sign in to comment.