74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package routing
|
|
|
|
import (
|
|
lishwist "lishwist/core"
|
|
"lishwist/http/rsvp"
|
|
"net/http"
|
|
)
|
|
|
|
func Users(app *lishwist.Session, h http.Header, r *rsvp.Request) rsvp.Response {
|
|
admin := app.Admin()
|
|
if admin == nil {
|
|
return NotFound(h, r)
|
|
}
|
|
|
|
users, err := admin.ListUsers()
|
|
if err != nil {
|
|
return rsvp.Error(http.StatusInternalServerError, "Failed to get users: %s", err)
|
|
}
|
|
|
|
return rsvp.Data("", users)
|
|
}
|
|
|
|
func User(app *lishwist.Session, h http.Header, r *rsvp.Request) rsvp.Response {
|
|
admin := app.Admin()
|
|
if admin == nil {
|
|
return NotFound(h, r)
|
|
}
|
|
|
|
reference := r.PathValue("userReference")
|
|
|
|
user, err := lishwist.GetUserByReference(reference)
|
|
if err != nil {
|
|
return rsvp.Error(http.StatusInternalServerError, "Failed to get user: %s", err)
|
|
}
|
|
if user == nil {
|
|
return rsvp.Error(http.StatusNotFound, "User not found")
|
|
}
|
|
|
|
return rsvp.Data("", user)
|
|
}
|
|
|
|
func UserPost(app *lishwist.Session, h http.Header, r *rsvp.Request) rsvp.Response {
|
|
admin := app.Admin()
|
|
if admin == nil {
|
|
return NotFound(h, r)
|
|
}
|
|
|
|
form := r.ParseForm()
|
|
|
|
reference := r.PathValue("userReference")
|
|
if reference == app.User.Reference {
|
|
return rsvp.Error(http.StatusForbidden, "You cannot delete yourself.")
|
|
}
|
|
|
|
user, err := lishwist.GetUserByReference(reference)
|
|
if err != nil {
|
|
return rsvp.Error(http.StatusInternalServerError, "Failed to get user: %s", err)
|
|
}
|
|
if user == nil {
|
|
return rsvp.Error(http.StatusNotFound, "User not found")
|
|
}
|
|
|
|
intent := form.Get("intent")
|
|
|
|
if intent != "" {
|
|
err = admin.UserSetLive(reference, intent != "delete")
|
|
if err != nil {
|
|
return rsvp.Error(http.StatusInternalServerError, "Failed to delete user: %s", err)
|
|
}
|
|
}
|
|
|
|
return rsvp.Data("", user)
|
|
}
|