1
0
This commit is contained in:
2022-08-31 15:18:14 +09:00
commit d40e30944b
61 changed files with 21400 additions and 0 deletions

224
ent/http/create.go Normal file
View File

@ -0,0 +1,224 @@
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strings"
"t/ent"
"t/ent/compartment"
"t/ent/entry"
"t/ent/fridge"
"t/ent/item"
"github.com/mailru/easyjson"
"go.uber.org/zap"
)
// Create creates a new ent.Compartment and stores it in the database.
func (h CompartmentHandler) Create(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Create"))
// Get the post data.
var d CompartmentCreateRequest
if err := easyjson.UnmarshalFromReader(r.Body, &d); err != nil {
l.Error("error decoding json", zap.Error(err))
BadRequest(w, "invalid json string")
return
}
// Save the data.
b := h.client.Compartment.Create()
e, err := b.Save(r.Context())
if err != nil {
switch {
default:
l.Error("could not create compartment", zap.Error(err))
InternalServerError(w, nil)
}
return
}
// Store id of fresh entity to log errors for the reload.
id := e.ID
// Reload entry.
q := h.client.Compartment.Query().Where(compartment.ID(e.ID))
ret, err := q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could not read compartment", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("compartment rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewCompartment3324871446View(ret), w)
}
// Create creates a new ent.Entry and stores it in the database.
func (h EntryHandler) Create(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Create"))
// Get the post data.
var d EntryCreateRequest
if err := easyjson.UnmarshalFromReader(r.Body, &d); err != nil {
l.Error("error decoding json", zap.Error(err))
BadRequest(w, "invalid json string")
return
}
// Validate the data.
errs := make(map[string]string)
if d.User == nil {
errs["user"] = `missing required field: "user"`
} else if err := entry.UserValidator(*d.User); err != nil {
errs["user"] = strings.TrimPrefix(err.Error(), "entry: ")
}
if d.First == nil {
errs["first"] = `missing required field: "first"`
}
if len(errs) > 0 {
l.Info("validation failed", zapFields(errs)...)
BadRequest(w, errs)
return
}
// Save the data.
b := h.client.Entry.Create()
if d.User != nil {
b.SetUser(*d.User)
}
if d.First != nil {
b.SetFirst(*d.First)
}
if d.CreatedAt != nil {
b.SetCreatedAt(*d.CreatedAt)
}
e, err := b.Save(r.Context())
if err != nil {
switch {
default:
l.Error("could not create entry", zap.Error(err))
InternalServerError(w, nil)
}
return
}
// Store id of fresh entity to log errors for the reload.
id := e.ID
// Reload entry.
q := h.client.Entry.Query().Where(entry.ID(e.ID))
ret, err := q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.String("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.String("id", id))
BadRequest(w, msg)
default:
l.Error("could not read entry", zap.Error(err), zap.String("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("entry rendered", zap.String("id", id))
easyjson.MarshalToHTTPResponseWriter(NewEntry2675665849View(ret), w)
}
// Create creates a new ent.Fridge and stores it in the database.
func (h FridgeHandler) Create(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Create"))
// Get the post data.
var d FridgeCreateRequest
if err := easyjson.UnmarshalFromReader(r.Body, &d); err != nil {
l.Error("error decoding json", zap.Error(err))
BadRequest(w, "invalid json string")
return
}
// Save the data.
b := h.client.Fridge.Create()
e, err := b.Save(r.Context())
if err != nil {
switch {
default:
l.Error("could not create fridge", zap.Error(err))
InternalServerError(w, nil)
}
return
}
// Store id of fresh entity to log errors for the reload.
id := e.ID
// Reload entry.
q := h.client.Fridge.Query().Where(fridge.ID(e.ID))
ret, err := q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could not read fridge", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("fridge rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewFridge2211356377View(ret), w)
}
// Create creates a new ent.Item and stores it in the database.
func (h ItemHandler) Create(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Create"))
// Get the post data.
var d ItemCreateRequest
if err := easyjson.UnmarshalFromReader(r.Body, &d); err != nil {
l.Error("error decoding json", zap.Error(err))
BadRequest(w, "invalid json string")
return
}
// Save the data.
b := h.client.Item.Create()
e, err := b.Save(r.Context())
if err != nil {
switch {
default:
l.Error("could not create item", zap.Error(err))
InternalServerError(w, nil)
}
return
}
// Store id of fresh entity to log errors for the reload.
id := e.ID
// Reload entry.
q := h.client.Item.Query().Where(item.ID(e.ID))
ret, err := q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could not read item", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("item rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewItem1548468123View(ret), w)
}

116
ent/http/delete.go Normal file
View File

@ -0,0 +1,116 @@
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"t/ent"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)
// Delete removes a ent.Compartment from the database.
func (h CompartmentHandler) Delete(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Delete"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
err = h.client.Compartment.DeleteOneID(id).Exec(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
default:
l.Error("could-not-delete-compartment", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("compartment deleted", zap.Int("id", id))
w.WriteHeader(http.StatusNoContent)
}
// Delete removes a ent.Entry from the database.
func (h EntryHandler) Delete(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Delete"))
// ID is URL parameter.
var err error
id := chi.URLParam(r, "id")
err = h.client.Entry.DeleteOneID(id).Exec(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.String("id", id))
NotFound(w, msg)
default:
l.Error("could-not-delete-entry", zap.Error(err), zap.String("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("entry deleted", zap.String("id", id))
w.WriteHeader(http.StatusNoContent)
}
// Delete removes a ent.Fridge from the database.
func (h FridgeHandler) Delete(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Delete"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
err = h.client.Fridge.DeleteOneID(id).Exec(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
default:
l.Error("could-not-delete-fridge", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("fridge deleted", zap.Int("id", id))
w.WriteHeader(http.StatusNoContent)
}
// Delete removes a ent.Item from the database.
func (h ItemHandler) Delete(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Delete"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
err = h.client.Item.DeleteOneID(id).Exec(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
default:
l.Error("could-not-delete-item", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("item deleted", zap.Int("id", id))
w.WriteHeader(http.StatusNoContent)
}

1075
ent/http/easyjson.go Normal file

File diff suppressed because it is too large Load Diff

185
ent/http/handler.go Normal file
View File

@ -0,0 +1,185 @@
// 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
}

147
ent/http/list.go Normal file
View File

@ -0,0 +1,147 @@
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"github.com/mailru/easyjson"
"go.uber.org/zap"
)
// Read fetches the ent.Compartment identified by a given url-parameter from the
// database and returns it to the client.
func (h *CompartmentHandler) List(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "List"))
q := h.client.Compartment.Query()
var err error
page := 1
if d := r.URL.Query().Get("page"); d != "" {
page, err = strconv.Atoi(d)
if err != nil {
l.Info("error parsing query parameter 'page'", zap.String("page", d), zap.Error(err))
BadRequest(w, "page must be an integer greater zero")
return
}
}
itemsPerPage := 30
if d := r.URL.Query().Get("itemsPerPage"); d != "" {
itemsPerPage, err = strconv.Atoi(d)
if err != nil {
l.Info("error parsing query parameter 'itemsPerPage'", zap.String("itemsPerPage", d), zap.Error(err))
BadRequest(w, "itemsPerPage must be an integer greater zero")
return
}
}
es, err := q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage).All(r.Context())
if err != nil {
l.Error("error fetching compartments from db", zap.Error(err))
InternalServerError(w, nil)
return
}
l.Info("compartments rendered", zap.Int("amount", len(es)))
easyjson.MarshalToHTTPResponseWriter(NewCompartment3324871446Views(es), w)
}
// Read fetches the ent.Entry identified by a given url-parameter from the
// database and returns it to the client.
func (h *EntryHandler) List(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "List"))
q := h.client.Entry.Query()
var err error
page := 1
if d := r.URL.Query().Get("page"); d != "" {
page, err = strconv.Atoi(d)
if err != nil {
l.Info("error parsing query parameter 'page'", zap.String("page", d), zap.Error(err))
BadRequest(w, "page must be an integer greater zero")
return
}
}
itemsPerPage := 30
if d := r.URL.Query().Get("itemsPerPage"); d != "" {
itemsPerPage, err = strconv.Atoi(d)
if err != nil {
l.Info("error parsing query parameter 'itemsPerPage'", zap.String("itemsPerPage", d), zap.Error(err))
BadRequest(w, "itemsPerPage must be an integer greater zero")
return
}
}
es, err := q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage).All(r.Context())
if err != nil {
l.Error("error fetching entries from db", zap.Error(err))
InternalServerError(w, nil)
return
}
l.Info("entries rendered", zap.Int("amount", len(es)))
easyjson.MarshalToHTTPResponseWriter(NewEntry2675665849Views(es), w)
}
// Read fetches the ent.Fridge identified by a given url-parameter from the
// database and returns it to the client.
func (h *FridgeHandler) List(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "List"))
q := h.client.Fridge.Query()
var err error
page := 1
if d := r.URL.Query().Get("page"); d != "" {
page, err = strconv.Atoi(d)
if err != nil {
l.Info("error parsing query parameter 'page'", zap.String("page", d), zap.Error(err))
BadRequest(w, "page must be an integer greater zero")
return
}
}
itemsPerPage := 30
if d := r.URL.Query().Get("itemsPerPage"); d != "" {
itemsPerPage, err = strconv.Atoi(d)
if err != nil {
l.Info("error parsing query parameter 'itemsPerPage'", zap.String("itemsPerPage", d), zap.Error(err))
BadRequest(w, "itemsPerPage must be an integer greater zero")
return
}
}
es, err := q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage).All(r.Context())
if err != nil {
l.Error("error fetching fridges from db", zap.Error(err))
InternalServerError(w, nil)
return
}
l.Info("fridges rendered", zap.Int("amount", len(es)))
easyjson.MarshalToHTTPResponseWriter(NewFridge2211356377Views(es), w)
}
// Read fetches the ent.Item identified by a given url-parameter from the
// database and returns it to the client.
func (h *ItemHandler) List(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "List"))
q := h.client.Item.Query()
var err error
page := 1
if d := r.URL.Query().Get("page"); d != "" {
page, err = strconv.Atoi(d)
if err != nil {
l.Info("error parsing query parameter 'page'", zap.String("page", d), zap.Error(err))
BadRequest(w, "page must be an integer greater zero")
return
}
}
itemsPerPage := 30
if d := r.URL.Query().Get("itemsPerPage"); d != "" {
itemsPerPage, err = strconv.Atoi(d)
if err != nil {
l.Info("error parsing query parameter 'itemsPerPage'", zap.String("itemsPerPage", d), zap.Error(err))
BadRequest(w, "itemsPerPage must be an integer greater zero")
return
}
}
es, err := q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage).All(r.Context())
if err != nil {
l.Error("error fetching items from db", zap.Error(err))
InternalServerError(w, nil)
return
}
l.Info("items rendered", zap.Int("amount", len(es)))
easyjson.MarshalToHTTPResponseWriter(NewItem1548468123Views(es), w)
}

