homebox/backend/app/api/v1/v1_ctrl_user.go

122 lines
3.5 KiB
Go
Raw Normal View History

2022-08-30 02:30:36 +00:00
package v1
import (
"net/http"
2022-08-30 18:05:11 +00:00
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/repo"
"github.com/hay-kot/homebox/backend/internal/services"
"github.com/hay-kot/homebox/backend/pkgs/server"
2022-09-03 18:38:35 +00:00
"github.com/rs/zerolog/log"
2022-08-30 02:30:36 +00:00
)
2022-08-30 18:05:11 +00:00
// HandleUserSelf godoc
// @Summary Get the current user
// @Tags User
// @Produce json
// @Param payload body services.UserRegistration true "User Data"
2022-08-31 05:22:01 +00:00
// @Success 204
2022-08-30 18:05:11 +00:00
// @Router /v1/users/register [Post]
func (ctrl *V1Controller) HandleUserRegistration() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
regData := services.UserRegistration{}
2022-08-30 18:05:11 +00:00
if err := server.Decode(r, &regData); err != nil {
2022-09-03 18:38:35 +00:00
log.Err(err).Msg("failed to decode user registration data")
2022-08-30 18:05:11 +00:00
server.RespondError(w, http.StatusInternalServerError, err)
return
}
2022-08-31 00:40:39 +00:00
_, err := ctrl.svc.User.RegisterUser(r.Context(), regData)
2022-08-30 18:05:11 +00:00
if err != nil {
log.Err(err).Msg("failed to register user")
2022-08-30 18:05:11 +00:00
server.RespondError(w, http.StatusInternalServerError, err)
return
}
2022-08-31 00:40:39 +00:00
server.Respond(w, http.StatusNoContent, nil)
2022-08-30 18:05:11 +00:00
}
}
2022-08-30 02:30:36 +00:00
// HandleUserSelf godoc
// @Summary Get the current user
// @Tags User
// @Produce json
// @Success 200 {object} server.Result{item=repo.UserOut}
2022-08-30 02:30:36 +00:00
// @Router /v1/users/self [GET]
// @Security Bearer
func (ctrl *V1Controller) HandleUserSelf() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := services.UseTokenCtx(r.Context())
usr, err := ctrl.svc.User.GetSelf(r.Context(), token)
2022-08-30 18:05:11 +00:00
if usr.ID == uuid.Nil || err != nil {
2022-09-03 18:38:35 +00:00
log.Err(err).Msg("failed to get user")
2022-08-31 00:40:39 +00:00
server.RespondServerError(w)
2022-08-30 02:30:36 +00:00
return
}
2022-08-31 00:40:39 +00:00
server.Respond(w, http.StatusOK, server.Wrap(usr))
2022-08-30 02:30:36 +00:00
}
}
2022-09-04 02:42:03 +00:00
// HandleUserSelfUpdate godoc
2022-08-30 02:30:36 +00:00
// @Summary Update the current user
// @Tags User
// @Produce json
// @Param payload body repo.UserUpdate true "User Data"
// @Success 200 {object} server.Result{item=repo.UserUpdate}
2022-08-30 02:30:36 +00:00
// @Router /v1/users/self [PUT]
// @Security Bearer
2022-09-04 02:42:03 +00:00
func (ctrl *V1Controller) HandleUserSelfUpdate() http.HandlerFunc {
2022-08-30 02:30:36 +00:00
return func(w http.ResponseWriter, r *http.Request) {
updateData := repo.UserUpdate{}
2022-08-30 02:30:36 +00:00
if err := server.Decode(r, &updateData); err != nil {
2022-09-03 18:38:35 +00:00
log.Err(err).Msg("failed to decode user update data")
2022-08-30 02:30:36 +00:00
server.RespondError(w, http.StatusBadRequest, err)
return
}
actor := services.UseUserCtx(r.Context())
newData, err := ctrl.svc.User.UpdateSelf(r.Context(), actor.ID, updateData)
if err != nil {
2022-09-03 18:38:35 +00:00
2022-08-30 02:30:36 +00:00
server.RespondError(w, http.StatusInternalServerError, err)
return
}
2022-08-31 00:40:39 +00:00
server.Respond(w, http.StatusOK, server.Wrap(newData))
2022-08-30 02:30:36 +00:00
}
}
// HandleUserUpdatePassword godoc
// @Summary Update the current user's password // TODO:
// @Tags User
// @Produce json
// @Success 204
// @Router /v1/users/self/password [PUT]
// @Security Bearer
func (ctrl *V1Controller) HandleUserUpdatePassword() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
}
}
2022-09-04 02:42:03 +00:00
// HandleUserSelfDelete godoc
// @Summary Deletes the user account
// @Tags User
// @Produce json
// @Success 204
// @Router /v1/users/self [DELETE]
// @Security Bearer
func (ctrl *V1Controller) HandleUserSelfDelete() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
actor := services.UseUserCtx(r.Context())
if err := ctrl.svc.User.DeleteSelf(r.Context(), actor.ID); err != nil {
server.RespondError(w, http.StatusInternalServerError, err)
return
}
server.Respond(w, http.StatusNoContent, nil)
}
}