-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.go
122 lines (99 loc) · 2.85 KB
/
orders.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
package main
import (
"context"
"time"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
"github.com/riku179/regisys-server/app"
"github.com/riku179/regisys-server/models"
"github.com/riku179/regisys-server/user"
)
// OrdersController implements the orders resource.
type OrdersController struct {
*goa.Controller
DB *models.OrdersDB
}
// NewOrdersController creates a orders controller.
func NewOrdersController(service *goa.Service) *OrdersController {
return &OrdersController{
Controller: service.NewController("OrdersController"),
}
}
// Add runs the add action.
func (c *OrdersController) Add(ctx *app.AddOrdersContext) error {
reqUser := user.FromContext(ctx)
// Registerじゃないとダメ
if reqUser.IsRegister == false {
return ctx.Forbidden()
}
item, err := ItemsDB.Get(ctx, ctx.Payload.ItemID)
if err == gorm.ErrRecordNotFound {
return ctx.NotFound()
}
order := models.OrdersFromAddOrderPayload(ctx.Payload)
order.UserID = reqUser.ID // レジ担当者ID
// メンバー価格の適用
if ctx.Payload.IsMemberPrice {
order.Price = item.MemberPrice
} else {
order.Price = item.Price
}
OrdersDB.Add(ctx, order)
return ctx.NoContent()
}
// Delete runs the delete action.
func (c *OrdersController) Delete(ctx *app.DeleteOrdersContext) error {
reqUser := user.FromContext(ctx)
if reqUser.IsRegister == false {
return ctx.Forbidden()
}
_, err := OrdersDB.Get(ctx, ctx.ID)
if err == gorm.ErrRecordNotFound {
// 指定された購入履歴が存在しない
return ctx.NotFound()
}
OrdersDB.Delete(ctx, ctx.ID)
return ctx.NoContent()
}
// Show runs the show action.
func (c *OrdersController) Show(ctx *app.ShowOrdersContext) error {
timeStart := time.Unix(int64(ctx.TimeStart), 0)
timeEnd := time.Unix(int64(ctx.TimeEnd), 0)
if ctx.User == nil {
// 全ユーザー
orders := []*models.Orders{}
OrdersDB.Db.Where(
"created_at >= ? AND created_at <= ?", timeStart, timeEnd,
).Find(&orders)
res := OrdersToRegisysOrders(ctx, orders)
return ctx.OK(res)
} else if _, err := UserDB.Get(ctx, *ctx.User); err == gorm.ErrRecordNotFound {
return ctx.NotFound()
}
// 指定ユーザーの取引のみ
orders := []*models.Orders{}
OrdersDB.Db.Where(
"user_id = ?", ctx.User,
).Where(
"created_at >= ? AND created_at <= ?", timeStart, timeEnd,
).Find(&orders)
res := OrdersToRegisysOrders(ctx, orders)
return ctx.OK(res)
}
// []*models.Orders -> []*app.RegisysOrders
func OrdersToRegisysOrders(ctx context.Context, orders []*models.Orders) app.RegisysOrdersCollection {
res := app.RegisysOrdersCollection{}
for _, v := range orders {
item, _ := ItemsDB.Get(ctx, v.ItemID)
res = append(res, &app.RegisysOrders{
ID: v.ID,
ItemID: v.ItemID,
Price: v.Price,
Quantity: v.Quantity,
UserID: v.UserID,
Datetime: int(v.CreatedAt.Unix()),
ItemName: item.ItemName,
})
}
return res
}