-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
208 lines (183 loc) · 5.22 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
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
203
204
205
206
207
208
package models
import (
"time"
"github.com/graphql-go/graphql"
)
/*
* This file contains the definitions of the order made by a user
*/
//Order made by the user
type Order struct {
ID uint `gorm:"primary_key"`
Name string //The Order name to be displayed in the frontend
Items []Item `gorm:"foreign_key:OrderID"` //Items is the list of items in the cart
Total float64 //Total price of items in the cart
Date time.Time //Date on which the order was made
UserID uint
}
//OrderConfig is the config for the Order object
var OrderConfig = graphql.ObjectConfig{
Name: "Order",
Fields: graphql.Fields{
"ID": &graphql.Field{
Type: graphql.Int,
},
"Name": &graphql.Field{
Type: graphql.String,
},
"Items": &graphql.Field{
Type: graphql.NewList(ItemSchema),
Description: "Getting the list of items",
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
//Getting the source Order
order, ok := params.Source.(Order)
if !ok {
return nil, nil
}
//Getting the Items from DB
DB.First(&order).Related(&order.Items, "Items")
return order.Items, nil
},
},
"Total": &graphql.Field{
Type: graphql.Float,
},
"Date": &graphql.Field{
Type: graphql.DateTime,
},
},
}
//OrderSchema is the schema of the Order model
var OrderSchema = graphql.NewObject(OrderConfig)
//ReadOrder will read a Order given an OrderID
var ReadOrder = &graphql.Field{
Type: OrderSchema, //the return of this field
Description: "Get a single Order and its detail",
Args: graphql.FieldConfigArgument{
"ID": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.Int),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
//marshall and cast the argument value
id, _ := params.Args["ID"].(int)
order := Order{ID: uint(id)}
//Finding the order from DB
DB.First(&order)
// return the new order object that we supposedly have in DB
return order, nil
},
}
//CreateOrderArgumentConfig is argument config required for the Order
var CreateOrderArgumentConfig = graphql.FieldConfigArgument{
"Name": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"Total": &graphql.ArgumentConfig{
Type: graphql.Float,
},
"Date": &graphql.ArgumentConfig{
Type: graphql.DateTime,
},
}
//CreateOrder for creating an Order
var CreateOrder = &graphql.Field{
Type: OrderSchema, // the return type for this field
Description: "Create new Order",
Args: CreateOrderArgumentConfig,
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
// marshall and cast the argument value
name, _ := params.Args["Name"].(string)
total, okt := params.Args["Total"]
date, okd := params.Args["Date"]
// perform mutation operation here
// for e.g. create an Order and save to DB.
newOrder := Order{Name: name}
if okt {
newOrder.Total = total.(float64)
}
if okd {
newOrder.Date = date.(time.Time)
} else {
//If Date not given then adding today's date
newOrder.Date = time.Now()
}
//Creating the Order in the DB
DB.Create(&newOrder)
// return the new Product object that we supposedly save to DB
return newOrder, nil
},
}
//UpdateOrderArgumentConfig is argument config required update of the Order
var UpdateOrderArgumentConfig = graphql.FieldConfigArgument{
"ID": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.Int),
},
"Name": &graphql.ArgumentConfig{
Type: graphql.String,
},
"Total": &graphql.ArgumentConfig{
Type: graphql.Float,
},
"Date": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.DateTime), //The format of DateTime at frontend is "2017-10-06T03:40:00.000Z"
},
"AddItem": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"RemoveItem": &graphql.ArgumentConfig{
Type: graphql.Int,
},
}
//UpdateOrder is for updating an Order
var UpdateOrder = &graphql.Field{
Type: OrderSchema, //the return type for this field
Description: "Update existing Order",
Args: UpdateOrderArgumentConfig,
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
//marshall and cast the argument
id, _ := params.Args["ID"].(int)
name, okn := params.Args["Name"]
total, okt := params.Args["Total"]
date, _ := params.Args["Date"]
addItem, okai := params.Args["AddItem"]
removeItem, okri := params.Args["RemoveItem"]
// perform mutation operation here
// will get the Order from db
// update the fields and update in the db
order := Order{
ID: uint(id),
Date: date.(time.Time),
}
if okn {
order.Name = name.(string)
}
if okt {
order.Total = total.(float64)
}
//Getting all the items
items := []Item{}
DB.Model(&order).Related(&items, "Items")
order.Items = items
if okai {
i := Item{ID: uint(addItem.(int))}
DB.First(&i)
order.Items = append(order.Items, i)
}
if okri {
item := Item{ID: uint(removeItem.(int))}
for i := len(order.Items) - 1; i >= 0; i++ {
if order.Items[i].ID != item.ID {
continue
}
copy(order.Items[i:], order.Items[i+1:])
order.Items = order.Items[:len(order.Items)-1]
DB.Model(&order).Association("Items").Delete(&item)
break
}
}
DB.Save(&order)
//return the updated order that we supposedly saved to the DB
return order, nil
},
}