forked from masseelch/elk
/
read.go
159 lines (152 loc) · 5.11 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/mailru/easyjson"
"github.com/chestarss/elk/internal/client_gen/ent"
"github.com/chestarss/elk/internal/client_gen/ent/category"
collar "github.com/chestarss/elk/internal/client_gen/ent/collar"
"github.com/chestarss/elk/internal/client_gen/ent/owner"
"github.com/chestarss/elk/internal/client_gen/ent/pet"
"go.uber.org/zap"
)
// Read fetches the ent.Category identified by a given url-parameter from the
// database and renders it to the client.
func (h *CategoryHandler) Read(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Read"))
// ID is URL parameter.
id64, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 0)
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 greater zero")
return
}
id := uint64(id64)
// Create the query to fetch the Category
q := h.client.Category.Query().Where(category.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.Uint64("id", id))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.Uint64("id", id))
BadRequest(w, msg)
default:
l.Error("could not read category", zap.Error(err), zap.Uint64("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("category rendered", zap.Uint64("id", id))
easyjson.MarshalToHTTPResponseWriter(NewCategory4094953247View(e), w)
}
// Read fetches the ent.Collar identified by a given url-parameter from the
// database and renders it to the client.
func (h *CollarHandler) 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 Collar
q := h.client.Collar.Query().Where(collar.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 collar", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("collar rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewCollar1522160880View(e), w)
}
// Read fetches the ent.Owner identified by a given url-parameter from the
// database and renders it to the client.
func (h *OwnerHandler) Read(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Read"))
// ID is URL parameter.
id, err := uuid.Parse(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 a valid UUID")
return
}
// Create the query to fetch the Owner
q := h.client.Owner.Query().Where(owner.ID(uuid.UUID(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.String()))
NotFound(w, msg)
case ent.IsNotSingular(err):
msg := stripEntError(err)
l.Error(msg, zap.Error(err), zap.String("id", id.String()))
BadRequest(w, msg)
default:
l.Error("could not read owner", zap.Error(err), zap.String("id", id.String()))
InternalServerError(w, nil)
}
return
}
l.Info("owner rendered", zap.String("id", id.String()))
easyjson.MarshalToHTTPResponseWriter(NewOwner139708381View(e), w)
}
// Read fetches the ent.Pet identified by a given url-parameter from the
// database and renders it to the client.
func (h *PetHandler) 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 Pet
q := h.client.Pet.Query().Where(pet.ID(id))
// Eager load edges that are required on read operation.
q.WithOwner().WithFriends(func(q *ent.PetQuery) {
q.WithOwner().WithFriends(func(q *ent.PetQuery) {
q.WithOwner().WithFriends(func(q *ent.PetQuery) {
q.WithOwner()
})
})
})
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 pet", zap.Error(err), zap.String("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("pet rendered", zap.String("id", id))
easyjson.MarshalToHTTPResponseWriter(NewPet1876743790View(e), w)
}