This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
63 lines (58 loc) · 1.55 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
package botsbyuberswe
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// Saving to the database
func storeStruct(s interface{}, prefix string, key string) error {
b, err := json.Marshal(s)
if err != nil {
log.Printf("Error: %s", err)
return err
}
return db.Put([]byte(fmt.Sprintf("%s:%s", prefix, key)), b, nil)
}
// getUserFromCookie gets the user struct from a cookie
func getUserFromCookie(cookie *http.Cookie) (User, error) {
var cookieObj Cookie
var user User
log.Printf("cookie val: %s", cookie.Value)
data, err := db.Get([]byte(fmt.Sprintf("cookie:%s", cookie.Value)), nil)
err = json.Unmarshal(data, &cookieObj)
if err != nil {
return user, nil
}
return getUserFromTwitchID(cookieObj.TwitchID)
}
// getUserFromTwitchID gets a user struct based on the twitchID, the twitchID is the key in our database
func getUserFromTwitchID(twitchID string) (User, error) {
var user User
data, err := db.Get([]byte(fmt.Sprintf("user:%s", twitchID)), nil)
if err != nil {
log.Println(err)
return user, err
}
err = json.Unmarshal(data, &user)
if err != nil {
log.Println(err)
return user, err
}
return user, nil
}
// getTwitchIDFromChannelName gets the twitch id of a user from the channel name
func getTwitchIDFromChannelName(channelName string) (string, error) {
var twitchID string
data, err := db.Get([]byte(fmt.Sprintf("userChannel:%s", channelName)), nil)
if err != nil {
log.Println(err)
return twitchID, err
}
err = json.Unmarshal(data, &twitchID)
if err != nil {
log.Println(err)
return twitchID, err
}
return twitchID, nil
}