forked from brenosiqueira/orders
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorder_item.go
53 lines (38 loc) · 996 Bytes
/
order_item.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
package main
import (
"fmt"
"log"
"time"
"errors"
)
type OrderItem struct {
Sku string `cql:"sku" json:"sku"`
UnitPrice int `cql:"unit_price" json:"unit_price"`
Quantity int `cql:"quantity" json:"quantity"`
}
func (oi OrderItem) ValidadeNewOrderItem() error {
if oi.Quantity <= 0 {
return errors.New("OrderItem Quantity cannot be less or equal to 0")
}
if oi.Sku == "" {
return errors.New("OrderItem Sku cannot be empty")
}
if oi.UnitPrice < 0 {
return errors.New("OrderItem UnitPrice cannot be less than 0")
}
return nil;
}
func (item *OrderItem) Save(order_id string) error {
err := item.ValidadeNewOrderItem()
if err != nil {
log.Print(err)
return err;
}
query := fmt.Sprintf("UPDATE orders SET updated_at = ?, items = items + [{sku: %v, unit_price: %v, quantity: %v}] WHERE id = %v",
item.Sku, item.UnitPrice, item.Quantity, order_id)
err = session.Query(query, time.Now()).Exec()
if err != nil {
log.Print(err)
}
return err
}