Implement GET scores in official ripple api

This commit is contained in:
Howl
2016-08-15 19:59:46 +02:00
parent 346f26177c
commit a6ca8de13e
16 changed files with 286 additions and 53 deletions

View File

@@ -25,6 +25,11 @@ func (md MethodData) ID() int {
return md.User.UserID
}
// Query is shorthand for md.C.Query.
func (md MethodData) Query(q string) string {
return md.C.Query(q)
}
// RequestData is the body of a request. It is wrapped into this type
// to implement the Unmarshal function, which is just a shorthand to
// json.Unmarshal.

View File

@@ -1,4 +1,60 @@
package common
import (
"errors"
"strconv"
"time"
)
// OsuTimeFormat is the time format for scores in the DB. Can be used with time.Parse etc.
const OsuTimeFormat = "060102150405"
// OsuTime is simply a time.Time, but can be used to convert an
// osu timestamp in the database into a native time.Time.
type OsuTime time.Time
func (s *OsuTime) setTime(t string) error {
newTime, err := time.Parse(OsuTimeFormat, t)
if _, ok := err.(*time.ParseError); err != nil && !ok {
return err
}
if err == nil {
*s = OsuTime(newTime)
}
return nil
}
// Scan decodes src into an OsuTime.
func (s *OsuTime) Scan(src interface{}) error {
if s == nil {
return errors.New("rippleapi/common: OsuTime is nil")
}
switch src := src.(type) {
case int64:
return s.setTime(strconv.FormatInt(src, 64))
case float64:
return s.setTime(strconv.FormatInt(int64(src), 64))
case string:
return s.setTime(src)
case []byte:
return s.setTime(string(src))
case nil:
// Nothing, leave zero value on timestamp
default:
return errors.New("rippleapi/common: unhandleable type")
}
return nil
}
// MarshalJSON -> time.Time.MarshalJSON
func (s OsuTime) MarshalJSON() ([]byte, error) {
return time.Time(s).MarshalJSON()
}
// UnmarshalJSON -> time.Time.UnmarshalJSON
func (s *OsuTime) UnmarshalJSON(x []byte) error {
t := new(time.Time)
err := t.UnmarshalJSON(x)
*s = OsuTime(*t)
return err
}

52
common/sort.go Normal file
View File

@@ -0,0 +1,52 @@
package common
import "strings"
// SortConfiguration is the configuration of Sort.
type SortConfiguration struct {
Allowed []string // Allowed parameters
Default string
DefaultSorting string // if empty, DESC
Table string
}
// Sort allows the request to modify how the query is sorted.
func Sort(md MethodData, config SortConfiguration) string {
if config.DefaultSorting == "" {
config.DefaultSorting = "DESC"
}
if config.Table != "" {
config.Table += "."
}
var sortBy string
for _, s := range md.C.Request.URL.Query()["sort"] {
sortParts := strings.Split(strings.ToLower(s), ",")
if contains(config.Allowed, sortParts[0]) {
if sortBy != "" {
sortBy += ", "
}
sortBy += config.Table + sortParts[0] + " "
if len(sortParts) > 1 && contains([]string{"asc", "desc"}, sortParts[1]) {
sortBy += sortParts[1]
} else {
sortBy += config.DefaultSorting
}
}
}
if sortBy == "" {
sortBy = config.Default
}
if sortBy == "" {
return ""
}
return "ORDER BY " + sortBy
}
func contains(a []string, s string) bool {
for _, el := range a {
if s == el {
return true
}
}
return false
}