Merge /users/id/:id and /users/name/:name into /users (with querystring params id and name)
This commit is contained in:
parent
1b1f2503a2
commit
3a199b8c41
|
@ -27,8 +27,7 @@ func Start(conf common.Conf, db *sql.DB) *gin.Engine {
|
||||||
gv1.GET("/privileges", Method(v1.PrivilegesGET, db))
|
gv1.GET("/privileges", Method(v1.PrivilegesGET, db))
|
||||||
|
|
||||||
// Read privilege required
|
// Read privilege required
|
||||||
gv1.GET("/users/id/:id", Method(v1.UserByIDGET, db, common.PrivilegeRead))
|
gv1.GET("/users", Method(v1.UsersGET, db, common.PrivilegeRead))
|
||||||
gv1.GET("/users/name/:name", Method(v1.UserByNameGET, 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/:username", Method(v1.UserWhatsTheIDGET, db, common.PrivilegeRead))
|
gv1.GET("/users/whatid/:username", Method(v1.UserWhatsTheIDGET, db, common.PrivilegeRead))
|
||||||
gv1.GET("/users/full/:id", Method(v1.UserFullGET, db, common.PrivilegeRead))
|
gv1.GET("/users/full/:id", Method(v1.UserFullGET, db, common.PrivilegeRead))
|
||||||
|
|
|
@ -3,13 +3,11 @@ package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.zxq.co/ripple/rippleapi/common"
|
"git.zxq.co/ripple/rippleapi/common"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type userData struct {
|
type userData struct {
|
||||||
|
@ -23,18 +21,27 @@ type userData struct {
|
||||||
Country string `json:"country"`
|
Country string `json:"country"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserByIDGET is the API handler for GET /users/id/:id
|
// UsersGET is the API handler for GET /users
|
||||||
func UserByIDGET(md common.MethodData) common.CodeMessager {
|
func UsersGET(md common.MethodData) common.CodeMessager {
|
||||||
var err error
|
var err error
|
||||||
var uid int
|
var whereClause string
|
||||||
uidStr := md.C.Param("id")
|
var param interface{}
|
||||||
if uidStr == "self" {
|
|
||||||
uid = md.ID()
|
switch {
|
||||||
} else {
|
case md.C.Query("id") == "self":
|
||||||
uid, err = strconv.Atoi(uidStr)
|
param = md.ID()
|
||||||
|
whereClause = "users.id = ?"
|
||||||
|
case md.C.Query("id") != "":
|
||||||
|
param, err = strconv.Atoi(md.C.Query("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.SimpleResponse(400, fmt.Sprintf("%s ain't a number", uidStr))
|
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 := `
|
||||||
|
@ -44,25 +51,9 @@ SELECT users.id, users.username, register_datetime, rank,
|
||||||
FROM users
|
FROM users
|
||||||
LEFT JOIN users_stats
|
LEFT JOIN users_stats
|
||||||
ON users.id=users_stats.id
|
ON users.id=users_stats.id
|
||||||
WHERE users.id=? AND users.allowed='1'
|
WHERE ` + whereClause + ` AND users.allowed='1'
|
||||||
LIMIT 1`
|
LIMIT 1`
|
||||||
return userPuts(md, md.DB.QueryRow(query, uid))
|
return userPuts(md, md.DB.QueryRow(query, param))
|
||||||
}
|
|
||||||
|
|
||||||
// UserByNameGET is the API handler for GET /users/name/:name
|
|
||||||
func UserByNameGET(md common.MethodData) common.CodeMessager {
|
|
||||||
username := md.C.Param("name")
|
|
||||||
|
|
||||||
query := `
|
|
||||||
SELECT users.id, users.username, register_datetime, rank,
|
|
||||||
latest_activity, users_stats.username_aka,
|
|
||||||
users_stats.country, users_stats.show_country
|
|
||||||
FROM users
|
|
||||||
LEFT JOIN users_stats
|
|
||||||
ON users.id=users_stats.id
|
|
||||||
WHERE users.username=? AND users.allowed='1'
|
|
||||||
LIMIT 1`
|
|
||||||
return userPuts(md, md.DB.QueryRow(query, username))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func userPuts(md common.MethodData, row *sql.Row) common.CodeMessager {
|
func userPuts(md common.MethodData, row *sql.Row) common.CodeMessager {
|
||||||
|
@ -119,11 +110,8 @@ func genCountry(md common.MethodData, uid int, showCountry bool, country string)
|
||||||
|
|
||||||
// UserSelfGET is a shortcut for /users/id/self. (/users/self)
|
// UserSelfGET is a shortcut for /users/id/self. (/users/self)
|
||||||
func UserSelfGET(md common.MethodData) common.CodeMessager {
|
func UserSelfGET(md common.MethodData) common.CodeMessager {
|
||||||
md.C.Params = append(md.C.Params, gin.Param{
|
md.C.Request.URL.RawQuery = "id=self&" + md.C.Request.URL.RawQuery
|
||||||
Key: "id",
|
return UsersGET(md)
|
||||||
Value: "self",
|
|
||||||
})
|
|
||||||
return UserByIDGET(md)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type whatIDResponse struct {
|
type whatIDResponse struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user