Skip to content

Commit

Permalink
Add batch post to generated controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
joeriddles committed Sep 26, 2024
1 parent 8d80a57 commit 55c540c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ func (g *generator) generateOpenApiBase(metadatas []*entity.GormModelMetadata) e
doc.Paths.Set(fmt.Sprintf("/%v/{id}/", utils.ToHtmlCase(metadata.Name)), &openapi3.PathItem{
Ref: fmt.Sprintf("./%v.gen.yaml#/paths/~1%%7Bid%%7D~1", utils.ToSnakeCase(metadata.Name)),
})
doc.Paths.Set(fmt.Sprintf("/%v/batch/", utils.ToHtmlCase(metadata.Name)), &openapi3.PathItem{
Ref: fmt.Sprintf("./%v.gen.yaml#/paths/~1batch~1", utils.ToSnakeCase(metadata.Name)),
})
}

if err := doc.Paths.Validate(context.Background()); err != nil {
Expand Down
28 changes: 28 additions & 0 deletions pkg/generate/templates/controller.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type {{.model.Name}}Controller interface {
Delete{{.model.Name}}ID(ctx context.Context, request {{Types}}Delete{{.model.Name}}IDRequestObject) ({{Types}}Delete{{.model.Name}}IDResponseObject, error)
Get{{.model.Name}}ID(ctx context.Context, request {{Types}}Get{{.model.Name}}IDRequestObject) ({{Types}}Get{{.model.Name}}IDResponseObject, error)
Put{{.model.Name}}ID(ctx context.Context, request {{Types}}Put{{.model.Name}}IDRequestObject) ({{Types}}Put{{.model.Name}}IDResponseObject, error)
Post{{.model.Name}}Batch(ctx context.Context, request {{Types}}Post{{.model.Name}}BatchRequestObject) ({{Types}}Post{{.model.Name}}BatchResponseObject, error)
}

type {{.model.Name|ToCamelCase}}Controller struct {
Expand Down Expand Up @@ -86,3 +87,30 @@ func (c *{{.model.Name|ToCamelCase}}Controller) Put{{.model.Name}}ID(ctx context
}
return {{Types}}Put{{.model.Name}}ID204Response{}, nil
}

func (c *{{.model.Name|ToCamelCase}}Controller) Post{{.model.Name}}Batch(ctx context.Context, request {{Types}}Post{{.model.Name}}BatchRequestObject) ({{Types}}Post{{.model.Name}}BatchResponseObject, error) {
srcs := request.Body

apiModels := []{{.model.Name}}{}
errs := []error{}
for _, src := range *srcs {
dst := &model.{{.model.Name}}{}
{{range .createApi.Fields}}
{{.|ConvertToModel}}{{end}}

createdModel, err := c.repository.Create(ctx, *dst)
if err != nil {
errs = append(errs, err)
continue
}

apiModel := c.apiMapper.Map(*createdModel)
apiModels = append(apiModels, apiModel)
}

if len(errs) > 0 {
return nil, errors.Join(errs...)
}

return {{Types}}Post{{.model.Name}}Batch201JSONResponse(apiModels), nil
}
28 changes: 28 additions & 0 deletions pkg/generate/templates/echo_controller.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type {{.model.Name}}Controller interface {
Delete{{.model.Name}}ID(ctx echo.Context, request {{Types}}Delete{{.model.Name}}IDRequestObject) ({{Types}}Delete{{.model.Name}}IDResponseObject, error)
Get{{.model.Name}}ID(ctx echo.Context, request {{Types}}Get{{.model.Name}}IDRequestObject) ({{Types}}Get{{.model.Name}}IDResponseObject, error)
Put{{.model.Name}}ID(ctx echo.Context, request {{Types}}Put{{.model.Name}}IDRequestObject) ({{Types}}Put{{.model.Name}}IDResponseObject, error)
Post{{.model.Name}}Batch(ctx echo.Context, request {{Types}}Post{{.model.Name}}BatchRequestObject) ({{Types}}Post{{.model.Name}}BatchResponseObject, error)
}

type {{.model.Name|ToCamelCase}}Controller struct {
Expand Down Expand Up @@ -87,3 +88,30 @@ func (c *{{.model.Name|ToCamelCase}}Controller) Put{{.model.Name}}ID(ctx echo.Co
}
return {{Types}}Put{{.model.Name}}ID204Response{}, nil
}

func (c *{{.model.Name|ToCamelCase}}Controller) Post{{.model.Name}}Batch(ctx echo.Context, request {{Types}}Post{{.model.Name}}BatchRequestObject) ({{Types}}Post{{.model.Name}}BatchResponseObject, error) {
srcs := request.Body

apiModels := []{{.model.Name}}{}
errs := []error{}
for _, src := range *srcs {
dst := &model.{{.model.Name}}{}
{{range .createApi.Fields}}
{{.|ConvertToModel}}{{end}}

createdModel, err := c.repository.Create(ctx.Request().Context(), *dst)
if err != nil {
errs = append(errs, err)
continue
}

apiModel := c.apiMapper.Map(*createdModel)
apiModels = append(apiModels, apiModel)
}

if len(errs) > 0 {
return nil, errors.Join(errs...)
}

return {{Types}}Post{{.model.Name}}Batch201JSONResponse(apiModels), nil
}
2 changes: 1 addition & 1 deletion pkg/generate/templates/main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
server := api.NewServer()
server := api.NewServer(nil)

r := http.NewServeMux()

Expand Down
36 changes: 36 additions & 0 deletions pkg/generate/templates/openapi_controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ paths:
description: Deleted
"404":
$ref: "#/components/responses/NotFound"
/batch/:
post:
tags:
- "{{.Name|ToSnakeCase}}"
summary: Batch create multiple new {{.Name}}s
parameters:
- name: clear
in: query
description: If true, clears all existing {{.Name}}s before creating new ones
required: false
schema:
type: boolean
default: false
requestBody:
required: true
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Create{{.Name}}'
responses:
"201":
description: Created
content:
application/json:
schema:
$ref: '#/components/schemas/{{.Name}}s'
"400":
$ref: "#/components/responses/BadRequest"
"409":
$ref: "#/components/responses/Conflict"

components:
schemas:
Expand All @@ -117,6 +149,10 @@ components:
{{end}}
{{range .Embedded}}{{range .Fields}}{{if Not (IsNullable .Type)}}- {{.Name|ToSnakeCase}}{{end}}
{{end}}{{end}}
{{.Name}}s:
type: array
items:
$ref: '#/components/schemas/{{.Name}}'
Create{{.Name}}:
type: object
properties:
Expand Down
4 changes: 4 additions & 0 deletions pkg/generate/templates/server.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ func (s *Server) Get{{.Name}}ID(ctx context.Context, request {{Types}}Get{{.Name
func (s *Server) Put{{.Name}}ID(ctx context.Context, request {{Types}}Put{{.Name}}IDRequestObject) ({{Types}}Put{{.Name}}IDResponseObject, error) {
return s.{{.Name}}Controller.Put{{.Name}}ID(ctx, request)
}

func (s *Server) Post{{.Name}}Batch(ctx context.Context, request Post{{.Name}}BatchRequestObject) (Post{{.Name}}BatchResponseObject, error) {
return s.{{.Name}}Controller.Post{{.Name}}Batch(ctx, request)
}
{{end}}

0 comments on commit 55c540c

Please sign in to comment.