This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
forked from osuthailand/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.go
82 lines (71 loc) · 1.81 KB
/
badge.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
package v1
import (
"database/sql"
"github.com/osuthailand/api/common"
)
type singleBadge struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Icon string `json:"icon"`
}
type multiBadgeData struct {
common.ResponseBase
Badges []singleBadge `json:"badges"`
}
// BadgesGET retrieves all the badges on this ripple instance.
func BadgesGET(md common.MethodData) common.CodeMessager {
var (
r multiBadgeData
rows *sql.Rows
err error
)
if md.Query("id") != "" {
rows, err = md.DB.Query("SELECT id, name, icon FROM badges WHERE id = ? LIMIT 1", md.Query("id"))
} else {
rows, err = md.DB.Query("SELECT id, name, icon FROM badges " + common.Paginate(md.Query("p"), md.Query("l"), 50))
}
if err != nil {
md.Err(err)
return Err500
}
defer rows.Close()
for rows.Next() {
nb := singleBadge{}
err = rows.Scan(&nb.ID, &nb.Name, &nb.Icon)
if err != nil {
md.Err(err)
}
r.Badges = append(r.Badges, nb)
}
if err := rows.Err(); err != nil {
md.Err(err)
}
r.ResponseBase.Code = 200
return r
}
type badgeMembersData struct {
common.ResponseBase
Members []userData `json:"members"`
}
// BadgeMembersGET retrieves the people who have a certain badge.
func BadgeMembersGET(md common.MethodData) common.CodeMessager {
i := common.Int(md.Query("id"))
if i == 0 {
return ErrMissingField("id")
}
var members badgeMembersData
err := md.DB.Select(&members.Members, `SELECT users.id, users.username, register_datetime, users.privileges,
latest_activity, users_stats.username_aka,
users_stats.country
FROM user_badges ub
INNER JOIN users ON users.id = ub.user
INNER JOIN users_stats ON users_stats.id = ub.user
WHERE badge = ?
ORDER BY id ASC `+common.Paginate(md.Query("p"), md.Query("l"), 50), i)
if err != nil {
md.Err(err)
return Err500
}
members.Code = 200
return members
}