Use custom type UnixTimestamp instead of repeating boilerplate code over and over.

This commit is contained in:
Howl
2016-07-06 22:32:30 +02:00
parent ff1d2fa1c3
commit 09523369b7
6 changed files with 95 additions and 76 deletions

View File

@@ -5,20 +5,19 @@ import (
"database/sql"
"strconv"
"strings"
"time"
"git.zxq.co/ripple/ocl"
"git.zxq.co/ripple/rippleapi/common"
)
type userData struct {
ID int `json:"id"`
Username string `json:"username"`
UsernameAKA string `json:"username_aka"`
RegisteredOn time.Time `json:"registered_on"`
Privileges uint64 `json:"privileges"`
LatestActivity time.Time `json:"latest_activity"`
Country string `json:"country"`
ID int `json:"id"`
Username string `json:"username"`
UsernameAKA string `json:"username_aka"`
RegisteredOn common.UnixTimestamp `json:"registered_on"`
Privileges uint64 `json:"privileges"`
LatestActivity common.UnixTimestamp `json:"latest_activity"`
Country string `json:"country"`
}
// UsersGET is the API handler for GET /users
@@ -49,12 +48,11 @@ func userPuts(md common.MethodData, row *sql.Row) common.CodeMessager {
var err error
var user userPutsUserData
var (
registeredOn int64
latestActivity int64
showCountry bool
var showCountry bool
err = row.Scan(
&user.ID, &user.Username, &user.RegisteredOn, &user.Privileges, &user.LatestActivity,
&user.UsernameAKA, &user.Country, &showCountry,
)
err = row.Scan(&user.ID, &user.Username, &registeredOn, &user.Privileges, &latestActivity, &user.UsernameAKA, &user.Country, &showCountry)
switch {
case err == sql.ErrNoRows:
return common.SimpleResponse(404, "No such user was found!")
@@ -63,9 +61,6 @@ func userPuts(md common.MethodData, row *sql.Row) common.CodeMessager {
return Err500
}
user.RegisteredOn = time.Unix(registeredOn, 0)
user.LatestActivity = time.Unix(latestActivity, 0)
user.Country = genCountry(md, user.ID, showCountry, user.Country)
user.Code = 200
@@ -191,14 +186,12 @@ LIMIT 1
// Fuck.
r := userFullResponse{}
var (
badges string
country string
showCountry bool
registeredOn int64
latestActivity int64
badges string
country string
showCountry bool
)
err := md.DB.QueryRow(query, param).Scan(
&r.ID, &r.Username, &registeredOn, &r.Privileges, &latestActivity,
&r.ID, &r.Username, &r.RegisteredOn, &r.Privileges, &r.LatestActivity,
&r.UsernameAKA, &badges, &country, &showCountry,
&r.PlayStyle, &r.FavouriteMode,
@@ -230,9 +223,6 @@ LIMIT 1
r.Country = genCountry(md, r.ID, showCountry, country)
r.Badges = badgesToArray(badges)
r.RegisteredOn = time.Unix(registeredOn, 0)
r.LatestActivity = time.Unix(latestActivity, 0)
for _, m := range []*modeData{&r.STD, &r.Taiko, &r.CTB, &r.Mania} {
m.Level = ocl.GetLevelPrecise(int64(m.TotalScore))
}