-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.create.go
71 lines (55 loc) · 1.6 KB
/
handlers.create.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
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/go-chi/chi"
"github.com/gofrs/uuid"
log "github.com/sirupsen/logrus"
"go.jlucktay.dev/rest-api/pkg/storage"
)
func (s *Server) createPayments() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.ContentLength == 0 {
log.Debug("Request body was empty, returning.")
http.Error(w, "Empty request body.", http.StatusBadRequest) // 400
return
}
bodyBytes, errRead := ioutil.ReadAll(r.Body)
if errRead != nil {
log.Fatal(errRead)
}
defer r.Body.Close()
var p storage.Payment
errUm := json.Unmarshal(bodyBytes, &p)
if errUm != nil {
log.Fatal(errUm)
}
id, errCreate := s.Storage.Create(p)
if errCreate != nil {
log.Fatal(errCreate)
}
w.Header().Set("Location", fmt.Sprintf("/v1/payments/%s", id))
w.WriteHeader(http.StatusCreated) // 201
}
}
func (s *Server) createPaymentByID() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := uuid.FromStringOrNil(chi.URLParam(r, "id"))
if id == uuid.Nil {
log.Debug("ID was invalid, returning.")
http.Error(w, "Invalid ID.", http.StatusNotFound) // 404
return
}
_, errRead := s.Storage.Read(id)
if errRead == nil {
log.Debug("ID already existed and was found, returning.")
http.Error(w, (&storage.AlreadyExistsError{ID: id}).Error(), http.StatusConflict) // 409
return
}
log.Debug("ID was given but should not have been, returning.")
http.Error(w, `Cannot specify an ID for payment creation.
One will be generated for you.`, http.StatusNotFound) // 404
}
}