forked from masseelch/elk
/
delete.go
118 lines (111 loc) · 3.45 KB
/
delete.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/google/uuid"
"github.com/chestarss/elk/internal/client_gen/ent"
"go.uber.org/zap"
)
// Delete removes a ent.Category from the database.
func (h CategoryHandler) Delete(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Delete"))
// 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)
err = h.client.Category.DeleteOneID(id).Exec(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)
default:
l.Error("could-not-delete-category", zap.Error(err), zap.Uint64("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("category deleted", zap.Uint64("id", id))
w.WriteHeader(http.StatusNoContent)
}
// Delete removes a ent.Collar from the database.
func (h CollarHandler) 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.Collar.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-collar", zap.Error(err), zap.Int("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("collar deleted", zap.Int("id", id))
w.WriteHeader(http.StatusNoContent)
}
// Delete removes a ent.Owner from the database.
func (h OwnerHandler) Delete(w http.ResponseWriter, r *http.Request) {
l := h.log.With(zap.String("method", "Delete"))
// 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
}
err = h.client.Owner.DeleteOneID(uuid.UUID(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.String()))
NotFound(w, msg)
default:
l.Error("could-not-delete-owner", zap.Error(err), zap.String("id", id.String()))
InternalServerError(w, nil)
}
return
}
l.Info("owner deleted", zap.String("id", id.String()))
w.WriteHeader(http.StatusNoContent)
}
// Delete removes a ent.Pet from the database.
func (h PetHandler) 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.Pet.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-pet", zap.Error(err), zap.String("id", id))
InternalServerError(w, nil)
}
return
}
l.Info("pet deleted", zap.String("id", id))
w.WriteHeader(http.StatusNoContent)
}