-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.go
56 lines (44 loc) · 1.34 KB
/
currency.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 database
import (
"context"
"github.com/uptrace/bun"
"time"
)
var _ bun.BeforeAppendModelHook = (*Currency)(nil)
// Currency database model.
type Currency struct {
bun.BaseModel `bun:"table:currencies"`
ID int64 `bun:"id,pk,autoincrement"`
Code string `bun:"code,notnull"`
Symbol string `bun:"symbol,notnull"`
Rate float64 `bun:"rate,notnull"`
UpdatedAt time.Time `bun:",notnull,default:current_timestamp"`
}
func (c *Currency) BeforeAppendModel(_ context.Context, query bun.Query) error {
switch query.(type) {
case *bun.InsertQuery:
c.UpdatedAt = time.Now()
case *bun.UpdateQuery:
c.UpdatedAt = time.Now()
}
return nil
}
type CurrencyQuerier interface {
GetCurrencyByCode(ctx context.Context, code string, c *Currency) error
CreateCurrency(ctx context.Context, c *Currency) error
}
func (d *DefaultDatabase) selectCurrencyByCodeQuery(code string) *bun.SelectQuery {
return d.client.NewSelect().Model(sampleCurrency).Where("code = ?", code)
}
func (d *DefaultDatabase) GetCurrencyByCode(ctx context.Context, code string, c *Currency) error {
if err := d.selectCurrencyByCodeQuery(code).Scan(ctx, c); err != nil {
return err
}
return nil
}
func (d *DefaultDatabase) CreateCurrency(ctx context.Context, c *Currency) error {
if _, err := d.client.NewInsert().Model(c).Exec(ctx); err != nil {
return err
}
return nil
}