-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
145 lines (119 loc) · 3.65 KB
/
order.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
package controller
import (
"net/http"
"strconv"
"github.com/ardafirdausr/kaseer/internal"
"github.com/ardafirdausr/kaseer/internal/app"
"github.com/ardafirdausr/kaseer/internal/entity"
"github.com/labstack/echo/v4"
)
type OrderController struct {
orderUc internal.OrderUsecase
productUc internal.ProductUsecase
}
func NewOrderController(ucs *app.Usecases) *OrderController {
orderUc := ucs.OrderUsecase
productUc := ucs.ProductUsecase
return &OrderController{orderUc, productUc}
}
func (oc OrderController) ShowAllOrders(c echo.Context) error {
ctx := c.Request().Context()
orders, err := oc.orderUc.GetAllOrders(ctx)
if err != nil {
return err
}
data := echo.Map{"Orders": orders}
return renderPage(c, "orders", "All Orders", data)
}
func (oc OrderController) ShowCreateOrderForm(c echo.Context) error {
ctx := c.Request().Context()
products, err := oc.productUc.GetAllProducts(ctx)
if err != nil {
return err
}
data := echo.Map{"Products": products}
return renderPage(c, "order_create", "Create New Order", data)
}
func (oc OrderController) GetTotalOrdersData(c echo.Context) error {
ctx := c.Request().Context()
orderType := c.FormValue("type")
var totalOrderCount int
switch orderType {
case "day":
dailyOrder, err := oc.orderUc.GetDailyOrderCount(ctx)
if err != nil {
return echo.ErrInternalServerError
}
totalOrderCount = dailyOrder
default:
totalOrder, err := oc.orderUc.GetTotalOrderCount(ctx)
if err != nil {
return echo.ErrInternalServerError
}
totalOrderCount = totalOrder
}
return responseJson(c, http.StatusOK, "Success", totalOrderCount)
}
func (oc OrderController) GetLatestIncomeData(c echo.Context) error {
ctx := c.Request().Context()
orderType := c.FormValue("type")
var totalIncome int
switch orderType {
case "day":
dailyIncome, err := oc.orderUc.GetLastDayIncome(ctx)
if err != nil {
return echo.ErrInternalServerError
}
totalIncome = dailyIncome
case "month":
monthlyIncome, err := oc.orderUc.GetLastMonthIncome(ctx)
if err != nil {
return echo.ErrInternalServerError
}
totalIncome = monthlyIncome
}
return responseJson(c, http.StatusOK, "Success", totalIncome)
}
func (oc OrderController) GetAnnualIncomeData(c echo.Context) error {
ctx := c.Request().Context()
annualIncomes, err := oc.orderUc.GetAnnualIncome(ctx)
if err != nil {
return err
}
return responseJson(c, http.StatusOK, "Success", annualIncomes)
}
func (oc OrderController) GetOrderDetailData(c echo.Context) error {
paramOrderID := c.Param("orderId")
orderID, err := strconv.ParseInt(paramOrderID, 10, 64)
if err != nil {
return echo.ErrInternalServerError
}
ctx := c.Request().Context()
orderItems, err := oc.orderUc.GetOrderItems(ctx, orderID)
if err != nil {
return err
}
return responseJson(c, http.StatusOK, "Success", orderItems)
}
func (oc OrderController) CreateOrder(c echo.Context) error {
var orderParam entity.CreateOrderParam
if err := c.Bind(&orderParam); err != nil {
return responseJson(c, http.StatusInternalServerError, "Failed processing data", nil)
}
err := c.Validate(&orderParam)
if ev, ok := err.(entity.ErrValidation); ok {
return responseJson(c, http.StatusBadRequest, "Invalid data", ev.Errors)
}
if err != nil {
return responseJson(c, http.StatusBadRequest, "Invalid data", nil)
}
ctx := c.Request().Context()
order, err := oc.orderUc.Create(ctx, orderParam)
if ev, ok := err.(entity.ErrValidation); ok {
return responseErrorJson(c, http.StatusBadRequest, ev.Message, ev.Errors)
}
if err != nil {
return responseJson(c, http.StatusInternalServerError, "Failed creating data", nil)
}
return responseJson(c, http.StatusCreated, "Success creating order", order)
}