-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
63 lines (52 loc) · 1.24 KB
/
store.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
package gormseeder
import (
"gorm.io/gorm"
)
// Seeder model
type Seeder struct {
gorm.Model
Tag string `gorm:"size:455"`
Filename string `gorm:"size:455"`
User string `gorm:"size:255"`
}
type SeederStore struct {
tableName string
db *gorm.DB
}
func NewSeederStore(db *gorm.DB, tableName string) *SeederStore {
if tableName == "" {
tableName = "Seeders"
}
// Creating a Seeder table if not exists
db.Table(tableName).AutoMigrate(&Seeder{})
return &SeederStore{
tableName: tableName,
db: db,
}
}
// If tag is not found an NoRecordFound
func (m *SeederStore) TagInDB(tag string) bool {
seed := &Seeder{}
if err := m.db.Table(m.tableName).Take(&seed, "tag = ?", tag).Error; err == gorm.ErrRecordNotFound {
return false
}
return true
}
func (m *SeederStore) FetchAll() ([]*Seeder, error) {
var SeederList []*Seeder
if err := m.db.Table(m.tableName).Order("id DESC").Find(&SeederList).Error; err != nil {
return nil, err
}
return SeederList, nil
}
func (m *SeederStore) SaveState(tag, filename, user string) error {
currentState := &Seeder{
Tag: tag,
Filename: filename,
User: user,
}
if err := m.db.Table(m.tableName).Create(¤tState).Error; err != nil {
return err
}
return nil
}