add badge read API

This commit is contained in:
Howl 2016-04-03 22:31:41 +02:00
parent 6b70214810
commit 2932ba3dd7
3 changed files with 64 additions and 2 deletions

View File

@ -18,10 +18,15 @@ func Start(conf common.Conf, db *sql.DB) {
{
gv1 := api.Group("/v1")
{
gv1.GET("/user/:id", Method(v1.UserGET, db, common.PrivilegeRead))
// Auth-free API endpoints
gv1.GET("/ping", Method(v1.Ping, db))
gv1.GET("/surprise_me", Method(v1.SurpriseMe, db))
gv1.GET("/privileges", Method(v1.PrivilegesGET, db))
// Read privilege required
gv1.GET("/users/:id", Method(v1.UserGET, db, common.PrivilegeRead))
gv1.GET("/badges", Method(v1.BadgesGET, db, common.PrivilegeRead))
gv1.GET("/badges/:id", Method(v1.BadgeByIDGET, db, common.PrivilegeRead))
}
}

57
app/v1/badge.go Normal file
View File

@ -0,0 +1,57 @@
package v1
import (
"database/sql"
"github.com/osuripple/api/common"
)
type badgeData struct {
ID int `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
}
// BadgeByIDGET is the handler for /badge/:id
func BadgeByIDGET(md common.MethodData) (r common.Response) {
b := badgeData{}
err := md.DB.QueryRow("SELECT id, name, icon FROM badges WHERE id=? LIMIT 1", md.C.Param("id")).Scan(&b.ID, &b.Name, &b.Icon)
switch {
case err == sql.ErrNoRows:
r.Code = 404
r.Message = "No such badge was found"
return
case err != nil:
md.C.Error(err)
r = Err500
return
}
r.Code = 200
r.Data = b
return
}
// BadgesGET retrieves all the badges on this ripple instance.
func BadgesGET(md common.MethodData) (r common.Response) {
var badges []badgeData
rows, err := md.DB.Query("SELECT id, name, icon FROM badges")
if err != nil {
md.C.Error(err)
r = Err500
}
defer rows.Close()
for rows.Next() {
nb := badgeData{}
err = rows.Scan(&nb.ID, &nb.Name, &nb.Icon)
if err != nil {
md.C.Error(err)
}
badges = append(badges, nb)
}
if err := rows.Err(); err != nil {
md.C.Error(err)
}
r.Code = 200
r.Data = badges
return
}

View File

@ -44,7 +44,7 @@ func UserGET(md common.MethodData) (r common.Response) {
latestActivity := int64(0)
var badges string
var showcountry bool
err = md.DB.QueryRow("SELECT users.id, users.username, register_datetime, rank, latest_activity, users_stats.username_aka, users_stats.badges_shown, users_stats.country, users_stats.show_country FROM users LEFT JOIN users_stats ON users.id=users_stats.id WHERE users.id=?", uid).Scan(
err = md.DB.QueryRow("SELECT users.id, users.username, register_datetime, rank, latest_activity, users_stats.username_aka, users_stats.badges_shown, users_stats.country, users_stats.show_country FROM users LEFT JOIN users_stats ON users.id=users_stats.id WHERE users.id=? LIMIT 1", uid).Scan(
&user.ID, &user.Username, &registeredOn, &user.Rank, &latestActivity, &user.UsernameAKA, &badges, &user.Country, &showcountry)
switch {
case err == sql.ErrNoRows: