diff --git a/app/start.go b/app/start.go index 112dd5f..d34a5cd 100644 --- a/app/start.go +++ b/app/start.go @@ -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 diff --git a/app/v1/self.go b/app/v1/self.go index 16296d9..e3ad005 100644 --- a/app/v1/self.go +++ b/app/v1/self.go @@ -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 +} diff --git a/common/update.go b/common/update.go new file mode 100644 index 0000000..962304d --- /dev/null +++ b/common/update.go @@ -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, ", ") +}