Add GET bans

This commit is contained in:
Marco Realacci 2022-11-22 16:43:46 +01:00
parent 77e9f405c6
commit 4f391f0b65
7 changed files with 117 additions and 10 deletions

View file

@ -1,6 +1,7 @@
package api
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
@ -10,6 +11,45 @@ import (
"github.com/notherealmarco/WASAPhoto/service/database"
)
func (rt *_router) GetUserBans(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
// Get user id
uid := ps.ByName("user_id")
if !authorization.SendAuthorizationError(ctx.Auth.UserAuthorized, uid, rt.db, w, rt.baseLogger, http.StatusNotFound) {
// A user should not be able to see other users' bans
return
}
// Get limits, or use default values
start_index, limit, err := helpers.GetLimits(r.URL.Query())
if err != nil {
// Send error if the limits are specified but invalid
helpers.SendBadRequest(w, "Invalid start_index or limit value", rt.baseLogger)
return
}
// Get bans
// We don't need to check if the user exists, because the authorization middleware already did that
bans, err := rt.db.GetUserBans(uid, start_index, limit)
if err != nil {
helpers.SendInternalError(err, "Database error: GetUserBans", w, rt.baseLogger)
return
}
// Return ban list
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) // is it needed?
json.NewEncoder(w).Encode(bans)
if err != nil {
helpers.SendInternalError(err, "Error encoding json", w, rt.baseLogger)
return
}
}
func (rt *_router) PutBan(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
uid := ps.ByName("user_id")