-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_handler.go
171 lines (149 loc) · 4.95 KB
/
book_handler.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package app
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/abhinav812/cloudy-bookstore/internal/model"
"github.com/abhinav812/cloudy-bookstore/internal/repository"
"github.com/go-chi/chi"
"gorm.io/gorm"
)
const (
appErrDataAccessFailure = "data access failure"
appErrJSONCreationFailure = "json creation failure"
appErrDataCreationFailure = "data creation failure"
appErrFormDecodingFailure = "form decoding failure"
appErrDataUpdateFailure = "data update failure"
)
//HandleListBooks - http route to list all the books. Returns result in form of model.BookDtos
func (s *Server) HandleListBooks(w http.ResponseWriter, _ *http.Request) {
books, err := repository.ListBooks(s.db)
if err != nil {
s.Logger().Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"error": "%v""}`, appErrDataAccessFailure)
return
}
if books == nil {
_, _ = fmt.Fprintf(w, "[]")
return
}
dtos := books.ToDto()
if err := json.NewEncoder(w).Encode(dtos); err != nil {
s.Logger().Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"error": "%v""}`, appErrJSONCreationFailure)
return
}
/*w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("[]"))*/
}
//HandleCreateBook - http route to create new book, with supplied model.Book json
func (s *Server) HandleCreateBook(w http.ResponseWriter, r *http.Request) {
book := &model.Book{}
if err := json.NewDecoder(r.Body).Decode(book); err != nil {
s.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusUnprocessableEntity)
_, _ = fmt.Fprintf(w, `{"error": "%v"}`, appErrFormDecodingFailure)
return
}
resp, err := repository.CreateBook(s.db, book)
if err != nil {
s.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"error": "%v"}`, appErrDataCreationFailure)
return
}
s.Logger().Info().Msgf("New book created. ID: %d", resp.ID)
w.WriteHeader(http.StatusCreated)
}
//HandleReadBook - http route to handle book by id request. Returns result in form of model.BookDto json
func (s *Server) HandleReadBook(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 0, 64)
if err != nil {
s.logger.Info().Msgf("can not parse ID: %v", id)
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
if id == 0 {
s.logger.Info().Msg("id value has to be greater that 0")
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
book, err := repository.ReadBook(s.db, uint(id))
if err != nil {
if err == gorm.ErrRecordNotFound {
w.WriteHeader(http.StatusNotFound)
return
}
s.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"error": "%v"}`, appErrDataAccessFailure)
return
}
dto := book.ToDto()
if err := json.NewEncoder(w).Encode(dto); err != nil {
s.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"error": "%v"}`, appErrJSONCreationFailure)
return
}
w.WriteHeader(http.StatusOK)
}
//HandleUpdateBook - http route to handle update book based on id and supplied model.Book json payload
func (s *Server) HandleUpdateBook(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 0, 64)
if err != nil {
s.logger.Info().Msgf("can not parse ID: %v", id)
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
if id == 0 {
s.logger.Info().Msg("id value has to be greater that 0")
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
book := &model.Book{}
if err := json.NewDecoder(r.Body).Decode(book); err != nil {
s.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusUnprocessableEntity)
_, _ = fmt.Fprintf(w, `{"error": "%v"}`, appErrFormDecodingFailure)
return
}
book.ID = uint(id)
if err := repository.UpdateBook(s.db, book); err != nil {
if err == gorm.ErrRecordNotFound {
w.WriteHeader(http.StatusNotFound)
return
}
s.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"error": "%v"}`, appErrDataUpdateFailure)
return
}
s.logger.Info().Msgf("Book updated: %d", id)
w.WriteHeader(http.StatusAccepted)
}
//HandleDeleteBook - http request to handle delete book request by bookId
func (s *Server) HandleDeleteBook(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 0, 64)
if err != nil {
s.logger.Info().Msgf("can not parse ID: %v", id)
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
if id == 0 {
s.logger.Info().Msg("id value has to be greater that 0")
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
if err := repository.DeleteBook(s.db, uint(id)); err != nil {
s.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"error": "%v"}`, appErrDataAccessFailure)
return
}
s.logger.Info().Msgf("Book deleted: %d", id)
w.WriteHeader(http.StatusAccepted)
}