186 lines
5.0 KiB
Go
186 lines
5.0 KiB
Go
|
// Code generated by entc, DO NOT EDIT.
|
||
|
|
||
|
package http
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
"t/ent"
|
||
|
|
||
|
"github.com/go-chi/chi/v5"
|
||
|
"go.uber.org/zap"
|
||
|
)
|
||
|
|
||
|
// NewHandler returns a ready to use handler with all generated endpoints mounted.
|
||
|
func NewHandler(c *ent.Client, l *zap.Logger) chi.Router {
|
||
|
r := chi.NewRouter()
|
||
|
MountRoutes(c, l, r)
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
// MountRoutes mounts all generated routes on the given router.
|
||
|
func MountRoutes(c *ent.Client, l *zap.Logger, r chi.Router) {
|
||
|
NewCompartmentHandler(c, l).MountRoutes(r)
|
||
|
NewEntryHandler(c, l).MountRoutes(r)
|
||
|
NewFridgeHandler(c, l).MountRoutes(r)
|
||
|
NewItemHandler(c, l).MountRoutes(r)
|
||
|
}
|
||
|
|
||
|
// CompartmentHandler handles http crud operations on ent.Compartment.
|
||
|
type CompartmentHandler struct {
|
||
|
client *ent.Client
|
||
|
log *zap.Logger
|
||
|
}
|
||
|
|
||
|
func NewCompartmentHandler(c *ent.Client, l *zap.Logger) *CompartmentHandler {
|
||
|
return &CompartmentHandler{
|
||
|
client: c,
|
||
|
log: l.With(zap.String("handler", "CompartmentHandler")),
|
||
|
}
|
||
|
}
|
||
|
func (h *CompartmentHandler) MountCreateRoute(r chi.Router) *CompartmentHandler {
|
||
|
r.Post("/compartments", h.Create)
|
||
|
return h
|
||
|
}
|
||
|
func (h *CompartmentHandler) MountReadRoute(r chi.Router) *CompartmentHandler {
|
||
|
r.Get("/compartments/{id}", h.Read)
|
||
|
return h
|
||
|
}
|
||
|
func (h *CompartmentHandler) MountUpdateRoute(r chi.Router) *CompartmentHandler {
|
||
|
r.Patch("/compartments/{id}", h.Update)
|
||
|
return h
|
||
|
}
|
||
|
func (h *CompartmentHandler) MountDeleteRoute(r chi.Router) *CompartmentHandler {
|
||
|
r.Delete("/compartments/{id}", h.Delete)
|
||
|
return h
|
||
|
}
|
||
|
func (h *CompartmentHandler) MountListRoute(r chi.Router) *CompartmentHandler {
|
||
|
r.Get("/compartments", h.List)
|
||
|
return h
|
||
|
}
|
||
|
func (h *CompartmentHandler) MountRoutes(r chi.Router) {
|
||
|
h.MountCreateRoute(r).MountReadRoute(r).MountUpdateRoute(r).MountDeleteRoute(r).MountListRoute(r)
|
||
|
}
|
||
|
|
||
|
// EntryHandler handles http crud operations on ent.Entry.
|
||
|
type EntryHandler struct {
|
||
|
client *ent.Client
|
||
|
log *zap.Logger
|
||
|
}
|
||
|
|
||
|
func NewEntryHandler(c *ent.Client, l *zap.Logger) *EntryHandler {
|
||
|
return &EntryHandler{
|
||
|
client: c,
|
||
|
log: l.With(zap.String("handler", "EntryHandler")),
|
||
|
}
|
||
|
}
|
||
|
func (h *EntryHandler) MountCreateRoute(r chi.Router) *EntryHandler {
|
||
|
r.Post("/entries", h.Create)
|
||
|
return h
|
||
|
}
|
||
|
func (h *EntryHandler) MountReadRoute(r chi.Router) *EntryHandler {
|
||
|
r.Get("/entries/{id}", h.Read)
|
||
|
return h
|
||
|
}
|
||
|
func (h *EntryHandler) MountUpdateRoute(r chi.Router) *EntryHandler {
|
||
|
r.Patch("/entries/{id}", h.Update)
|
||
|
return h
|
||
|
}
|
||
|
func (h *EntryHandler) MountDeleteRoute(r chi.Router) *EntryHandler {
|
||
|
r.Delete("/entries/{id}", h.Delete)
|
||
|
return h
|
||
|
}
|
||
|
func (h *EntryHandler) MountListRoute(r chi.Router) *EntryHandler {
|
||
|
r.Get("/entries", h.List)
|
||
|
return h
|
||
|
}
|
||
|
func (h *EntryHandler) MountRoutes(r chi.Router) {
|
||
|
h.MountCreateRoute(r).MountReadRoute(r).MountUpdateRoute(r).MountDeleteRoute(r).MountListRoute(r)
|
||
|
}
|
||
|
|
||
|
// FridgeHandler handles http crud operations on ent.Fridge.
|
||
|
type FridgeHandler struct {
|
||
|
client *ent.Client
|
||
|
log *zap.Logger
|
||
|
}
|
||
|
|
||
|
func NewFridgeHandler(c *ent.Client, l *zap.Logger) *FridgeHandler {
|
||
|
return &FridgeHandler{
|
||
|
client: c,
|
||
|
log: l.With(zap.String("handler", "FridgeHandler")),
|
||
|
}
|
||
|
}
|
||
|
func (h *FridgeHandler) MountCreateRoute(r chi.Router) *FridgeHandler {
|
||
|
r.Post("/fridges", h.Create)
|
||
|
return h
|
||
|
}
|
||
|
func (h *FridgeHandler) MountReadRoute(r chi.Router) *FridgeHandler {
|
||
|
r.Get("/fridges/{id}", h.Read)
|
||
|
return h
|
||
|
}
|
||
|
func (h *FridgeHandler) MountUpdateRoute(r chi.Router) *FridgeHandler {
|
||
|
r.Patch("/fridges/{id}", h.Update)
|
||
|
return h
|
||
|
}
|
||
|
func (h *FridgeHandler) MountDeleteRoute(r chi.Router) *FridgeHandler {
|
||
|
r.Delete("/fridges/{id}", h.Delete)
|
||
|
return h
|
||
|
}
|
||
|
func (h *FridgeHandler) MountListRoute(r chi.Router) *FridgeHandler {
|
||
|
r.Get("/fridges", h.List)
|
||
|
return h
|
||
|
}
|
||
|
func (h *FridgeHandler) MountRoutes(r chi.Router) {
|
||
|
h.MountCreateRoute(r).MountReadRoute(r).MountUpdateRoute(r).MountDeleteRoute(r).MountListRoute(r)
|
||
|
}
|
||
|
|
||
|
// ItemHandler handles http crud operations on ent.Item.
|
||
|
type ItemHandler struct {
|
||
|
client *ent.Client
|
||
|
log *zap.Logger
|
||
|
}
|
||
|
|
||
|
func NewItemHandler(c *ent.Client, l *zap.Logger) *ItemHandler {
|
||
|
return &ItemHandler{
|
||
|
client: c,
|
||
|
log: l.With(zap.String("handler", "ItemHandler")),
|
||
|
}
|
||
|
}
|
||
|
func (h *ItemHandler) MountCreateRoute(r chi.Router) *ItemHandler {
|
||
|
r.Post("/items", h.Create)
|
||
|
return h
|
||
|
}
|
||
|
func (h *ItemHandler) MountReadRoute(r chi.Router) *ItemHandler {
|
||
|
r.Get("/items/{id}", h.Read)
|
||
|
return h
|
||
|
}
|
||
|
func (h *ItemHandler) MountUpdateRoute(r chi.Router) *ItemHandler {
|
||
|
r.Patch("/items/{id}", h.Update)
|
||
|
return h
|
||
|
}
|
||
|
func (h *ItemHandler) MountDeleteRoute(r chi.Router) *ItemHandler {
|
||
|
r.Delete("/items/{id}", h.Delete)
|
||
|
return h
|
||
|
}
|
||
|
func (h *ItemHandler) MountListRoute(r chi.Router) *ItemHandler {
|
||
|
r.Get("/items", h.List)
|
||
|
return h
|
||
|
}
|
||
|
func (h *ItemHandler) MountRoutes(r chi.Router) {
|
||
|
h.MountCreateRoute(r).MountReadRoute(r).MountUpdateRoute(r).MountDeleteRoute(r).MountListRoute(r)
|
||
|
}
|
||
|
|
||
|
func stripEntError(err error) string {
|
||
|
return strings.TrimPrefix(err.Error(), "ent: ")
|
||
|
}
|
||
|
|
||
|
func zapFields(errs map[string]string) []zap.Field {
|
||
|
if errs == nil || len(errs) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
r := make([]zap.Field, 0)
|
||
|
for k, v := range errs {
|
||
|
r = append(r, zap.String(k, v))
|
||
|
}
|
||
|
return r
|
||
|
}
|