-
Notifications
You must be signed in to change notification settings - Fork 0
/
purchaseController.go
153 lines (116 loc) · 3.36 KB
/
purchaseController.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
package main
import (
"net/http"
"net/url"
"strconv"
"github.com/asvins/router/errors"
"github.com/asvins/warehouse/models"
)
func FillPurchaseIdWithUrlValue(p *models.Purchase, params url.Values) error {
id, err := strconv.Atoi(params.Get("id"))
if err != nil {
return err
}
p.ID = id
return nil
}
func retreivePurchase(w http.ResponseWriter, r *http.Request) errors.Http {
purchase := &models.Purchase{}
if err := BuildStructFromQueryString(purchase, r.URL.Query()); err != nil {
return errors.BadRequest(err.Error())
}
purchases, err := purchase.Retreive(db)
if err != nil {
return errors.InternalServerError(err.Error())
}
if len(purchases) == 0 {
return errors.NotFound("record not found")
}
rend.JSON(w, http.StatusOK, purchases)
return nil
}
func retreivePurchaseById(w http.ResponseWriter, r *http.Request) errors.Http {
params := r.URL.Query()
purchase := models.Purchase{}
id, err := strconv.Atoi(params.Get("id"))
if err != nil {
return errors.BadRequest(err.Error())
}
purchase.ID = id
purchases, err := purchase.Retreive(db)
if err != nil {
return errors.InternalServerError(err.Error())
}
if len(purchases) != 1 {
return errors.NotFound("record not found")
}
rend.JSON(w, http.StatusOK, purchases[0])
return nil
}
func retreivePurchaseByOrderId(w http.ResponseWriter, r *http.Request) errors.Http {
params := r.URL.Query()
purchase := models.Purchase{}
orderId, err := strconv.Atoi(params.Get("order_id"))
if err != nil {
return errors.BadRequest(err.Error())
}
purchase.OrderId = orderId
purchases, err := purchase.Retreive(db)
if err != nil {
return errors.InternalServerError(err.Error())
}
if len(purchases) != 1 {
return errors.NotFound("record not found")
}
rend.JSON(w, http.StatusOK, purchases[0])
return nil
}
func retreiveOpenPurchase(w http.ResponseWriter, r *http.Request) errors.Http {
purchase := models.Purchase{}
purchs, err := purchase.RetreiveOpen(db)
if err != nil {
return errors.InternalServerError(err.Error())
}
rend.JSON(w, http.StatusOK, purchs)
return nil
}
func retreiveConfirmedPurchases(w http.ResponseWriter, r *http.Request) errors.Http {
purchase := models.Purchase{}
purchs, err := purchase.RetreiveConfirmed(db)
if err != nil {
return errors.InternalServerError(err.Error())
}
rend.JSON(w, http.StatusOK, purchs)
return nil
}
func retreiveConcludedPurchases(w http.ResponseWriter, r *http.Request) errors.Http {
purchase := models.Purchase{}
purchs, err := purchase.RetreiveConcluded(db)
if err != nil {
return errors.InternalServerError(err.Error())
}
rend.JSON(w, http.StatusOK, purchs)
return nil
}
func confirmPurchase(w http.ResponseWriter, r *http.Request) errors.Http {
var purchase models.Purchase
if err := FillPurchaseIdWithUrlValue(&purchase, r.URL.Query()); err != nil {
return errors.BadRequest(err.Error())
}
if err := purchase.Confirm(db); err != nil {
return errors.InternalServerError(err.Error())
}
rend.JSON(w, http.StatusOK, purchase.ID)
return nil
}
func concludePurchase(w http.ResponseWriter, r *http.Request) errors.Http {
var purchase models.Purchase
if err := FillPurchaseIdWithUrlValue(&purchase, r.URL.Query()); err != nil {
return errors.BadRequest(err.Error())
}
if err := purchase.Conclude(db); err != nil {
return errors.InternalServerError(err.Error())
}
rend.JSON(w, http.StatusOK, purchase.ID)
return nil
}