-
Notifications
You must be signed in to change notification settings - Fork 0
/
gormsetrepo.go
76 lines (67 loc) · 1.73 KB
/
gormsetrepo.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
package storage
import (
scryfallset "github.com/high-creek-software/goscryfall/sets"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type gormSetRepo struct {
db *gorm.DB
}
func newGormSetRepo(db *gorm.DB) *gormSetRepo {
repo := &gormSetRepo{db: db}
repo.db.AutoMigrate(&gormSet{})
return repo
}
func (r *gormSetRepo) StoreSets(sets []scryfallset.Set) error {
var insert []gormSet
for _, set := range sets {
gs := gormSet{
Object: set.Object,
Id: set.Id,
Code: set.Code,
Name: set.Name,
Uri: set.Uri,
ScryfallUri: set.ScryfallUri,
ReleasedAt: set.ReleasedAt,
SetType: set.SetType,
CardCount: set.CardCount,
ParentSetCode: set.ParentSetCode,
Digital: set.Digital,
NonfoilOnly: set.NonfoilOnly,
FoilOnly: set.FoilOnly,
IconSvgUri: set.IconSvgUri,
}
insert = append(insert, gs)
}
return r.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&insert).Error
}
func (r *gormSetRepo) ListSets() ([]scryfallset.Set, error) {
var all []gormSet
r.db.Order("released_at desc").Find(&all)
var res []scryfallset.Set
for _, a := range all {
s := scryfallset.Set{
Object: a.Object,
Id: a.Id,
Code: a.Code,
Name: a.Name,
Uri: a.Uri,
ScryfallUri: a.ScryfallUri,
ReleasedAt: a.ReleasedAt,
SetType: a.SetType,
CardCount: a.CardCount,
ParentSetCode: a.ParentSetCode,
Digital: a.Digital,
NonfoilOnly: a.NonfoilOnly,
FoilOnly: a.FoilOnly,
IconSvgUri: a.IconSvgUri,
}
res = append(res, s)
}
return res, nil
}
func (r *gormSetRepo) SetCount() int64 {
var count int64
r.db.Model(&gormSet{}).Count(&count)
return count
}