47 lines
1 KiB
Go
47 lines
1 KiB
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.red-panda.pet/pandaware/house/backend/middleware"
|
|
"git.red-panda.pet/pandaware/house/backend/router"
|
|
)
|
|
|
|
func init() {
|
|
routes["GET"]["/user/{id}"] = new(UserGET)
|
|
}
|
|
|
|
type UserGET struct{}
|
|
|
|
// ValidateParams implements router.ValidatedParamRoute.
|
|
func (u *UserGET) ValidateParams(ctx *router.Context) error {
|
|
if ctx.Parameter("id") == "me" {
|
|
return nil
|
|
}
|
|
return middleware.ValidateUUIDParam(ctx, "id")
|
|
}
|
|
|
|
// Authorize implements router.AuthorizedRoute.
|
|
func (u *UserGET) Authorize(ctx *router.Context) error {
|
|
return middleware.Authenticated(ctx)
|
|
}
|
|
|
|
// Handle implements router.Route.
|
|
func (u *UserGET) Handle(ctx *router.Context) error {
|
|
currentUser := middleware.User(u, ctx)
|
|
id := ctx.Parameter("id")
|
|
|
|
if id == "me" {
|
|
id = currentUser.ID
|
|
}
|
|
|
|
user, err := ctx.Query.GetUser(ctx, id)
|
|
if err != nil {
|
|
return ctx.Error(err, http.StatusNotFound, "user not found")
|
|
}
|
|
|
|
return ctx.JSON(200, user)
|
|
}
|
|
|
|
var _ router.AuthorizedRoute = new(UserGET)
|
|
var _ router.ValidatedParamRoute = new(UserGET)
|