/
order.go
150 lines (121 loc) · 3.6 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
146
147
148
149
150
package usecase
import (
"context"
"fmt"
"log"
"github.com/ardafirdausr/kaseer/internal"
"github.com/ardafirdausr/kaseer/internal/entity"
)
type OrderUsecase struct {
orderRepository internal.OrderRepository
productRepository internal.ProductRepository
UnitOfWork internal.UnitOfWork
}
func NewOrderUsecase(
orderRepository internal.OrderRepository,
productRepository internal.ProductRepository,
UnitOfWork internal.UnitOfWork) *OrderUsecase {
return &OrderUsecase{orderRepository, productRepository, UnitOfWork}
}
func (ou OrderUsecase) GetAllOrders(ctx context.Context) ([]*entity.Order, error) {
orders, err := ou.orderRepository.GetAllOrders(ctx)
if err != nil {
log.Println(err.Error())
}
return orders, err
}
func (ou OrderUsecase) GetOrderItems(ctx context.Context, orderID int64) ([]*entity.OrderItem, error) {
orderItems, err := ou.orderRepository.GetOrderItemsByID(ctx, orderID)
if err != nil {
log.Println(err.Error())
}
return orderItems, err
}
func (ou OrderUsecase) GetAnnualIncome(ctx context.Context) ([]*entity.AnnualIncome, error) {
annualIncomes, err := ou.orderRepository.GetAnnualIncome(ctx)
if err != nil {
log.Println(err.Error())
}
return annualIncomes, err
}
func (ou OrderUsecase) GetDailyOrderCount(ctx context.Context) (int, error) {
res, err := ou.orderRepository.GetDailyOrderCount(ctx)
if err != nil {
log.Println(err.Error())
}
return res, err
}
func (ou OrderUsecase) GetTotalOrderCount(ctx context.Context) (int, error) {
res, err := ou.orderRepository.GetTotalOrderCount(ctx)
if err != nil {
log.Println(err.Error())
}
return res, err
}
func (ou OrderUsecase) GetLastDayIncome(ctx context.Context) (int, error) {
res, err := ou.orderRepository.GetLastDayIncome(ctx)
if err != nil {
log.Println(err.Error())
}
return res, err
}
func (ou OrderUsecase) GetLastMonthIncome(ctx context.Context) (int, error) {
res, err := ou.orderRepository.GetLastMonthIncome(ctx)
if err != nil {
log.Println(err.Error())
}
return res, err
}
func (ou OrderUsecase) Create(ctx context.Context, param entity.CreateOrderParam) (*entity.Order, error) {
// check available quantity
orderQuantity := make(map[int64]int)
productSale := make(map[int64]int)
productIDs := make([]int64, 0)
for _, item := range param.Items {
orderQuantity[item.ProductID] = item.Quantity
productSale[item.ProductID] = item.Quantity
productIDs = append(productIDs, item.ProductID)
}
products, err := ou.productRepository.GetProductsByIDs(ctx, productIDs...)
if err != nil {
return nil, err
}
ev := entity.ErrValidation{
Message: "Insufficient product quantity",
Errors: map[string]string{},
}
for _, product := range products {
if product.Stock-orderQuantity[product.ID] < 0 {
ev.Errors[product.Name] = fmt.Sprintf("%s remaining quantity: %d", product.Name, product.Stock)
}
}
if len(ev.Errors) > 0 {
return nil, ev
}
txContext, err := ou.UnitOfWork.Begin(ctx)
if err != nil {
log.Println(err.Error())
return nil, err
}
order, err := ou.orderRepository.Create(txContext, param)
if err != nil {
log.Println(err.Error())
ou.UnitOfWork.Rollback(txContext)
return nil, err
}
if err := ou.orderRepository.CreateOrderItems(txContext, order.ID, param.Items); err != nil {
log.Println(err.Error())
ou.UnitOfWork.Rollback(txContext)
return nil, err
}
if err := ou.productRepository.DecrementProductByIDs(txContext, productSale); err != nil {
log.Println(err.Error())
ou.UnitOfWork.Rollback(txContext)
return nil, err
}
if err := ou.UnitOfWork.Commit(txContext); err != nil {
log.Println(err.Error())
return nil, err
}
return order, nil
}