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

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...)
}