package routing import ( lishwist "lishwist/core" "lishwist/http/response" "net/http" "github.com/Teajey/rsvp" ) func Users(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { admin := app.Admin() if admin == nil { return response.NotFound() } users, err := admin.ListUsers() if err != nil { return response.Error(http.StatusInternalServerError, "Failed to get users: %s", err) } return response.Data("", users) } func User(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { admin := app.Admin() if admin == nil { return response.NotFound() } reference := r.PathValue("userReference") user, err := lishwist.GetUserByReference(reference) if err != nil { return response.Error(http.StatusInternalServerError, "Failed to get user: %s", err) } if user == nil { return response.Error(http.StatusNotFound, "User not found") } return response.Data("", user) } func UserPost(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { admin := app.Admin() if admin == nil { return response.NotFound() } err := r.ParseForm() if err != nil { return response.Error(http.StatusBadRequest, "Failed to parse form") } reference := r.PathValue("userReference") if reference == app.User().Reference { return response.Error(http.StatusForbidden, "You cannot delete yourself.") } intent := r.Form.Get("intent") switch intent { case "delete": err = admin.UserSetLive(reference, false) if err != nil { return response.Error(http.StatusInternalServerError, "Failed to delete user: %s", err) } case "rename": name := r.Form.Get("display_name") err = admin.RenameUser(reference, name) if err != nil { return response.Error(http.StatusInternalServerError, "Failed to rename user: %s", err) } } user, err := lishwist.GetUserByReference(reference) if err != nil { return response.Error(http.StatusInternalServerError, "Failed to get user: %s", err) } return response.Data("", user) }