/
orderController.go
202 lines (176 loc) · 5.82 KB
/
orderController.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package controllers
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/minhtran241/restaurant-management/database"
"github.com/minhtran241/restaurant-management/models"
)
var orderCollection *mongo.Collection = database.OpenCollection(database.Client, "order")
// GetOrders responds with the list of all orders as JSON.
// GetOrders godoc
// @Summary Get all orders
// @Description Responds with the list of all orders as JSON.
// @Tags orders
// @Produce json
// @Success 200 {array} models.Order
// @Router /orders [get]
func GetOrders() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
result, err := orderCollection.Find(ctx, bson.M{})
if err != nil {
c.JSON(
http.StatusInternalServerError,
gin.H{"error": "error occurred while listing orders"},
)
return
}
var allOrders []bson.M
if err = result.All(ctx, &allOrders); err != nil {
log.Fatal(err)
}
c.JSON(http.StatusOK, allOrders)
}
}
// GetOrder responds with the order with provided ID as JSON.
// GetOrder godoc
// @Summary Get single order by ID
// @Description Responds with the order with provided ID as JSON.
// @Tags orders
// @Produce json
// @Success 200 {object} models.Order
// @Router /orders/{order_id} [get]
func GetOrder() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
orderId := c.Param("order_id")
var order models.Order
err := orderCollection.FindOne(ctx, bson.M{"_id": orderId}).Decode(&order)
if err == mongo.ErrNoDocuments {
c.JSON(http.StatusNotFound, gin.H{"error": "order was not found"})
} else if err != nil {
c.JSON(
http.StatusInternalServerError,
gin.H{"error": "error occurred when fetching the order"},
)
return
}
c.JSON(http.StatusOK, order)
}
}
// CreateOrder takes a order JSON and store in DB.
// CreateOrder godoc
// @Summary Store a new order
// @Description Takes a order JSON and store in DB. Return saved JSON.
// @Tags orders
// @Produce json
// @Success 200 {object} models.Order
// @Router /orders [post]
func CreateOrder() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var table models.Table
var order models.Order
if err := c.BindJSON(&order); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
validationErr := validate.Struct(order)
if validationErr != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": validationErr.Error()})
return
}
if order.Table_id != nil {
err := tableCollection.FindOne(ctx, bson.M{"table_id": order.Table_id}).Decode(&table)
if err == mongo.ErrNoDocuments {
c.JSON(http.StatusNotFound, gin.H{"error": "table was not found"})
return
} else if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}
order.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
order.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
order.ID = primitive.NewObjectID()
order.Order_id = order.ID.Hex()
result, insertErr := orderCollection.InsertOne(ctx, order)
if insertErr != nil {
msg := fmt.Sprintf("Failed to created order")
c.JSON(http.StatusInternalServerError, msg)
return
}
c.JSON(http.StatusOK, result)
}
}
// UpdateOrder takes a order JSON and update order stored in DB.
// UpdateOrder godoc
// @Summary Update a order
// @Description Takes a order JSON and update order stored in DB. Return saved JSON.
// @Tags orders
// @Produce json
// @Success 200 {object} models.Order
// @Router /orders/{order_id} [patch]
func UpdateOrder() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var table models.Table
var order models.Order
orderId := c.Param("order_id")
if err := c.BindJSON(&order); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var updateObj primitive.D
if order.Table_id != nil {
err := tableCollection.FindOne(ctx, bson.M{"table_id": order.Table_id}).Decode(&table)
if err != nil {
msg := fmt.Sprintf("Table was not found")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
updateObj = append(updateObj, bson.E{Key: "table_id", Value: order.Table_id})
}
order.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
updateObj = append(updateObj, bson.E{Key: "updated_at", Value: order.Updated_at})
upsert := true
filter := bson.M{"order_id": orderId}
opt := options.UpdateOptions{
Upsert: &upsert,
}
result, err := orderCollection.UpdateOne(
ctx,
filter,
bson.D{{Key: "$set", Value: updateObj}},
&opt,
)
if err != nil {
msg := "Failed to update the order"
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
c.JSON(http.StatusOK, result)
}
}
func OrderItemOrderCreator(order models.Order) string {
order.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
order.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
order.ID = primitive.NewObjectID()
order.Order_id = order.ID.Hex()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
orderCollection.InsertOne(ctx, order)
return order.Order_id
}