Skip to content

Commit 20da4ab

Browse files
committed
modified order and going for mad stuff
1 parent c59a190 commit 20da4ab

File tree

8 files changed

+134
-19
lines changed

8 files changed

+134
-19
lines changed

brokers/brokers.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type CokutBroker interface {
2222
GetMealsByRestaurant(rid string) (l []interface{}, err error)
2323
GetSpecialMeals() (l []interface{}, err error)
2424
GetSpiceyMeals() (l []interface{}, err error)
25-
CreateOrder(o *models.Order) (id string, err error)
25+
CreateOrder(o *models.Order) (po *models.Order, err error)
2626
GetAllOrders() (l []interface{}, err error)
2727
GetOrdersByUser(uid string) (l []interface{}, err error)
2828
}
@@ -34,6 +34,7 @@ type DbBroker interface {
3434
Add(collectionName string, i interface{}) (id string, err error)
3535
DeleteOne(collectionName string, i interface{}) (n int64, err error)
3636
Get(collectionName string, i interface{}) (l []interface{}, err error)
37+
GetMultipleByID(collectionName string, model interface{}, ids []string) (l []interface{}, err error)
3738
FindOneAndUpdate(collectionName string, i interface{}, u interface{}) (l interface{}, err error)
3839
DeleteFromMap(collectionName string, filter interface{}, update interface{}) (l interface{}, err error)
3940
FindOneAndPush(collectionName string, i interface{}, u interface{}, field string) (l interface{}, err error)

models/meal.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ type Meal struct {
1111
IsVeg bool `json:"isVeg,omitempty" bson:"isVeg,omitempty"`
1212
Special bool `json:"special,omitempty" bson:"special,omitempty"`
1313
Spicey bool `json:"spicey,omitempty" bson:"spicey,omitempty"`
14+
Available bool `json:"available,omitempty" bson:"available,omitempty"`
1415
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
1516
RID string `json:"rid,omitempty" bson:"rid,omitempty"`
1617
Name string `json:"name,omitempty" bson:"name,omitempty" `
17-
Price float32 `json:"price,omitempty" bson:"price,omitempty" `
18-
DisplayPrice float32 `json:"display_price,omitempty" bson:"display_price,omitempty"`
18+
Price float64 `json:"price,omitempty" bson:"price,omitempty" `
19+
DisplayPrice float64 `json:"display_price,omitempty" bson:"display_price,omitempty"`
1920
}
2021

2122
func (m *Meal) GetModelData() string {

models/order.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ import (
77
"go.mongodb.org/mongo-driver/bson/primitive"
88
)
99

10+
type Summary struct {
11+
Meal Meal `json:"meal,omitempty" bson:"meal,omitempty"`
12+
Count int `json:"count,omitempty" bson:"count,omitempty"`
13+
Price float64 `json:"price,omitempty" bson:"price,omitempty"`
14+
}
15+
1016
type Order struct {
1117
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
1218
RID string `json:"rid,omitempty" bson:"rid,omitempty"`
1319
UID string `json:"uid,omitempty" bson:"uid,omitempty"`
1420
Address string `json:"address,omitempty" bson:"address,omitempty"`
15-
Meals []string `json:"meals,omitempty" bson:"meals,omitempty"`
21+
Items map[string]int `json:"items,omitempty" bson:"items,omitempty"`
22+
Summary []Summary `json:"summary,omitempty" bson:"summary,omitempty"`
1623
Time primitive.DateTime `json:"time,omitempty" bson:"time,omitempty"`
17-
Price float32 `json:"price,omitempty" bson:"price,omitempty"`
18-
DeliveryCharge float32 `json:"delivery_charge,omitempty" bson:"delivery_charge,omitempty"`
24+
Price float64 `json:"price,omitempty" bson:"price,omitempty"`
25+
Total float64 `json:"total,omitempty" bson:"total,omitempty"`
26+
DeliveryCharge float64 `json:"delivery_charge,omitempty" bson:"delivery_charge,omitempty"`
1927
}
2028

2129
func (o *Order) GetModelData() string {
@@ -28,9 +36,5 @@ func (o *Order) Validate() error {
2836
return errors.New("NOT_VALIDATED")
2937
}
3038

31-
if len(o.Meals) == 0 {
32-
return errors.New("ITEMS_EMPTY")
33-
}
34-
3539
return nil
3640
}

models/restaurant.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
type Restaurant struct {
1111
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
12+
Closed bool `json:"closed,omitempty" bson:"closed,omitempty"`
1213
Name string `json:"name,omitempty" bson:"name,omitempty"`
1314
Phone string `json:"phone,omitempty" bson:"phone,omitempty" `
1415
LogoURL string `json:"logo,omitempty" bson:"logo,omitempty"`

store/order.go

+64-3
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,84 @@
11
package store
22

33
import (
4+
"errors"
5+
"log"
46
"time"
57

68
"github.com/incrypt0/cokut-server/models"
79
"go.mongodb.org/mongo-driver/bson/primitive"
810
)
911

1012
// CreateOrder creates a new order
11-
func (s *Store) CreateOrder(o *models.Order) (id string, err error) {
13+
func (s *Store) CreateOrder(o *models.Order) (po *models.Order, err error) {
1214
c := s.orders // Basic Validation
1315

1416
o.Time = primitive.NewDateTimeFromTime(time.Now())
1517

1618
if err = o.Validate(); err != nil {
17-
return id, err
19+
return po, err
1820
}
1921

20-
return s.w.Add(c, o)
22+
if err = s.processOrder(o); err != nil {
23+
return po, err
24+
}
25+
26+
if _, err := s.w.Add(c, o); err != nil {
27+
return nil, err
28+
}
29+
return o, err
30+
}
31+
32+
func (s *Store) processOrder(o *models.Order) error {
33+
mealCollection := s.mc
34+
ids := make([]string, 0, len(o.Items))
35+
36+
for key := range o.Items {
37+
log.Println(key, "_____", len(o.Items))
38+
ids = append(ids, key)
39+
}
40+
41+
rid, err := primitive.ObjectIDFromHex(o.RID)
42+
if err != nil {
43+
return err
44+
}
45+
46+
if r, err := s.w.FindOne(s.rc, models.Restaurant{ID: rid}); err != nil {
47+
return err
48+
} else if r.(*models.Restaurant).Closed {
49+
return errors.New("RESTAURANT CLOSED")
50+
}
51+
52+
l, err := s.w.GetMultipleByID(mealCollection, models.Meal{}, ids)
53+
54+
s.calculateOrderPrice(o, l)
55+
s.calculateDeliveryCharge(o)
56+
s.calculateTotal(o)
57+
58+
if err != nil {
59+
return err
60+
}
61+
62+
return err
63+
}
64+
65+
func (s *Store) calculateOrderPrice(o *models.Order, l []interface{}) {
66+
for _, item := range l {
67+
meal := item.(*models.Meal)
68+
count := o.Items[meal.ID.Hex()]
69+
price := meal.Price * float64(count)
70+
71+
o.Summary = append(o.Summary, models.Summary{Meal: *meal, Count: count, Price: price})
72+
o.Price += price
73+
}
74+
}
75+
76+
func (s *Store) calculateDeliveryCharge(o *models.Order) {
77+
o.DeliveryCharge = 20
78+
}
79+
80+
func (s *Store) calculateTotal(o *models.Order) {
81+
o.Total = o.Price + o.DeliveryCharge
2182
}
2283

2384
// GetAllOrders Admin only function

store/user_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,9 @@ func TestDrop(t *testing.T) {
9292

9393
_ = s.w.DropTest()
9494
}
95+
96+
func TestOrder(t *testing.T) {
97+
var s *Store = NewStore("mctest", "uctest", "octest", "rctest", workers.New())
98+
99+
_ = s.w.DropTest()
100+
}

workers/worker.go

+41-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (w *Worker) Add(collectionName string, i interface{}) (id string, err error
8080
}
8181

8282
id = result.InsertedID.(primitive.ObjectID).Hex()
83+
log.Println(result)
8384

8485
return id, err
8586
}
@@ -105,18 +106,18 @@ func (w *Worker) DeleteOne(collectionName string, i interface{}) (n int64, err e
105106
}
106107

107108
// Get gets details from db with given filter
108-
func (w *Worker) Get(collectionName string, i interface{}) (l []interface{}, errerr error) {
109+
func (w *Worker) Get(collectionName string, filter interface{}) (l []interface{}, err error) {
109110
c := w.db.Collection(collectionName)
110111
ctx := context.Background()
111-
typ := reflect.TypeOf(i)
112-
a := reflect.Zero(reflect.TypeOf(i)).Interface()
112+
typ := reflect.TypeOf(filter)
113+
a := reflect.Zero(reflect.TypeOf(filter)).Interface()
113114

114115
// log.Println("Without Filter : ", reflect.DeepEqual(a, i))
115-
if reflect.DeepEqual(a, i) {
116-
i = bson.D{}
116+
if reflect.DeepEqual(a, filter) {
117+
filter = bson.D{}
117118
}
118119

119-
cur, err := c.Find(ctx, i)
120+
cur, err := c.Find(ctx, filter)
120121

121122
for cur.Next(ctx) {
122123
i := reflect.New(typ).Interface()
@@ -134,6 +135,40 @@ func (w *Worker) Get(collectionName string, i interface{}) (l []interface{}, err
134135
return l, err
135136
}
136137

138+
func (w *Worker) GetMultipleByID(collectionName string, model interface{}, ids []string) (l []interface{}, err error) {
139+
log.Println(ids, len(ids))
140+
pids := make([]primitive.ObjectID, len(ids))
141+
142+
ctx := context.Background()
143+
c := w.db.Collection(collectionName)
144+
typ := reflect.TypeOf(model)
145+
146+
for i, b := range ids {
147+
log.Println(i, "|", b, ids)
148+
pids[i], err = primitive.ObjectIDFromHex(b)
149+
150+
if err != nil {
151+
log.Println(err)
152+
return nil, err
153+
}
154+
}
155+
156+
cur, err := c.Find(ctx, bson.M{"_id": bson.M{"$in": pids}})
157+
for cur.Next(ctx) {
158+
i := reflect.New(typ).Interface()
159+
160+
// Remember dont use a pointer to l here by i
161+
if err = cur.Decode(i); err != nil {
162+
log.Println(err)
163+
return nil, err
164+
}
165+
166+
l = append(l, i)
167+
}
168+
169+
return l, err
170+
}
171+
137172
// FindOneAndUpdate FindOneAndUpdate
138173
func (w *Worker) FindOneAndUpdate(collectionName string, filter interface{}, update interface{}) (
139174
l interface{},

workers/workers_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ func TestDBHandler(t *testing.T) {
6565
} else if i.(*Test).Blah[1].Hai != "hello" {
6666
t.Error(utils.ModelToString(i))
6767
}
68+
69+
if l, err := w.GetMultipleByID(c, Test{}, []string{id1.Hex(), id2.Hex()}); err != nil {
70+
t.Error(err)
71+
} else {
72+
t.Log(utils.ModelToString(l))
73+
}
6874
}

0 commit comments

Comments
 (0)