2022-10-07 02:54:09 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
2022-10-30 02:15:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2022-10-07 02:54:09 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/pkgs/server"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
GroupInvitationCreate struct {
|
|
|
|
Uses int `json:"uses"`
|
|
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupInvitation struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
|
|
Uses int `json:"uses"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-10-15 20:15:55 +00:00
|
|
|
// HandleGroupGet godoc
|
|
|
|
// @Summary Get the current user's group
|
|
|
|
// @Tags Group
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} repo.Group
|
|
|
|
// @Router /v1/groups [Get]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleGroupGet() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleGroupGeneral()
|
2022-10-15 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleGroupUpdate godoc
|
|
|
|
// @Summary Updates some fields of the current users group
|
|
|
|
// @Tags Group
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.GroupUpdate true "User Data"
|
|
|
|
// @Success 200 {object} repo.Group
|
|
|
|
// @Router /v1/groups [Put]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleGroupUpdate() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleGroupGeneral()
|
|
|
|
}
|
2022-10-15 20:15:55 +00:00
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) handleGroupGeneral() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-15 20:15:55 +00:00
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
|
2022-10-17 02:50:44 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2022-10-30 04:05:38 +00:00
|
|
|
group, err := ctrl.repo.Groups.GroupByID(ctx, ctx.GID)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to get group")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusOK, group)
|
2022-10-17 02:50:44 +00:00
|
|
|
|
|
|
|
case http.MethodPut:
|
|
|
|
data := repo.GroupUpdate{}
|
|
|
|
if err := server.Decode(r, &data); err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusBadRequest)
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
group, err := ctrl.svc.Group.UpdateGroup(ctx, data)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to update group")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
2022-10-30 04:05:38 +00:00
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusOK, group)
|
2022-10-15 20:15:55 +00:00
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
|
|
|
|
return nil
|
2022-10-15 20:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleGroupInvitationsCreate godoc
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary Get the current user
|
2022-10-15 20:15:55 +00:00
|
|
|
// @Tags Group
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Param payload body GroupInvitationCreate true "User Data"
|
|
|
|
// @Success 200 {object} GroupInvitation
|
|
|
|
// @Router /v1/groups/invitations [Post]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleGroupInvitationsCreate() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-07 02:54:09 +00:00
|
|
|
data := GroupInvitationCreate{}
|
|
|
|
if err := server.Decode(r, &data); err != nil {
|
|
|
|
log.Err(err).Msg("failed to decode user registration data")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusBadRequest)
|
2022-10-07 02:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.ExpiresAt.IsZero() {
|
|
|
|
data.ExpiresAt = time.Now().Add(time.Hour * 24)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
|
2022-10-15 20:15:55 +00:00
|
|
|
token, err := ctrl.svc.Group.NewInvitation(ctx, data.Uses, data.ExpiresAt)
|
2022-10-07 02:54:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to create new token")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-10-07 02:54:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusCreated, GroupInvitation{
|
2022-10-07 02:54:09 +00:00
|
|
|
Token: token,
|
|
|
|
ExpiresAt: data.ExpiresAt,
|
|
|
|
Uses: data.Uses,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|