Finished first part of balance calculation

This commit is contained in:
2026-03-01 13:45:49 +01:00
parent 87ae8cc0b9
commit 188b238e7d
2 changed files with 71 additions and 3 deletions

View File

@@ -1,12 +1,32 @@
package handlers
import "net/http"
import (
"encoding/json"
"log"
"net/http"
"shap-planner-backend/storage"
)
func GetBalance(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
userParam := query.Get("user")
if userParam == "" {
if userParam == "all" {
// TODO: add later
} else {
balance, err := storage.ComputeBalance(userParam)
if err != nil {
log.Println("GET [api/balance] " + r.RemoteAddr + ": " + err.Error())
http.Error(w, "Invalid request query", http.StatusBadRequest)
return
}
err = json.NewEncoder(w).Encode(map[string]interface{}{
"balance": balance,
})
if err != nil {
log.Println("GET [api/balance] " + r.RemoteAddr + ": " + err.Error())
return
}
log.Println("GET [api/balance] " + r.RemoteAddr + ": Successfully retrieved balance")
}
}