-
Notifications
You must be signed in to change notification settings - Fork 335
/
products.go
127 lines (106 loc) · 2.71 KB
/
products.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
package data
import (
"fmt"
)
// ErrProductNotFound is an error raised when a product can not be found in the database
var ErrProductNotFound = fmt.Errorf("Product not found")
// Product defines the structure for an API product
// swagger:model
type Product struct {
// the id for the product
//
// required: false
// min: 1
ID int `json:"id"` // Unique identifier for the product
// the name for this poduct
//
// required: true
// max length: 255
Name string `json:"name" validate:"required"`
// the description for this poduct
//
// required: false
// max length: 10000
Description string `json:"description"`
// the price for the product
//
// required: true
// min: 0.01
Price float32 `json:"price" validate:"required,gt=0"`
// the SKU for the product
//
// required: true
// pattern: [a-z]+-[a-z]+-[a-z]+
SKU string `json:"sku" validate:"sku"`
}
// Products defines a slice of Product
type Products []*Product
// GetProducts returns all products from the database
func GetProducts() Products {
return productList
}
// GetProductByID returns a single product which matches the id from the
// database.
// If a product is not found this function returns a ProductNotFound error
func GetProductByID(id int) (*Product, error) {
i := findIndexByProductID(id)
if id == -1 {
return nil, ErrProductNotFound
}
return productList[i], nil
}
// UpdateProduct replaces a product in the database with the given
// item.
// If a product with the given id does not exist in the database
// this function returns a ProductNotFound error
func UpdateProduct(p Product) error {
i := findIndexByProductID(p.ID)
if i == -1 {
return ErrProductNotFound
}
// update the product in the DB
productList[i] = &p
return nil
}
// AddProduct adds a new product to the database
func AddProduct(p Product) {
// get the next id in sequence
maxID := productList[len(productList)-1].ID
p.ID = maxID + 1
productList = append(productList, &p)
}
// DeleteProduct deletes a product from the database
func DeleteProduct(id int) error {
i := findIndexByProductID(id)
if i == -1 {
return ErrProductNotFound
}
productList = append(productList[:i], productList[i+1])
return nil
}
// findIndex finds the index of a product in the database
// returns -1 when no product can be found
func findIndexByProductID(id int) int {
for i, p := range productList {
if p.ID == id {
return i
}
}
return -1
}
var productList = []*Product{
&Product{
ID: 1,
Name: "Latte",
Description: "Frothy milky coffee",
Price: 2.45,
SKU: "abc323",
},
&Product{
ID: 2,
Name: "Esspresso",
Description: "Short and strong coffee without milk",
Price: 1.99,
SKU: "fjd34",
},
}