2016-04-03 20:31:41 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2016-04-19 14:07:27 +00:00
|
|
|
"git.zxq.co/ripple/rippleapi/common"
|
2016-04-03 20:31:41 +00:00
|
|
|
)
|
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
type singleBadge struct {
|
2016-04-03 20:31:41 +00:00
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Icon string `json:"icon"`
|
|
|
|
}
|
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
type badgeData struct {
|
|
|
|
common.ResponseBase
|
|
|
|
singleBadge
|
|
|
|
}
|
|
|
|
|
2016-04-03 20:31:41 +00:00
|
|
|
// BadgeByIDGET is the handler for /badge/:id
|
2016-04-16 16:05:24 +00:00
|
|
|
func BadgeByIDGET(md common.MethodData) common.CodeMessager {
|
|
|
|
var b badgeData
|
2016-04-03 20:31:41 +00:00
|
|
|
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:
|
2016-04-16 16:05:24 +00:00
|
|
|
return common.SimpleResponse(404, "No such badge was found")
|
2016-04-03 20:31:41 +00:00
|
|
|
case err != nil:
|
2016-04-08 17:05:54 +00:00
|
|
|
md.Err(err)
|
2016-04-16 16:05:24 +00:00
|
|
|
return Err500
|
2016-04-03 20:31:41 +00:00
|
|
|
}
|
2016-04-16 16:05:24 +00:00
|
|
|
b.Code = 200
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
var r multiBadgeData
|
2016-04-03 20:31:41 +00:00
|
|
|
rows, err := md.DB.Query("SELECT id, name, icon FROM badges")
|
|
|
|
if err != nil {
|
2016-04-08 17:05:54 +00:00
|
|
|
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 {
|
2016-04-08 17:05:54 +00:00
|
|
|
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 {
|
2016-04-08 17:05:54 +00:00
|
|
|
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
|
|
|
}
|