Add update username method

This commit is contained in:
Marco Realacci 2022-11-18 17:18:46 +01:00
parent 626b7fa3e9
commit 7c2c993dc3
10 changed files with 82 additions and 9 deletions

View file

@ -1,14 +1,35 @@
package api
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/notherealmarco/WASAPhoto/service/api/authorization"
"github.com/notherealmarco/WASAPhoto/service/api/reqcontext"
"github.com/notherealmarco/WASAPhoto/service/structures"
)
func (rt *_router) UpdateUsername(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
auth, err := ctx.Auth.UserAuthorized(rt.db, r.URL.Path // todo: prendere il coso giusto dal path)
uid := ps.ByName("user_id")
if !authorization.SendAuthorizationError(ctx.Auth.UserAuthorized, uid, rt.db, w) {
return
}
var req structures.UserDetails
err := json.NewDecoder(r.Body).Decode(&req) //todo: capire se serve close
if err != nil {
w.WriteHeader(http.StatusBadRequest) // todo: move to DecodeOrBadRequest helper
return
}
err = rt.db.UpdateUsername(uid, req.Name)
if err != nil {
w.WriteHeader(http.StatusInternalServerError) // todo: is not ok, maybe let's use a helper
return
}
w.WriteHeader(http.StatusNoContent) // todo: change to 204 also in API spec
}