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

63 lines
1.5 KiB
Go
Raw Normal View History

2022-10-01 22:02:14 +00:00
package v1
import (
"net/http"
"time"
"github.com/hay-kot/homebox/backend/internal/services"
"github.com/hay-kot/homebox/backend/pkgs/server"
"github.com/rs/zerolog/log"
)
type (
2022-10-04 04:09:26 +00:00
GroupInvitationCreate struct {
2022-10-01 22:02:14 +00:00
Uses int `json:"uses"`
ExpiresAt time.Time `json:"expiresAt"`
}
2022-10-04 04:09:26 +00:00
GroupInvitation struct {
2022-10-01 22:02:14 +00:00
Token string `json:"token"`
ExpiresAt time.Time `json:"expiresAt"`
Uses int `json:"uses"`
}
)
// HandleUserSelf godoc
// @Summary Get the current user
// @Tags User
// @Produce json
2022-10-04 04:09:26 +00:00
// @Param payload body GroupInvitationCreate true "User Data"
// @Success 200 {object} GroupInvitation
2022-10-01 22:02:14 +00:00
// @Router /v1/groups/invitations [Post]
// @Security Bearer
func (ctrl *V1Controller) HandleGroupInvitationsCreate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2022-10-04 04:09:26 +00:00
data := GroupInvitationCreate{}
2022-10-01 22:02:14 +00:00
if err := server.Decode(r, &data); err != nil {
log.Err(err).Msg("failed to decode user registration data")
server.RespondError(w, http.StatusInternalServerError, err)
return
}
if data.ExpiresAt.IsZero() {
data.ExpiresAt = time.Now().Add(time.Hour * 24)
}
ctx := services.NewContext(r.Context())
token, err := ctrl.svc.User.NewInvitation(ctx, data.Uses, data.ExpiresAt)
if err != nil {
log.Err(err).Msg("failed to create new token")
server.RespondError(w, http.StatusInternalServerError, err)
return
}
2022-10-04 04:09:26 +00:00
server.Respond(w, http.StatusCreated, GroupInvitation{
2022-10-01 22:02:14 +00:00
Token: token,
ExpiresAt: data.ExpiresAt,
Uses: data.Uses,
2022-10-04 04:09:26 +00:00
})
2022-10-01 22:02:14 +00:00
}
}