/users/full now using querystring. /users requests from now on will have an uniform system for getting the user (?id=self | ?id=1337 | ?name=Howl)
This commit is contained in:
parent
8114329d38
commit
09582361a3
|
@ -30,7 +30,7 @@ func Start(conf common.Conf, db *sql.DB) *gin.Engine {
|
||||||
gv1.GET("/users", Method(v1.UsersGET, db, common.PrivilegeRead))
|
gv1.GET("/users", Method(v1.UsersGET, db, common.PrivilegeRead))
|
||||||
gv1.GET("/users/self", Method(v1.UserSelfGET, db, common.PrivilegeRead))
|
gv1.GET("/users/self", Method(v1.UserSelfGET, db, common.PrivilegeRead))
|
||||||
gv1.GET("/users/whatid", Method(v1.UserWhatsTheIDGET, db, common.PrivilegeRead))
|
gv1.GET("/users/whatid", Method(v1.UserWhatsTheIDGET, db, common.PrivilegeRead))
|
||||||
gv1.GET("/users/full/:id", Method(v1.UserFullGET, db, common.PrivilegeRead))
|
gv1.GET("/users/full", Method(v1.UserFullGET, db, common.PrivilegeRead))
|
||||||
gv1.GET("/users/userpage/:id", Method(v1.UserUserpageGET, db, common.PrivilegeRead))
|
gv1.GET("/users/userpage/:id", Method(v1.UserUserpageGET, db, common.PrivilegeRead))
|
||||||
gv1.GET("/badges", Method(v1.BadgesGET, db, common.PrivilegeRead))
|
gv1.GET("/badges", Method(v1.BadgesGET, db, common.PrivilegeRead))
|
||||||
gv1.GET("/badges/:id", Method(v1.BadgeByIDGET, db, common.PrivilegeRead))
|
gv1.GET("/badges/:id", Method(v1.BadgeByIDGET, db, common.PrivilegeRead))
|
||||||
|
|
|
@ -23,25 +23,9 @@ type userData struct {
|
||||||
|
|
||||||
// UsersGET is the API handler for GET /users
|
// UsersGET is the API handler for GET /users
|
||||||
func UsersGET(md common.MethodData) common.CodeMessager {
|
func UsersGET(md common.MethodData) common.CodeMessager {
|
||||||
var err error
|
shouldRet, whereClause, param := whereClauseUser(md)
|
||||||
var whereClause string
|
if shouldRet != nil {
|
||||||
var param interface{}
|
return *shouldRet
|
||||||
|
|
||||||
switch {
|
|
||||||
case md.C.Query("id") == "self":
|
|
||||||
param = md.ID()
|
|
||||||
whereClause = "users.id = ?"
|
|
||||||
case md.C.Query("id") != "":
|
|
||||||
param, err = strconv.Atoi(md.C.Query("id"))
|
|
||||||
if err != nil {
|
|
||||||
return common.SimpleResponse(400, "passed user ID is not a valid number")
|
|
||||||
}
|
|
||||||
whereClause = "users.id = ?"
|
|
||||||
case md.C.Query("name") != "":
|
|
||||||
param = md.C.Query("name")
|
|
||||||
whereClause = "users.username = ?"
|
|
||||||
default:
|
|
||||||
return common.SimpleResponse(400, "must provide either querystring param id or param name")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
|
@ -157,6 +141,11 @@ type userFullResponse struct {
|
||||||
|
|
||||||
// UserFullGET gets all of an user's information, with one exception: their userpage.
|
// UserFullGET gets all of an user's information, with one exception: their userpage.
|
||||||
func UserFullGET(md common.MethodData) common.CodeMessager {
|
func UserFullGET(md common.MethodData) common.CodeMessager {
|
||||||
|
shouldRet, whereClause, param := whereClauseUser(md)
|
||||||
|
if shouldRet != nil {
|
||||||
|
return *shouldRet
|
||||||
|
}
|
||||||
|
|
||||||
// Hellest query I've ever done.
|
// Hellest query I've ever done.
|
||||||
query := `
|
query := `
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -192,7 +181,7 @@ LEFT JOIN leaderboard_ctb
|
||||||
ON users.id=leaderboard_ctb.user
|
ON users.id=leaderboard_ctb.user
|
||||||
LEFT JOIN leaderboard_mania
|
LEFT JOIN leaderboard_mania
|
||||||
ON users.id=leaderboard_mania.user
|
ON users.id=leaderboard_mania.user
|
||||||
WHERE users.id=? AND users.allowed = '1'
|
WHERE ` + whereClause + ` AND users.allowed = '1'
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`
|
`
|
||||||
// Fuck.
|
// Fuck.
|
||||||
|
@ -204,7 +193,7 @@ LIMIT 1
|
||||||
registeredOn int64
|
registeredOn int64
|
||||||
latestActivity int64
|
latestActivity int64
|
||||||
)
|
)
|
||||||
err := md.DB.QueryRow(query, md.C.Param("id")).Scan(
|
err := md.DB.QueryRow(query, param).Scan(
|
||||||
&r.ID, &r.Username, ®isteredOn, &r.Rank, &latestActivity,
|
&r.ID, &r.Username, ®isteredOn, &r.Rank, &latestActivity,
|
||||||
|
|
||||||
&r.UsernameAKA, &badges, &country, &showCountry,
|
&r.UsernameAKA, &badges, &country, &showCountry,
|
||||||
|
@ -263,3 +252,21 @@ func UserUserpageGET(md common.MethodData) common.CodeMessager {
|
||||||
r.Code = 200
|
r.Code = 200
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func whereClauseUser(md common.MethodData) (*common.CodeMessager, string, interface{}) {
|
||||||
|
switch {
|
||||||
|
case md.C.Query("id") == "self":
|
||||||
|
return nil, "users.id = ?", md.ID()
|
||||||
|
case md.C.Query("id") != "":
|
||||||
|
id, err := strconv.Atoi(md.C.Query("id"))
|
||||||
|
if err != nil {
|
||||||
|
a := common.SimpleResponse(400, "please pass a valid user ID")
|
||||||
|
return &a, "", nil
|
||||||
|
}
|
||||||
|
return nil, "users.id = ?", id
|
||||||
|
case md.C.Query("name") != "":
|
||||||
|
return nil, "users.username = ?", md.C.Query("name")
|
||||||
|
}
|
||||||
|
a := common.SimpleResponse(400, "you need to pass either querystring parameters name or id")
|
||||||
|
return &a, "", nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user