Better JSON response for /account/addfriend

This commit is contained in:
gamray 2024-05-18 12:27:47 +02:00
parent 2899416842
commit 7990f0978a
3 changed files with 35 additions and 6 deletions

View File

@ -145,10 +145,15 @@ func handleAddFriend(w http.ResponseWriter, r *http.Request) {
return
}
response, err := db.AddFriend(uuid, r.Form.Get("username"))
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
success, err := db.AddFriend(uuid, r.Form.Get("username"))
message := "Success"
if !success {
message = err.Error()
}
response := defs.GenericResponse{
Success: success,
Message: message,
}
jsonResponse(w, r, response)

View File

@ -276,15 +276,16 @@ func isFriendWith(sourceUsername string, friendUsername string) (bool, error) {
}
func AddFriend(uuid []byte, friendUsername string) (bool, error) {
// We are making db errors more generic as error is used in the response data to the client.
username, err := FetchUsernameFromUUID(uuid);
if err != nil {
return false, err
return false, fmt.Errorf("An error occured, are you connected ?")
}
var doesUserExist int
err = handle.QueryRow("SELECT COUNT(*) FROM accounts WHERE username = ?", friendUsername).Scan(&doesUserExist)
if err != nil {
return false, err
return false, fmt.Errorf("An error occured, if this persist, please contact an administrators.")
}
if doesUserExist == 0 {

23
defs/common.go Normal file
View File

@ -0,0 +1,23 @@
/*
Copyright (C) 2024 Pagefault Games
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package defs
type GenericResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}