From 74b554adff3e5c19d1b48af5f2b1fd2854ceabd6 Mon Sep 17 00:00:00 2001 From: Ethan Davidson <31261035+EthanThatOneKid@users.noreply.github.com> Date: Thu, 30 Nov 2023 19:34:10 -0800 Subject: [PATCH] add idea about batch crud operations --- .../interactors/interactors_generated.go | 96 ++++++++++--------- api/openapi/openapi.go | 5 +- cli/interactors_generator/crudl.go.tmpl | 16 +++- 3 files changed, 66 insertions(+), 51 deletions(-) diff --git a/api/openapi/interactors/interactors_generated.go b/api/openapi/interactors/interactors_generated.go index 3198890..0ed6ba6 100644 --- a/api/openapi/interactors/interactors_generated.go +++ b/api/openapi/interactors/interactors_generated.go @@ -1,28 +1,36 @@ -// Code is generated. DO NOT EDIT. - -package interactors - -import ( - "context" - "io" - "net/http" - - "github.com/swaggest/rest/nethttp" - "github.com/swaggest/rest/web" - "github.com/swaggest/usecase" -) - -// crudl is a helper function for registering create, read, update, delete, -// and list usecases for a given resource. -type crudl struct { +// Code is generated. DO NOT EDIT. + +package interactors + +import ( + "context" + "io" + "net/http" + + "github.com/swaggest/rest/nethttp" + "github.com/swaggest/rest/web" + "github.com/swaggest/usecase" +) + +// crud is a helper struct for registering create, read, update, delete, +// and batch usecases for a given resource. +type crud struct { patternPrefix string creater usecase.Interactor + batchCreater usecase.Interactor reader usecase.Interactor + batchReader usecase.Interactor updater usecase.Interactor + batchUpdater usecase.Interactor deleter usecase.Interactor - lister usecase.Interactor + batchDeleter usecase.Interactor } +// TODO: Replace instances of 'crudl' with 'crud'. + +// TODO: Split up the functions into templates so they are included in the +// generated code only if they are used. + type crudlOptionFn func(*crudl) func withPrefix(prefix string) crudlOptionFn { @@ -87,29 +95,29 @@ func useCRUDL(service *web.Service, optionFns ...crudlOptionFn) { service.Get(o.patternPrefix, o.lister, nethttp.SuccessStatus(http.StatusOK)) } } - -// ContainsContext can be embedded by any interface to have an overrideable -// context. -type ContainsContext interface { - WithContext(context.Context) ContainsContext -} - -// Store is the store interface. -type Store interface { - io.Closer - ContainsContext - -} - -// UseEvents uses a generated Events interactor. -func UseEvents(service *web.Service, store Store) { - useCRUDL( - service, - withPrefix("/events"), - ) -} - -// UseAll uses all interactors. -func UseAll(service *web.Service, store Store) { - UseEvents(service, store) -} + +// ContainsContext can be embedded by any interface to have an overrideable +// context. +type ContainsContext interface { + WithContext(context.Context) ContainsContext +} + +// Store is the store interface. +type Store interface { + io.Closer + ContainsContext + +} + +// UseEvents uses a generated Events interactor. +func UseEvents(service *web.Service, store Store) { + useCRUDL( + service, + withPrefix("/events"), + ) +} + +// UseAll uses all interactors. +func UseAll(service *web.Service, store Store) { + UseEvents(service, store) +} diff --git a/api/openapi/openapi.go b/api/openapi/openapi.go index a5bddbd..d5cf9c0 100644 --- a/api/openapi/openapi.go +++ b/api/openapi/openapi.go @@ -3,7 +3,6 @@ package openapi import ( "net/http" - "github.com/acmcsufoss/api.acmcsuf.com/api" "github.com/acmcsufoss/api.acmcsuf.com/api/openapi/interactors" "github.com/go-chi/chi/v5/middleware" "github.com/rs/cors" @@ -12,7 +11,7 @@ import ( ) // NewOpenAPI creates a new OpenAPI handler. -func NewOpenAPI(s api.Store) http.Handler { +func NewOpenAPI(s interactors.Store) http.Handler { // Service initializes router with required middlewares. service := web.NewService(openapi3.NewReflector()) @@ -35,7 +34,7 @@ func NewOpenAPI(s api.Store) http.Handler { } // useAPIStoreInteractors registers all generated API handler interactors. -func useAPIStoreInteractors(service *web.Service, s api.Store) { +func useAPIStoreInteractors(service *web.Service, s interactors.Store) { interactors.UseAll(service, s) // TODO: Register additional interactors here as needed... } diff --git a/cli/interactors_generator/crudl.go.tmpl b/cli/interactors_generator/crudl.go.tmpl index 06919eb..b58a686 100644 --- a/cli/interactors_generator/crudl.go.tmpl +++ b/cli/interactors_generator/crudl.go.tmpl @@ -1,14 +1,22 @@ -// crudl is a helper function for registering create, read, update, delete, -// and list usecases for a given resource. -type crudl struct { +// crud is a helper struct for registering create, read, update, delete, +// and batch usecases for a given resource. +type crud struct { patternPrefix string creater usecase.Interactor + batchCreater usecase.Interactor reader usecase.Interactor + batchReader usecase.Interactor updater usecase.Interactor + batchUpdater usecase.Interactor deleter usecase.Interactor - lister usecase.Interactor + batchDeleter usecase.Interactor } +// TODO: Replace instances of 'crudl' with 'crud'. + +// TODO: Split up the functions into templates so they are included in the +// generated code only if they are used. + type crudlOptionFn func(*crudl) func withPrefix(prefix string) crudlOptionFn {