-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.go
43 lines (35 loc) · 805 Bytes
/
patch.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
package apps
import (
"io"
"net/http"
"github.com/frohwerk/deputy-backend/pkg/api"
"github.com/frohwerk/deputy-backend/pkg/httputil"
"github.com/go-chi/chi"
)
func (h *handler) Patch(rw http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "app")
app, err := h.patch(id, r.Body)
if err != nil {
httputil.WriteErrorResponse(rw, err)
} else {
httputil.WriteJsonResponse(rw, app)
}
}
func (h *handler) patch(id string, body io.ReadCloser) (*api.App, error) {
attrs, err := tryDecode(body)
if err != nil {
return nil, err
}
app, err := h.apps.Get(id)
if err != nil {
return nil, err
}
if len(attrs.Name) > 0 {
app.Name = attrs.Name
}
result, err := h.apps.Update(app)
if err != nil {
return nil, err
}
return &api.App{Id: result.Id, Name: result.Name}, nil
}