-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.go
133 lines (115 loc) · 3.79 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Achievement struct {
gorm.Model
Slug string `gorm:"unique;not null" json:"slug"`
Name string `gorm:"unique;not null" json:"name"`
Desc string `json:"desc"`
Img string `json:"img"`
Members []Member `gorm:"many2many:member_achievements;" json:"members,omitempty"`
}
type Member struct {
gorm.Model
Name string `gorm:"unique;not null" json:"name"`
Img string `json:"img"`
Achievements []Achievement `gorm:"many2many:member_achievements;" json:"achievements,omitempty"`
Teams []Team `gorm:"many2many:team_members;" json:"teams,omitempty"`
Games []Game `gorm:"many2many game_members;" json:"games,omitempty"`
Stats []Stat `json:"stats,omitempty"`
}
type Team struct {
gorm.Model
Name string `gorm:"unique;not null" json:"name"`
Img string `json:"img"`
Members []Member `gorm:"many2many:team_members;" json:"members,omitempty"`
Games []Game `gorm:"many2many game_teams;" json:"games,omitempty"`
Stats []Stat `json:"stats,omitempty"`
}
type Game struct {
gorm.Model
Status gameStatus `json:"status"`
Teams []Team `gorm:"many2many:game_teams;" json:"teams,omitempty"`
Members []Member `gorm:"many2many:game_members;" json:"members,omitempty"`
Stats []Stat `json:"stats,omitempty"`
}
type Stat struct {
gorm.Model
GameID uint `json:"game_id"`
TeamID uint `json:"team_id"`
MemberID uint `json:"member_id"`
NumAttacks uint `json:"num_attacks"`
NumHits uint `json:"num_hits"`
AmountDamage uint `json:"amount_damage"`
NumKills uint `json:"num_kills"`
InstantKills uint `json:"instant_kills"`
NumAssists uint `json:"num_assists"`
NumSpells uint `json:"num_spells"`
SpellsDamage uint `json:"spells_damage"`
IsWinner bool `json:"is_winner"`
}
var db *gorm.DB
func initDatabase() {
var err error
db, err = gorm.Open(databaseDialect, databaseFile)
if err != nil {
panic("failed to connect to database")
}
db.AutoMigrate(&Achievement{}, &Member{}, &Team{}, &Game{}, &Stat{})
}
/*
get all records where .. condition a b c
*/
func getAllRecordsWhereABC(model, a, b, c interface{}) error {
return db.Where(a, b, c).Find(model).Error
}
func getRecordWhereABC(model, a, b, c interface{}) error {
return db.Where(a, b, c).First(model).Error
}
func createFromModel(model interface{}) error {
return db.Create(model).Error
}
func getAllRecords(model interface{}) error {
return db.Find(model).Error
}
func getRecordByID(model, id interface{}) error {
return db.First(model, id).Error
}
func updateRecordByID(oldRecord, newRecord, id interface{}) error {
if err := db.First(oldRecord, id).Error; err != nil {
return err
}
return db.Model(oldRecord).Omit("ID","CreatedAt","UpdatedAt","DeletedAt").Updates(newRecord).Error
}
func deleteRecordByID(model, id interface{}) error {
return db.Delete(model, id).Error
}
func findAssociationRecords(modelA, idA, assoc, modelB interface{}) error {
if err := db.First(modelA, idA).Error; err != nil {
return err
}
if err := db.Model(modelA).Association(assoc.(string)).Find(modelB).Error; err != nil {
return err
}
return nil
}
func appendAssociationRecord(modelA, idA, assoc, modelB, idB interface{}) error {
if err := db.First(modelA, idA).Error; err != nil {
return err
}
if err := db.First(modelB, idB).Error; err != nil {
return err
}
return db.Model(modelA).Association(assoc.(string)).Append(modelB).Error
}
func deleteAssociationRecord(modelA, idA, assoc, modelB, idB interface{}) error {
if err := db.First(modelA, idA).Error; err != nil {
return err
}
if err := db.First(modelB, idB).Error; err != nil {
return err
}
return db.Model(modelA).Association(assoc.(string)).Delete(modelB).Error
}