This repository has been archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
96 lines (85 loc) · 2.03 KB
/
database.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
package main
import (
"runtime"
"time"
"github.com/google/uuid"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/sirupsen/logrus"
)
// User ...
type User struct {
ID string `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
Service string
Name string
DisplayName string
Nick string
UserID string
Email string
}
// NewDatabase ...
func (ur *UnRustleLogs) NewDatabase() {
file := "/data/users.db"
if runtime.GOOS == "windows" {
file = "users.db"
}
var err error
ur.db, err = gorm.Open("sqlite3", file)
if err != nil {
logrus.Fatal(err)
}
ur.db.AutoMigrate(&User{})
}
// AddTwitchUser ...
func (ur *UnRustleLogs) AddTwitchUser(user *TwitchUser) string {
if id, ok := ur.UserInDatabase(user.Name, TWITCHSERVICE); ok {
return id
}
id, _ := uuid.NewRandom()
ur.db.Create(&User{
ID: id.String(),
Name: user.Name,
DisplayName: user.DisplayName,
Email: user.Email,
UserID: user.ID,
Service: TWITCHSERVICE,
})
return id.String()
}
// AddDggUser ...
func (ur *UnRustleLogs) AddDggUser(user *DestinyggUser) string {
if id, ok := ur.UserInDatabase(user.Username, DESTINYGGSERVICE); ok {
return id
}
id, _ := uuid.NewRandom()
ur.db.Create(&User{
ID: id.String(),
Name: user.Username,
DisplayName: user.Nick,
UserID: user.UserID,
Service: DESTINYGGSERVICE,
})
return id.String()
}
// DeleteUser ...
func (ur *UnRustleLogs) DeleteUser(name, service string) {
var u User
ur.db.Where("name = ? and service = ?", name, service).First(&u)
if name == u.Name && service == u.Service {
ur.db.Delete(&u)
}
}
// UserInDatabase ...
func (ur *UnRustleLogs) UserInDatabase(name, service string) (string, bool) {
var u User
ur.db.Where("name = ? and service = ?", name, service).First(&u)
return u.ID, u.Name == name && u.Service == service
}
// GetUser ...
func (ur *UnRustleLogs) GetUser(id string) (*User, bool) {
var u User
ur.db.Where("id = ?", id).First(&u)
return &u, u.ID == id
}