-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
126 lines (108 loc) · 3.41 KB
/
database.go
File metadata and controls
126 lines (108 loc) · 3.41 KB
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
package goker
import (
"fmt"
_ "github.com/go-sql-driver/mysql" // used by gorm
"github.com/jinzhu/gorm"
"log"
"time"
)
func init() {
connectionStr := fmt.Sprintf("%v:%v@/%v?charset=utf8&parseTime=True",
GokerCtx.Cfg.Database.User,
GokerCtx.Cfg.Database.Password,
GokerCtx.Cfg.Database.Name)
log.Println("Trying to connect to dababase: ", connectionStr)
var err error
GokerCtx.DB, err = gorm.Open("mysql", connectionStr)
if err != nil {
log.Panicf("Got error when connect database, the error is '%v'", err)
}
createTables()
}
func DBClose() {
GokerCtx.DB.Close()
}
func DBClear() {
log.Print("Dropping database tables...")
defer log.Println("done")
GokerCtx.DB.DropTable(User{})
GokerCtx.DB.DropTable(Cup{})
GokerCtx.DB.DropTable(Game{})
GokerCtx.DB.DropTable(Score{})
GokerCtx.DB.DropTable(UserCup{})
GokerCtx.DB.DropTable(UserGame{})
}
// Init default database by dropping recreating tables with default data
func DBDefaultData() {
DBClear()
createDefaultData()
}
func FillCupData(cup *Cup) {
GokerCtx.DB.Model(cup).Related(&cup.Owner, "OwnerId")
cup.Users = DBGetUsersForCup(cup.Id)
GokerCtx.DB.Model(cup).Related(&cup.Games)
}
func DBGetCupsForUser(userId int64) []Cup {
cups := []Cup{}
GokerCtx.DB.Debug().Raw("SELECT l.id, l.name, l.owner_id, l.created_at, l.updated_at FROM cups l, user_cups WHERE l.id = user_cups.cup_id AND user_cups.user_id = ?", userId).Scan(&cups)
for i := 0; i < len(cups); i++ {
FillCupData(&cups[i])
}
return cups
}
func DBGetUsersForCup(cupId int64) []User {
users := []User{}
GokerCtx.DB.Raw("SELECT u.id, u.login, u.password, u.email, u.name, u.created_at, u.updated_at, u.deleted_at FROM users u, user_cups ul WHERE u.id = ul.user_id AND ul.cup_id = ?", cupId).Scan(&users)
return users
}
func DBGetUsersForGame(gameId int64) []User {
users := []User{}
GokerCtx.DB.Raw("SELECT u.id, u.login, u.password, u.email, u.name, u.created_at, u.updated_at, u.deleted_at FROM users u, user_games ug WHERE u.id = ug.user_id AND ug.game_id = ?", gameId).Scan(&users)
return users
}
func DBGetGamesForUser(userId int64) []Game {
games := []Game{}
GokerCtx.DB.Debug().Raw("SELECT g.id, g.type, g.cup_id FROM games g, user_games ug WHERE g.id = ug.game_id AND ug.user_id = ?", userId).Scan(&games)
return games
}
func createTables() {
GokerCtx.DB.AutoMigrate(User{})
GokerCtx.DB.AutoMigrate(Cup{})
GokerCtx.DB.AutoMigrate(Game{})
GokerCtx.DB.AutoMigrate(Score{})
GokerCtx.DB.AutoMigrate(UserCup{})
GokerCtx.DB.AutoMigrate(UserGame{})
}
func createDefaultData() {
log.Print("Creating default database data...")
defer log.Print("done")
createTables()
//db.Debug().Unscoped().Where("login = ?", "guillaume").Delete(User{})
user1 := User{
Login: "guillaume",
Password: "guillaume",
Name: "guillaume lazar",
CreatedAt: time.Now(),
UpdatedAt: time.Now()}
GokerCtx.DB.Save(&user1)
user2 := User{
Login: "robin",
Password: "robin",
Name: "robin penea",
CreatedAt: time.Now(),
UpdatedAt: time.Now()}
GokerCtx.DB.Save(&user2)
cup := Cup{
Name: "cup-test",
Owner: user1,
CreatedAt: time.Now(),
UpdatedAt: time.Now()}
GokerCtx.DB.Save(&cup)
GokerCtx.DB.Save(&UserCup{User: user1, Cup: cup})
GokerCtx.DB.Save(&UserCup{User: user2, Cup: cup})
// Game data
game := Game{Type: GameTypeCashGame, Cup: cup}
GokerCtx.DB.Save(&game)
GokerCtx.DB.Save(&UserGame{User: user1, Game: game})
GokerCtx.DB.Save(&UserGame{User: user2, Game: game})
}