-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.go
54 lines (48 loc) · 1.2 KB
/
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
package receipt
import (
"log"
"strconv"
"github.com/jmoiron/sqlx"
)
// language store uint64 as int64 in the database
type ProductId int64
type Product struct {
Id ProductId
Price float64
Name string
UnitName string
}
func NewProductId(pid string) (ProductId, error) {
value, err := strconv.ParseInt(pid, 10, 64)
if err != nil {
return ProductId(0), err
}
return ProductId(value), nil
}
func fetchProducts(productIds []ProductId) (map[ProductId]*Product, []ProductId) {
query, args, _ := sqlx.In("SELECT * FROM product WHERE id IN (?)", productIds)
query = db.Rebind(query)
rows, err := db.Queryx(query, args...)
if err != nil {
log.Printf("DB query product fail %s", err)
return nil, productIds
}
products := make(map[ProductId]*Product)
for rows.Next() {
product := Product{}
if err := rows.StructScan(&product); err != nil {
log.Printf("%s", err)
}
products[product.Id] = &product
}
if len(productIds) == len(products) {
return products, nil
}
missing := make([]ProductId, 0, len(productIds)-len(products))
for _, productId := range productIds {
if _, ok := products[productId]; !ok {
missing = append(missing, productId)
}
}
return products, missing
}