Skip to content

Commit

Permalink
add idea about batch crud operations
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Dec 1, 2023
1 parent 649bf76 commit 74b554a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 51 deletions.
96 changes: 52 additions & 44 deletions api/openapi/interactors/interactors_generated.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
5 changes: 2 additions & 3 deletions api/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())

Expand All @@ -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...
}
16 changes: 12 additions & 4 deletions cli/interactors_generator/crudl.go.tmpl
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 74b554a

Please sign in to comment.