forked from masseelch/elk
/
update.go
77 lines (73 loc) · 2.07 KB
/
update.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
// 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/chestarss/elk/internal/uuid/ent"
"github.com/chestarss/elk/internal/uuid/ent/user"
"go.uber.org/zap"
)
// Update updates a given ent.User and saves the changes to the database.
func (h UserHandler) 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 UserUpdateRequest
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.User.UpdateOneID(id)
if d.UUID != nil {
b.SetUUID(*d.UUID)
}
// 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-user", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
// Reload entry.
q := h.client.User.Query().Where(user.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-user", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("user rendered", zap.Int("id", id))
easyjson.MarshalToHTTPResponseWriter(NewUser3451555716View(e), w)
}