-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdominos.go
78 lines (68 loc) · 1.87 KB
/
dominos.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
package main
import (
"github.com/WiiLink24/nwc24"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
const (
LinkDominos = `INSERT INTO "user" (basket, wii_id) VALUES ('[]', $1) ON CONFLICT(wii_id) DO UPDATE SET basket = '[]', wii_id = $1`
UnlinkDominos = `DELETE FROM "user" WHERE wii_id = $1`
SetDominosLinked = `UPDATE users SET dominos_linked = $1 WHERE email = $2`
)
func handleDominos(c *gin.Context, query string, toggle bool) {
email, ok := c.Get("email")
if !ok {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": "some how got here unauthorized",
})
return
}
var wiiNumberStr string
err := pool.QueryRow(ctx, GetWiiNumberUser, email.(string)).Scan(&wiiNumberStr)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": err.Error(),
})
return
}
wiiNumber, err := strconv.ParseUint(wiiNumberStr, 10, 64)
if err != nil {
// Failed to parse Wii Number or invalid integer
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": "invalid Wii number",
})
return
}
number := nwc24.LoadWiiNumber(wiiNumber)
if !number.CheckWiiNumber() {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": "invalid Wii number",
})
return
}
// Link the account now
_, err = dominosPool.Exec(ctx, query, strconv.Itoa(int(number.GetHollywoodID())))
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Error": err.Error(),
})
return
}
// Toggle linked flag
_, err = pool.Exec(ctx, SetDominosLinked, toggle, email.(string))
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Error": err.Error(),
})
return
}
}
func linkDominos(c *gin.Context) {
handleDominos(c, LinkDominos, true)
c.Redirect(http.StatusFound, "/manage")
}
func unlinkDominos(c *gin.Context) {
handleDominos(c, UnlinkDominos, false)
c.Redirect(http.StatusFound, "/manage")
}