-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.go
43 lines (35 loc) · 939 Bytes
/
cart.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
package cart
import (
"errors"
"time"
)
var ErrInvalidUserID = errors.New("userID is not valid")
// Cart holds the basic data of a shopping cart
type Cart struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
// Price is a value type for price
type Price float64
// Item represents a fixed number of a single product in the shopping cart
type Item struct {
ID int64 `json:"id"`
ProductID int64 `json:"product_id"`
CartID int64 `json:"cart_id"`
Quantity int64 `json:"quantity"`
// Price is the total price of the item, i.e. product's price * quantity
Price Price `json:"price"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
// NewCart creates a new cart
func NewCart(userID int64) (*Cart, error) {
if userID == 0 {
return nil, ErrInvalidUserID
}
return &Cart{
UserID: userID,
}, nil
}