initial commit
This commit is contained in:
commit
d40b69f1f9
58 changed files with 7919 additions and 0 deletions
47
backend/routes/user-get.go
Normal file
47
backend/routes/user-get.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue