1
0
api/ent/http/update.go

261 lines
7.4 KiB
Go
Raw Normal View History

2022-08-31 06:18:14 +00:00
// 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)
}