/
model.go
56 lines (46 loc) · 1.17 KB
/
model.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
package productdb
import (
"time"
"github.com/ardanlabs/service/business/domain/productbus"
"github.com/google/uuid"
)
type product struct {
ID uuid.UUID `db:"product_id"`
UserID uuid.UUID `db:"user_id"`
Name string `db:"name"`
Cost float64 `db:"cost"`
Quantity int `db:"quantity"`
DateCreated time.Time `db:"date_created"`
DateUpdated time.Time `db:"date_updated"`
}
func toDBProduct(bus productbus.Product) product {
db := product{
ID: bus.ID,
UserID: bus.UserID,
Name: bus.Name,
Cost: bus.Cost,
Quantity: bus.Quantity,
DateCreated: bus.DateCreated.UTC(),
DateUpdated: bus.DateUpdated.UTC(),
}
return db
}
func toBusProduct(db product) productbus.Product {
bus := productbus.Product{
ID: db.ID,
UserID: db.UserID,
Name: db.Name,
Cost: db.Cost,
Quantity: db.Quantity,
DateCreated: db.DateCreated.In(time.Local),
DateUpdated: db.DateUpdated.In(time.Local),
}
return bus
}
func toBusProducts(dbs []product) []productbus.Product {
bus := make([]productbus.Product, len(dbs))
for i, db := range dbs {
bus[i] = toBusProduct(db)
}
return bus
}