This repository has been archived by the owner on Jul 4, 2020. It is now read-only.
forked from Hazuki-san/api
-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.go
75 lines (65 loc) · 1.69 KB
/
common.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
package peppy
import (
"database/sql"
_json "encoding/json"
"strconv"
"github.com/jmoiron/sqlx"
"github.com/valyala/fasthttp"
"github.com/osuthailand/api/common"
)
var modes = []string{"std", "taiko", "ctb", "mania"}
var defaultResponse = []struct{}{}
func genmode(m string) string {
i := genmodei(m)
return modes[i]
}
func genmodei(m string) int {
v := common.Int(m)
if v > 3 || v < 0 {
v = 0
}
return v
}
func rankable(m string) bool {
x := genmodei(m)
return x != 2
}
func genUser(c *fasthttp.RequestCtx, db *sqlx.DB) (string, string) {
var whereClause string
var p string
// used in second case of switch
s, err := strconv.Atoi(query(c, "u"))
switch {
// We know for sure that it's an username.
case query(c, "type") == "string":
whereClause = "users.username_safe = ?"
p = common.SafeUsername(query(c, "u"))
// It could be an user ID, so we look for an user with that username first.
case err == nil:
err = db.QueryRow("SELECT id FROM users WHERE id = ? LIMIT 1", s).Scan(&p)
if err == sql.ErrNoRows {
// If no user with that userID were found, assume username.
whereClause = "users.username_safe = ?"
p = common.SafeUsername(query(c, "u"))
} else {
// An user with that userID was found. Thus it's an userID.
whereClause = "users.id = ?"
}
// u contains letters, so it's an username.
default:
whereClause = "users.username_safe = ?"
p = common.SafeUsername(query(c, "u"))
}
return whereClause, p
}
func query(c *fasthttp.RequestCtx, s string) string {
return string(c.QueryArgs().Peek(s))
}
func json(c *fasthttp.RequestCtx, code int, data interface{}) {
c.SetStatusCode(code)
d, err := _json.Marshal(data)
if err != nil {
panic(err)
}
c.Write(d)
}