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"
|
2024-01-18 19:45:42 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/web/adapters"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
2022-10-07 02:54:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
GroupInvitationCreate struct {
|
2024-01-04 17:55:26 +00:00
|
|
|
Uses int `json:"uses" validate:"required,min=1,max=100"`
|
2022-10-07 02:54:09 +00:00
|
|
|
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
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get Group
|
|
|
|
// @Tags Group
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} repo.Group
|
|
|
|
// @Router /v1/groups [Get]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleGroupGet() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request) (repo.Group, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Groups.GroupByID(auth, auth.GID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
2022-10-15 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleGroupUpdate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Update Group
|
|
|
|
// @Tags Group
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.GroupUpdate true "User Data"
|
|
|
|
// @Success 200 {object} repo.Group
|
|
|
|
// @Router /v1/groups [Put]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleGroupUpdate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, body repo.GroupUpdate) (repo.Group, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
2024-01-18 19:45:42 +00:00
|
|
|
|
|
|
|
ok := ctrl.svc.Currencies.IsSupported(body.Currency)
|
|
|
|
if !ok {
|
|
|
|
return repo.Group{}, validate.NewFieldErrors(
|
2024-01-28 03:29:10 +00:00
|
|
|
validate.NewFieldError("currency", "currency '"+body.Currency+"' is not supported"),
|
2024-01-18 19:45:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return ctrl.svc.Group.UpdateGroup(auth, body)
|
2022-10-15 20:15:55 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Action(fn, http.StatusOK)
|
2022-10-15 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleGroupInvitationsCreate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Create Group Invitation
|
|
|
|
// @Tags Group
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body GroupInvitationCreate true "User Data"
|
|
|
|
// @Success 200 {object} GroupInvitation
|
|
|
|
// @Router /v1/groups/invitations [Post]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleGroupInvitationsCreate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, body GroupInvitationCreate) (GroupInvitation, error) {
|
|
|
|
if body.ExpiresAt.IsZero() {
|
|
|
|
body.ExpiresAt = time.Now().Add(time.Hour * 24)
|
2022-10-07 02:54:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
auth := services.NewContext(r.Context())
|
2022-10-07 02:54:09 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
token, err := ctrl.svc.Group.NewInvitation(auth, body.Uses, body.ExpiresAt)
|
2022-10-07 02:54:09 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return GroupInvitation{
|
2022-10-07 02:54:09 +00:00
|
|
|
Token: token,
|
2023-03-21 04:32:10 +00:00
|
|
|
ExpiresAt: body.ExpiresAt,
|
|
|
|
Uses: body.Uses,
|
|
|
|
}, err
|
2022-10-07 02:54:09 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Action(fn, http.StatusCreated)
|
2022-10-07 02:54:09 +00:00
|
|
|
}
|