-
Notifications
You must be signed in to change notification settings - Fork 0
/
createAuthor.go
60 lines (55 loc) · 1.63 KB
/
createAuthor.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
package handlers
import (
"encoding/json"
"fmt"
"net/http"
"github.com/saromanov/knowledge/internal/models/convert"
restModel "github.com/saromanov/knowledge/internal/models/rest"
"github.com/saromanov/knowledge/internal/rest/response"
"github.com/saromanov/knowledge/internal/storage"
"github.com/go-chi/render"
"github.com/sirupsen/logrus"
)
type createAuthorHandler struct {
store storage.Storage
}
// NewCreateAuthorHandler provides creating of the author
func NewCreateAuthorHandler(st storage.Storage) *createAuthorHandler {
return &createAuthorHandler{
store: st,
}
}
func (h *createAuthorHandler) Handle(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := logrus.New().WithContext(ctx)
var st restModel.Author
if err := json.NewDecoder(r.Body).Decode(&st); err != nil {
log.WithError(err).Error("unable to parse request")
response.WriteError(w, r, http.StatusBadRequest, restModel.Error{
Message: "unable to decode request",
})
return
}
id, err := h.store.CreateAuthor(ctx, convert.RestAuthorToStorageAuthor(&st))
if err != nil {
log.WithError(err).Error("unable to create author")
response.WriteError(w, r, http.StatusInternalServerError, restModel.Error{
Message: fmt.Sprintf("unable to create author: %v", err),
})
return
}
st.ID = fmt.Sprintf("%d", id)
out, err := json.Marshal(st)
if err != nil {
text := "unable to marshal response"
log.WithError(err).Error(text)
response.WriteError(w, r, http.StatusInternalServerError, restModel.Error{
Message: text,
})
return
}
w.WriteHeader(http.StatusCreated)
render.JSON(w, r, restModel.Response{
Data: (json.RawMessage)(out),
})
}