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

@ -18,6 +18,7 @@ func (rt *_router) Handler() http.Handler {
rt.router.DELETE("/users/:user_id/followers/:follower_uid", rt.wrap(rt.DeleteFollow))
rt.router.GET("/users/:user_id/following", rt.wrap(rt.GetFollowersFollowing))
rt.router.GET("/users/:user_id/bans", rt.wrap(rt.GetUserBans))
rt.router.PUT("/users/:user_id/bans/:ban_uid", rt.wrap(rt.PutBan))
rt.router.DELETE("/users/:user_id/bans/:ban_uid", rt.wrap(rt.DeleteBan))

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")

View file

@ -44,3 +44,40 @@ func (rt *_router) GetUserProfile(w http.ResponseWriter, r *http.Request, ps htt
return
}
}
func (rt *_router) GetUserPhotos(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
// Get user id
uid := ps.ByName("user_id")
if !authorization.SendErrorIfNotLoggedIn(ctx.Auth.Authorized, rt.db, w, rt.baseLogger) ||
!helpers.SendNotFoundIfBanned(rt.db, ctx.Auth.GetUserID(), uid, w, rt.baseLogger) {
return
}
// Get limits, or use default values
start_index, limit, err := helpers.GetLimits(r.URL.Query())
if err != nil {
helpers.SendBadRequest(w, "Invalid start_index or limit value", rt.baseLogger)
return
}
// Get user photos
photos, err := rt.db.GetUserPhotos(uid, start_index, limit)
if err != nil {
helpers.SendInternalError(err, "Database error: GetUserPhotos", w, rt.baseLogger)
return
}
// Return user photos
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(photos)
if err != nil {
helpers.SendInternalError(err, "Error encoding json", w, rt.baseLogger)
return
}
}