Better endpoints + bugfix on friends online count

This commit is contained in:
gamray 2024-05-18 23:26:00 +02:00
parent 0b53bb87c3
commit 9b4e400558
3 changed files with 15 additions and 9 deletions

View File

@ -42,8 +42,8 @@ func Init(mux *http.ServeMux) error {
mux.HandleFunc("POST /account/login", handleAccountLogin)
mux.HandleFunc("POST /account/changepw", handleAccountChangePW)
mux.HandleFunc("GET /account/logout", handleAccountLogout)
mux.HandleFunc("POST /account/addfriend", handleAddFriend)
mux.HandleFunc("POST /account/removefriend", handleRemoveFriend)
mux.HandleFunc("POST /account/friends", handleAddFriend)
mux.HandleFunc("DELETE /account/friends", handleRemoveFriend)
mux.HandleFunc("GET /account/friendsonline", handleFriendsOnlineStat)
// game

View File

@ -160,19 +160,20 @@ func handleAddFriend(w http.ResponseWriter, r *http.Request) {
}
func handleRemoveFriend(w http.ResponseWriter, r *http.Request) {
formErr := r.ParseForm()
if formErr != nil {
httpError(w, r, fmt.Errorf("failed to parse request form: %s", formErr), http.StatusBadRequest)
return
}
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}
success, err := db.RemoveFriend(uuid, r.Form.Get("username"))
var friend string
if r.URL.Query().Has("username") {
friend = r.URL.Query().Get("username")
} else {
httpError(w, r, fmt.Errorf("No friend specified."), http.StatusBadRequest)
}
success, err := db.RemoveFriend(uuid, friend)
message := "Success"
if !success {
message = err.Error()

View File

@ -269,6 +269,11 @@ func FetchUUIDFromUsername(username string) ([]byte, error) {
var uuid []byte
err := handle.QueryRow("SELECT uuid FROM accounts WHERE username = ?", username).Scan(&uuid)
if err != nil {
if err == sql.ErrNoRows {
// No rows is not an error in this context, it signifies that the user does not exists
// we reserve err for db errors.
return uuid, nil
}
return nil, err
}