This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
/
read.go
118 lines (112 loc) · 3.75 KB
/
read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/mailru/easyjson"
"github.com/masseelch/elk/internal/fridge/ent"
"github.com/masseelch/elk/internal/fridge/ent/compartment"
"github.com/masseelch/elk/internal/fridge/ent/fridge"
"github.com/masseelch/elk/internal/fridge/ent/item"
"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(NewCompartment1151786357View(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(NewFridge2716213877View(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(NewItem1509516544View(e), w)
}