ripple-api/app/v1/badge.go

83 lines
1.8 KiB
Go
Raw Normal View History

2016-04-03 20:31:41 +00:00
package v1
import (
"database/sql"
"github.com/osuYozora/api/common"
2016-04-03 20:31:41 +00:00
)
2016-04-16 16:05:24 +00:00
type singleBadge struct {
2016-09-02 15:00:36 +00:00
ID int `json:"id,omitempty"`
2016-04-03 20:31:41 +00:00
Name string `json:"name"`
Icon string `json:"icon"`
}
2016-04-16 16:05:24 +00:00
type multiBadgeData struct {
common.ResponseBase
Badges []singleBadge `json:"badges"`
2016-04-03 20:31:41 +00:00
}
// BadgesGET retrieves all the badges on this ripple instance.
2016-04-16 16:05:24 +00:00
func BadgesGET(md common.MethodData) common.CodeMessager {
2016-05-14 19:02:05 +00:00
var (
r multiBadgeData
rows *sql.Rows
err error
)
if md.Query("id") != "" {
2016-09-01 20:58:21 +00:00
rows, err = md.DB.Query("SELECT id, name, icon FROM badges WHERE id = ? LIMIT 1", md.Query("id"))
2016-05-14 19:02:05 +00:00
} else {
2016-09-01 20:58:21 +00:00
rows, err = md.DB.Query("SELECT id, name, icon FROM badges " + common.Paginate(md.Query("p"), md.Query("l"), 50))
2016-05-14 19:02:05 +00:00
}
2016-04-03 20:31:41 +00:00
if err != nil {
md.Err(err)
2016-04-16 16:05:24 +00:00
return Err500
2016-04-03 20:31:41 +00:00
}
defer rows.Close()
for rows.Next() {
2016-04-16 16:05:24 +00:00
nb := singleBadge{}
2016-04-03 20:31:41 +00:00
err = rows.Scan(&nb.ID, &nb.Name, &nb.Icon)
if err != nil {
md.Err(err)
2016-04-03 20:31:41 +00:00
}
2016-04-16 16:05:24 +00:00
r.Badges = append(r.Badges, nb)
2016-04-03 20:31:41 +00:00
}
if err := rows.Err(); err != nil {
md.Err(err)
2016-04-03 20:31:41 +00:00
}
2016-04-16 16:05:24 +00:00
r.ResponseBase.Code = 200
return r
2016-04-03 20:31:41 +00:00
}
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
}