-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify.go
39 lines (32 loc) · 842 Bytes
/
modify.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
package users
import (
"encoding/json"
"github.com/go-chi/chi"
"net/http"
"strconv"
domain "github.com/eduardoraider/go-box/internal/users"
)
func (h *handler) Modify(rw http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
u, err := h.factory.RestoreOne(int64(id))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
u, err = domain.DecodeAndModify(r.Body, u)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
err = h.repo.Update(int64(id), u)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.Header().Add("Content-Type", "application/json")
json.NewEncoder(rw).Encode(u)
}