/
domain_product.go
89 lines (69 loc) · 1.57 KB
/
domain_product.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
package productcatalog
import (
"errors"
"regexp"
)
var ErrProductNotFound = errors.New("product not found")
type Price struct {
amount float64
currency string
}
func NewPrice(amount float64, currency string) Price {
return Price{
amount: amount,
currency: currency,
}
}
func (p Price) Amount() float64 {
return p.amount
}
func (p Price) Currency() string {
return p.currency
}
// Product is an entity that represents a single product visable in the product catalog
type Product struct {
id ProductID
name string
description string
price Price
thumbnail string
}
var emptyProduct = Product{}
var productIDReg = regexp.MustCompile(`[\w\d\-]+`)
type ProductID string
func NewProductId(id string) (ProductID, error) {
if !productIDReg.MatchString(id) {
return ProductID(""), errors.New("the ID doesn't match")
}
return ProductID(id), nil
}
func NewProduct(id ProductID, name, description string, price Price, thumbnail string) (Product, error) {
if name == "" {
return emptyProduct, errors.New("the name cannot be empty")
}
if description == "" {
return emptyProduct, errors.New("the description cannot be empty")
}
return Product{
id: id,
name: name,
price: price,
description: description,
thumbnail: thumbnail,
}, nil
}
func (p Product) ID() ProductID {
return p.id
}
func (p Product) Name() string {
return p.name
}
func (p Product) Description() string {
return p.description
}
func (p Product) Price() Price {
return p.price
}
func (p Product) Thumbnail() string {
return p.thumbnail
}