/
views_reservation.go
73 lines (67 loc) · 1.96 KB
/
views_reservation.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
package marketplace
import (
"net/http"
"strconv"
"github.com/gocraft/web"
"qxklmrhx7qkzais6.onion/Tochka/tochka-free-market/modules/util"
)
func (c *Context) ActiveReservation(w web.ResponseWriter, r *web.Request) {
http.NotFound(w, r.Request)
}
func (c *Context) PreBookPackage(w web.ResponseWriter, r *web.Request) {
reservations := FindActiveReservationsForBuyer(c.ViewUser.Uuid)
if len(reservations) > 10 {
redirectUrl := "/user/active_reservation"
http.Redirect(w, r.Request, redirectUrl, 302)
return
}
groups := c.ViewItem.Item.PackagesWithoutReservation().GroupsTable()
for group := range groups {
if group.Sha256() == r.PathParams["hash"] {
c.GroupAvailability = &group
util.RenderTemplate(w, "reservation/pre_book", c)
return
}
}
http.NotFound(w, r.Request)
}
func (c *Context) BookPackage(w web.ResponseWriter, r *web.Request) {
transactionCount := 0
for _, t := range FindTransactionsForBuyer(c.ViewUser.Uuid) {
if t.CurrentPaymentStatus() == "PENDING" {
transactionCount += 1
}
}
if transactionCount > 10 {
redirectUrl := "/user/active_reservation"
http.Redirect(w, r.Request, redirectUrl, 302)
return
}
shippingId, _ := strconv.ParseInt(r.FormValue("shipping_id"), 10, 64)
shippingOption, _ := FindShippingOptionById(uint(shippingId))
quantity, err := strconv.ParseInt(r.FormValue("quantity"), 10, 64)
if err != nil {
quantity = int64(1)
}
groups := c.ViewItem.PackagesWithoutReservation().GroupsTable()
itemPackage, err := groups.GetPackageByHash(r.PathParams["hash"])
if err != nil {
http.Error(w, err.Error(), 500)
return
}
transaction, err := CreateTransactionForPackage(
*itemPackage,
*c.ViewUser.User,
r.FormValue("type"),
int(quantity),
shippingOption,
r.FormValue("shipping_address"),
)
if err != nil {
c.Error = "Unable to perform reservation"
c.PreBookPackage(w, r)
return
}
transaction.FundFromUserWallets(*c.ViewUser.User)
http.Redirect(w, r.Request, "/payments/"+transaction.Uuid, 302)
}