Allow much more freedom to query for different things in /api/v1/beatmaps

This commit is contained in:
Howl 2016-08-23 16:27:09 +02:00
parent f35bb0a7e8
commit 42011ad10c
2 changed files with 64 additions and 13 deletions

View File

@ -82,23 +82,16 @@ func BeatmapSetStatusPOST(md common.MethodData) common.CodeMessager {
SET ranked = ?, ranked_status_freezed = ? SET ranked = ?, ranked_status_freezed = ?
WHERE beatmapset_id = ?`, req.RankedStatus, req.Frozen, param) WHERE beatmapset_id = ?`, req.RankedStatus, req.Frozen, param)
return getSet(md, param) return getMultipleBeatmaps(md)
} }
// BeatmapGET retrieves a beatmap. // BeatmapGET retrieves a beatmap.
func BeatmapGET(md common.MethodData) common.CodeMessager { func BeatmapGET(md common.MethodData) common.CodeMessager {
if md.Query("s") == "" && md.Query("b") == "" {
return common.SimpleResponse(400, "Must pass either querystring param 'b' or 's'")
}
setID := common.Int(md.Query("s"))
if setID != 0 {
return getSet(md, setID)
}
beatmapID := common.Int(md.Query("b")) beatmapID := common.Int(md.Query("b"))
if beatmapID != 0 { if beatmapID != 0 {
return getBeatmap(md, beatmapID) return getBeatmapSingle(md, beatmapID)
} }
return common.SimpleResponse(400, "Please pass either a valid beatmapset ID or a valid beatmap ID") return getMultipleBeatmaps(md)
} }
const baseBeatmapSelect = ` const baseBeatmapSelect = `
@ -111,8 +104,33 @@ SELECT
FROM beatmaps FROM beatmaps
` `
func getSet(md common.MethodData, setID int) common.CodeMessager { func getMultipleBeatmaps(md common.MethodData) common.CodeMessager {
rows, err := md.DB.Query(baseBeatmapSelect+"WHERE beatmapset_id = ?", setID) sort := common.Sort(md, common.SortConfiguration{
Allowed: []string{
"beatmapset_id",
"beatmap_id",
"id",
"ar",
"od",
"difficulty_std",
"difficulty_taiko",
"difficulty_ctb",
"difficulty_mania",
"max_combo",
"latest_update",
"playcount",
"passcount",
},
Default: "id DESC",
Table: "beatmaps",
})
where := common.Where("beatmapsetid = ?", md.Query("s")).
Where("song_name = ?", md.Query("song_name")).
Where("ranked_status_freezed = ?", md.Query("ranked_status_frozen"), "0", "1")
rows, err := md.DB.Query(baseBeatmapSelect+
where.Clause+" "+sort+" "+
common.Paginate(md.Query("p"), md.Query("l"), 50), where.Params...)
if err != nil { if err != nil {
md.Err(err) md.Err(err)
return Err500 return Err500
@ -138,7 +156,7 @@ func getSet(md common.MethodData, setID int) common.CodeMessager {
return r return r
} }
func getBeatmap(md common.MethodData, beatmapID int) common.CodeMessager { func getBeatmapSingle(md common.MethodData, beatmapID int) common.CodeMessager {
var b beatmap var b beatmap
err := md.DB.QueryRow(baseBeatmapSelect+"WHERE beatmap_id = ? LIMIT 1", beatmapID).Scan( err := md.DB.QueryRow(baseBeatmapSelect+"WHERE beatmap_id = ? LIMIT 1", beatmapID).Scan(
&b.BeatmapID, &b.BeatmapsetID, &b.BeatmapMD5, &b.BeatmapID, &b.BeatmapsetID, &b.BeatmapMD5,

33
common/where.go Normal file
View File

@ -0,0 +1,33 @@
package common
// WhereClause is a struct representing a where clause.
// This is made to easily create WHERE clauses from parameters passed from a request.
type WhereClause struct {
Clause string
Params []interface{}
}
// Where adds a new WHERE clause to the WhereClause.
func (w *WhereClause) Where(clause, passedParam string, allowedValues ...string) *WhereClause {
if passedParam == "" {
return w
}
if len(allowedValues) != 0 && !contains(allowedValues, passedParam) {
return w
}
// checks passed, if string is empty add "WHERE"
if w.Clause == "" {
w.Clause += "WHERE "
} else {
w.Clause += " AND "
}
w.Clause += clause
w.Params = append(w.Params, passedParam)
return w
}
// Where is the same as WhereClause.Where, but creates a new WhereClause.
func Where(clause, passedParam string, allowedValues ...string) *WhereClause {
w := new(WhereClause)
return w.Where(clause, passedParam, allowedValues...)
}