add UsersSelfSettingsGET and POST

This commit is contained in:
Howl 2016-11-06 22:51:21 +01:00
parent 9de913888d
commit 6c8d7baee2
3 changed files with 106 additions and 0 deletions

View File

@ -112,10 +112,12 @@ func Start(conf common.Conf, dbO *sqlx.DB) *gin.Engine {
gv1.GET("/friends/with", Method(v1.FriendsWithGET, common.PrivilegeReadConfidential))
gv1.GET("/users/self/donor_info", Method(v1.UsersSelfDonorInfoGET, common.PrivilegeReadConfidential))
gv1.GET("/users/self/favourite_mode", Method(v1.UsersSelfFavouriteModeGET, common.PrivilegeReadConfidential))
gv1.GET("/users/self/settings", Method(v1.UsersSelfSettingsGET, common.PrivilegeReadConfidential))
// Write privilege required
gv1.GET("/friends/add", Method(v1.FriendsAddGET, common.PrivilegeWrite))
gv1.GET("/friends/del", Method(v1.FriendsDelGET, common.PrivilegeWrite))
gv1.POST("/users/self/settings", Method(v1.UsersSelfSettingsPOST, common.PrivilegeWrite))
//gv1.POST("/beatmaps/rank_requests", Method(v1.BeatmapRankRequestsSubmitPOST, common.PrivilegeWrite))
// Admin: beatmap

View File

@ -43,3 +43,75 @@ func UsersSelfFavouriteModeGET(md common.MethodData) common.CodeMessager {
}
return f
}
type userSettingsData struct {
Email string `json:"email"`
UsernameAKA string `json:"username_aka"`
FavouriteMode *int `json:"favourite_mode"`
CustomBadge struct {
singleBadge
Show *bool `json:"show"`
} `json:"custom_badge"`
PlayStyle *int `json:"play_style"`
}
// UsersSelfSettingsPOST allows to modify information about the current user.
func UsersSelfSettingsPOST(md common.MethodData) common.CodeMessager {
var d userSettingsData
md.RequestData.Unmarshal(&d)
q := new(common.UpdateQuery).
Add("u.email", d.Email).
Add("s.username_aka", d.UsernameAKA).
Add("s.favourite_mode", d.FavouriteMode).
Add("s.custom_badge_name", d.CustomBadge.Name).
Add("s.custom_badge_icon", d.CustomBadge.Icon).
Add("s.show_custom_badge", d.CustomBadge.Show).
Add("s.play_style", d.PlayStyle)
_, err := md.DB.Exec("UPDATE users u, users_stats s SET "+q.Fields()+" WHERE s.id = u.id AND u.id = ?", append(q.Parameters, md.ID())...)
if err != nil {
md.Err(err)
return Err500
}
return UsersSelfSettingsGET(md)
}
type userSettingsResponse struct {
common.ResponseBase
ID int `json:"id"`
Username string `json:"username"`
userSettingsData
}
// UsersSelfSettingsGET allows to get "sensitive" information about the current user.
func UsersSelfSettingsGET(md common.MethodData) common.CodeMessager {
var r userSettingsResponse
var ccb bool
r.Code = 200
err := md.DB.QueryRow(`
SELECT
u.id, u.username,
u.email, s.username_aka, s.favourite_mode,
s.show_custom_badge, s.custom_badge_icon,
s.custom_badge_name, s.can_custom_badge,
s.play_style
FROM users u
LEFT JOIN users_stats s ON u.id = s.id
WHERE u.id = ?`, md.ID()).Scan(
&r.ID, &r.Username,
&r.Email, &r.UsernameAKA, &r.FavouriteMode,
&r.CustomBadge.Show, &r.CustomBadge.Icon,
&r.CustomBadge.Name, &ccb,
&r.PlayStyle,
)
if err != nil {
md.Err(err)
return Err500
}
if !ccb {
r.CustomBadge = struct {
singleBadge
Show *bool `json:"show"`
}{}
}
return r
}

32
common/update.go Normal file
View File

@ -0,0 +1,32 @@
package common
import (
"reflect"
"strings"
)
// UpdateQuery is simply an SQL update query,
// that can be built upon passed parameters.
type UpdateQuery struct {
fields []string
Parameters []interface{}
}
// Add adds a new field with correspective value to UpdateQuery
func (u *UpdateQuery) Add(field string, value interface{}) *UpdateQuery {
val := reflect.ValueOf(value)
if val.Kind() == reflect.Ptr && val.IsNil() {
return u
}
if s, ok := value.(string); ok && s == "" {
return u
}
u.fields = append(u.fields, field+" = ?")
u.Parameters = append(u.Parameters, value)
return u
}
// Fields retrieves the fields joined by a comma.
func (u *UpdateQuery) Fields() string {
return strings.Join(u.fields, ", ")
}