149
ent/http/read.go Normal file
View File

@ -0,0 +1,149 @@
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"t/ent"
"t/ent/compartment"
"t/ent/entry"
"t/ent/fridge"
"t/ent/item"
"github.com/go-chi/chi/v5"
"github.com/mailru/easyjson"
"go.uber.org/zap"
)
// Read fetches the ent.Compartment identified by a given url-parameter from the
// database and renders it to the client.
func (h *CompartmentHandler) Read(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Read"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
// Create the query to fetch the Compartment
q := h.client.Compartment.Query().Where(compartment.ID(id))
e, err := q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could not read compartment", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("compartment rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewCompartment3324871446View(e), w)
}
// Read fetches the ent.Entry identified by a given url-parameter from the
// database and renders it to the client.
func (h *EntryHandler) Read(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Read"))
// ID is URL parameter.
var err error
id := chi.URLParam(r, "id")
// Create the query to fetch the Entry
q := h.client.Entry.Query().Where(entry.ID(id))
e, err := q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.String("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.String("id", id))
BadRequest(w, msg)
default:
l.Error("could not read entry", zap.Error(err), zap.String("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("entry rendered", zap.String("id", id))
easyjson.MarshalToHTTPResponseWriter(NewEntry2675665849View(e), w)
}
// Read fetches the ent.Fridge identified by a given url-parameter from the
// database and renders it to the client.
func (h *FridgeHandler) Read(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Read"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
// Create the query to fetch the Fridge
q := h.client.Fridge.Query().Where(fridge.ID(id))
e, err := q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could not read fridge", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("fridge rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewFridge2211356377View(e), w)
}
// Read fetches the ent.Item identified by a given url-parameter from the
// database and renders it to the client.
func (h *ItemHandler) Read(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Read"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
// Create the query to fetch the Item
q := h.client.Item.Query().Where(item.ID(id))
e, err := q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could not read item", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("item rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewItem1548468123View(e), w)
}

3
ent/http/relations.go Normal file
View File

@ -0,0 +1,3 @@
// Code generated by entc, DO NOT EDIT.
package http

42
ent/http/request.go Normal file
View File

@ -0,0 +1,42 @@
// Code generated by entc, DO NOT EDIT.
package http
import (
"time"
)
// Payload of a ent.Compartment create request.
type CompartmentCreateRequest struct {
}
// Payload of a ent.Compartment update request.
type CompartmentUpdateRequest struct {
}
// Payload of a ent.Entry create request.
type EntryCreateRequest struct {
User *string `json:"user"`
First *int `json:"first"`
CreatedAt *time.Time `json:"created_at"`
}
// Payload of a ent.Entry update request.
type EntryUpdateRequest struct {
}
// Payload of a ent.Fridge create request.
type FridgeCreateRequest struct {
}
// Payload of a ent.Fridge update request.
type FridgeUpdateRequest struct {
}
// Payload of a ent.Item create request.
type ItemCreateRequest struct {
}
// Payload of a ent.Item update request.
type ItemUpdateRequest struct {
}

200
ent/http/response.go Normal file
View File

@ -0,0 +1,200 @@
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"t/ent"
"time"
"github.com/mailru/easyjson"
)
// Basic HTTP Error Response
type ErrResponse struct {
Code int `json:"code"` // http response status code
Status string `json:"status"` // user-level status message
Errors interface{} `json:"errors,omitempty"` // application-level error
}
func (e ErrResponse) MarshalToHTTPResponseWriter(w http.ResponseWriter) (int, error) {
d, err := easyjson.Marshal(e)
if err != nil {
return 0, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(d)))
w.WriteHeader(e.Code)
return w.Write(d)
}
func BadRequest(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusBadRequest,
Status: http.StatusText(http.StatusBadRequest),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Conflict(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusConflict,
Status: http.StatusText(http.StatusConflict),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Forbidden(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusForbidden,
Status: http.StatusText(http.StatusForbidden),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func InternalServerError(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusInternalServerError,
Status: http.StatusText(http.StatusInternalServerError),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func NotFound(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusNotFound,
Status: http.StatusText(http.StatusNotFound),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Unauthorized(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusUnauthorized,
Status: http.StatusText(http.StatusUnauthorized),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
type (
// Compartment3324871446View represents the data serialized for the following serialization group combinations:
// []
Compartment3324871446View struct {
ID int `json:"id,omitempty"`
}
Compartment3324871446Views []*Compartment3324871446View
)
func NewCompartment3324871446View(e *ent.Compartment) *Compartment3324871446View {
if e == nil {
return nil
}
return &Compartment3324871446View{
ID: e.ID,
}
}
func NewCompartment3324871446Views(es []*ent.Compartment) Compartment3324871446Views {
if len(es) == 0 {
return nil
}
r := make(Compartment3324871446Views, len(es))
for i, e := range es {
r[i] = NewCompartment3324871446View(e)
}
return r
}
type (
// Entry2675665849View represents the data serialized for the following serialization group combinations:
// []
Entry2675665849View struct {
ID string `json:"id,omitempty"`
User string `json:"user,omitempty"`
First int `json:"first,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
Entry2675665849Views []*Entry2675665849View
)
func NewEntry2675665849View(e *ent.Entry) *Entry2675665849View {
if e == nil {
return nil
}
return &Entry2675665849View{
ID: e.ID,
User: e.User,
First: e.First,
CreatedAt: e.CreatedAt,
}
}
func NewEntry2675665849Views(es []*ent.Entry) Entry2675665849Views {
if len(es) == 0 {
return nil
}
r := make(Entry2675665849Views, len(es))
for i, e := range es {
r[i] = NewEntry2675665849View(e)
}
return r
}
type (
// Fridge2211356377View represents the data serialized for the following serialization group combinations:
// []
Fridge2211356377View struct {
ID int `json:"id,omitempty"`
}
Fridge2211356377Views []*Fridge2211356377View
)
func NewFridge2211356377View(e *ent.Fridge) *Fridge2211356377View {
if e == nil {
return nil
}
return &Fridge2211356377View{
ID: e.ID,
}
}
func NewFridge2211356377Views(es []*ent.Fridge) Fridge2211356377Views {
if len(es) == 0 {
return nil
}
r := make(Fridge2211356377Views, len(es))
for i, e := range es {
r[i] = NewFridge2211356377View(e)
}
return r
}
type (
// Item1548468123View represents the data serialized for the following serialization group combinations:
// []
Item1548468123View struct {
ID int `json:"id,omitempty"`
}
Item1548468123Views []*Item1548468123View
)
func NewItem1548468123View(e *ent.Item) *Item1548468123View {
if e == nil {
return nil
}
return &Item1548468123View{
ID: e.ID,
}
}
func NewItem1548468123Views(es []*ent.Item) Item1548468123Views {
if len(es) == 0 {
return nil
}
r := make(Item1548468123Views, len(es))
for i, e := range es {
r[i] = NewItem1548468123View(e)
}
return r
}

260
ent/http/update.go Normal file
View File

@ -0,0 +1,260 @@
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"t/ent"
"t/ent/compartment"
"t/ent/entry"
"t/ent/fridge"
"t/ent/item"
"github.com/go-chi/chi/v5"
"github.com/mailru/easyjson"
"go.uber.org/zap"
)
// Update updates a given ent.Compartment and saves the changes to the database.
func (h CompartmentHandler) Update(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Update"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
// Get the post data.
var d CompartmentUpdateRequest
if err := easyjson.UnmarshalFromReader(r.Body, &d); err != nil {
l.Error("error decoding json", zap.Error(err))
BadRequest(w, "invalid json string")
return
}
// Save the data.
b := h.client.Compartment.UpdateOneID(id)
// Store in database.
e, err := b.Save(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could-not-update-compartment", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
// Reload entry.
q := h.client.Compartment.Query().Where(compartment.ID(e.ID))
e, err = q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could-not-read-compartment", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("compartment rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewCompartment3324871446View(e), w)
}
// Update updates a given ent.Entry and saves the changes to the database.
func (h EntryHandler) Update(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Update"))
// ID is URL parameter.
var err error
id := chi.URLParam(r, "id")
// Get the post data.
var d EntryUpdateRequest
if err := easyjson.UnmarshalFromReader(r.Body, &d); err != nil {
l.Error("error decoding json", zap.Error(err))
BadRequest(w, "invalid json string")
return
}
// Validate the data.
errs := make(map[string]string)
if len(errs) > 0 {
l.Info("validation failed", zapFields(errs)...)
BadRequest(w, errs)
return
}
// Save the data.
b := h.client.Entry.UpdateOneID(id)
// Store in database.
e, err := b.Save(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.String("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.String("id", id))
BadRequest(w, msg)
default:
l.Error("could-not-update-entry", zap.Error(err), zap.String("id", id))
InternalServerError(w, nil)
}
return
}
// Reload entry.
q := h.client.Entry.Query().Where(entry.ID(e.ID))
e, err = q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.String("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.String("id", id))
BadRequest(w, msg)
default:
l.Error("could-not-read-entry", zap.Error(err), zap.String("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("entry rendered", zap.String("id", id))
easyjson.MarshalToHTTPResponseWriter(NewEntry2675665849View(e), w)
}
// Update updates a given ent.Fridge and saves the changes to the database.
func (h FridgeHandler) Update(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Update"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
// Get the post data.
var d FridgeUpdateRequest
if err := easyjson.UnmarshalFromReader(r.Body, &d); err != nil {
l.Error("error decoding json", zap.Error(err))
BadRequest(w, "invalid json string")
return
}
// Save the data.
b := h.client.Fridge.UpdateOneID(id)
// Store in database.
e, err := b.Save(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could-not-update-fridge", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
// Reload entry.
q := h.client.Fridge.Query().Where(fridge.ID(e.ID))
e, err = q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could-not-read-fridge", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("fridge rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewFridge2211356377View(e), w)
}
// Update updates a given ent.Item and saves the changes to the database.
func (h ItemHandler) Update(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Update"))
// ID is URL parameter.
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
l.Error("error getting id from url parameter", zap.String("id", chi.URLParam(r, "id")), zap.Error(err))
BadRequest(w, "id must be an integer")
return
}
// Get the post data.
var d ItemUpdateRequest
if err := easyjson.UnmarshalFromReader(r.Body, &d); err != nil {
l.Error("error decoding json", zap.Error(err))
BadRequest(w, "invalid json string")
return
}
// Save the data.
b := h.client.Item.UpdateOneID(id)
// Store in database.
e, err := b.Save(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could-not-update-item", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
// Reload entry.
q := h.client.Item.Query().Where(item.ID(e.ID))
e, err = q.Only(r.Context())
if err != nil {
switch {
case ent.IsNotFound(err):
msg := stripEntError(err)
l.Info(msg, zap.Error(err), zap.Int("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Int("id", id))
BadRequest(w, msg)
default:
l.Error("could-not-read-item", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("item rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewItem1548468123View(e), w)
}