-
Notifications
You must be signed in to change notification settings - Fork 0
/
bill_router.go
199 lines (154 loc) · 4.79 KB
/
bill_router.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package v1
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/rojasuap/go-backend-api_facturacion/pkg/bill"
"github.com/rojasuap/go-backend-api_facturacion/pkg/response"
)
// PromotionRouter is the router of the promotions.
type BillRouter struct {
Repository bill.Repository
}
// CreateHandler Create a new bills.
func (pr *BillRouter) CreateHandler(w http.ResponseWriter, r *http.Request) {
var p bill.Bill
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
defer r.Body.Close()
ctx := r.Context()
err = pr.Repository.Create(ctx, &p)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
w.Header().Add("Location", fmt.Sprintf("%s%d", r.URL.String(), p.ID))
response.JSON(w, r, http.StatusCreated, response.Map{"bills": p})
}
// RootHandler - Returns all the available APIs
// @Summary This API can be used as health check for this application.
// @Description Tells if the chi-swagger APIs are working or not.
// @Tags chi-swagger
// @Accept json
// @Produce json
// @Success 200 {string} response "api response"
// @Router / [get]
func (pr *BillRouter) GetAllHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
bills, err := pr.Repository.GetAll(ctx)
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{"bills": bills})
}
// GetOneHandler response one bills by id.
func (pr *BillRouter) GetOneHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
ctx := r.Context()
p, err := pr.Repository.GetOne(ctx, uint(id))
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{"bill": p})
}
// GetAllHandler response all the bills.
func (pr *BillRouter) GetAllDaysHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
bills, err := pr.Repository.GetAllDays(ctx)
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{"bills": bills})
}
// GetAllHandler response all the bills.
func (pr *BillRouter) GetAllPaymentsHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
bills, err := pr.Repository.GetAllPayments(ctx)
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{"bills": bills})
}
// UpdateHandler update a stored bill by id.
func (pr *BillRouter) UpdateHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
var p bill.Bill
err = json.NewDecoder(r.Body).Decode(&p)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
defer r.Body.Close()
ctx := r.Context()
err = pr.Repository.Update(ctx, uint(id), p)
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, nil)
}
// DeleteHandler Remove a bill by ID.
func (pr *BillRouter) DeleteHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
ctx := r.Context()
err = pr.Repository.Delete(ctx, uint(id))
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{})
}
// GetByUserHandler response promotions by user id.
/**func (pr *PostRouter) GetByUserHandler(w http.ResponseWriter, r *http.Request) {
userIDStr := chi.URLParam(r, "userId")
userID, err := strconv.Atoi(userIDStr)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
ctx := r.Context()
promotions, err := pr.Repository.GetByUser(ctx, uint(userID))
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{"promotions": promotions})
}*/
// Routes returns bill router with each endpoint.
func (pr *BillRouter) Routes() http.Handler {
r := chi.NewRouter()
//r.Use(middleware.Authorizator)
//r.Get("/user/{userId}", pr.GetByUserHandler)
r.Get("/", pr.GetAllHandler)
r.Post("/", pr.CreateHandler)
r.Get("/{id}", pr.GetOneHandler)
r.Get("/dias", pr.GetAllDaysHandler)
r.Get("/totales", pr.GetAllPaymentsHandler)
r.Put("/{id}", pr.UpdateHandler)
r.Delete("/{id}", pr.DeleteHandler)
return r
}