/
handlers.go
56 lines (45 loc) · 1.6 KB
/
handlers.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
package account
import (
"net/http"
"github.com/dfang/qor-demo/models/orders"
"github.com/dfang/qor-demo/models/users"
"github.com/dfang/qor-demo/utils"
"github.com/qor/render"
)
// Controller products controller
type Controller struct {
View *render.Render
}
// Profile profile show page
func (ctrl Controller) Profile(w http.ResponseWriter, req *http.Request) {
var (
currentUser = utils.GetCurrentUser(req)
tx = utils.GetDB(req)
billingAddress, shippingAddress users.Address
)
// TODO refactor
tx.Model(currentUser).Related(¤tUser.Addresses, "Addresses")
tx.Model(currentUser).Related(&billingAddress, "DefaultBillingAddress")
tx.Model(currentUser).Related(&shippingAddress, "DefaultShippingAddress")
ctrl.View.Execute("profile", map[string]interface{}{
"CurrentUser": currentUser, "DefaultBillingAddress": billingAddress, "DefaultShippingAddress": shippingAddress,
}, req, w)
}
// Orders orders page
func (ctrl Controller) Orders(w http.ResponseWriter, req *http.Request) {
var (
Orders []orders.Order
currentUser = utils.GetCurrentUser(req)
tx = utils.GetDB(req)
)
tx.Preload("OrderItems").Where("state <> ? AND state != ?", orders.DraftState, "").Where(&orders.Order{UserID: ¤tUser.ID}).Find(&Orders)
ctrl.View.Execute("orders", map[string]interface{}{"Orders": Orders}, req, w)
}
// Update update profile page
func (ctrl Controller) Update(w http.ResponseWriter, req *http.Request) {
// FIXME
}
// AddCredit add credit
func (ctrl Controller) AddCredit(w http.ResponseWriter, req *http.Request) {
// FIXME
}