2022-09-01 23:11:14 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-09-02 01:52:40 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/google/uuid"
|
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"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/web/adapters"
|
|
|
|
"github.com/hay-kot/safeserve/errchain"
|
2022-09-01 23:11:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandleLabelsGetAll godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get All Labels
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
2023-03-21 05:48:22 +00:00
|
|
|
// @Success 200 {object} []repo.LabelOut
|
2023-03-07 06:18:58 +00:00
|
|
|
// @Router /v1/labels [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelsGetAll() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request) ([]repo.LabelSummary, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Labels.GetAll(auth, auth.GID)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLabelsCreate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Create Label
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.LabelCreate true "Label Data"
|
|
|
|
// @Success 200 {object} repo.LabelSummary
|
|
|
|
// @Router /v1/labels [POST]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelsCreate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, data repo.LabelCreate) (repo.LabelOut, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Labels.Create(auth, auth.GID, data)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Action(fn, http.StatusCreated)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLabelDelete godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Delete Label
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Label ID"
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/labels/{id} [DELETE]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelDelete() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID) (any, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
err := ctrl.repo.Labels.DeleteByGroup(auth, auth.GID, ID)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.CommandID("id", fn, http.StatusNoContent)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLabelGet godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get Label
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Label ID"
|
|
|
|
// @Success 200 {object} repo.LabelOut
|
|
|
|
// @Router /v1/labels/{id} [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelGet() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID) (repo.LabelOut, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Labels.GetOneByGroup(auth, auth.GID, ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.CommandID("id", fn, http.StatusOK)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLabelUpdate godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Update Label
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Label ID"
|
|
|
|
// @Success 200 {object} repo.LabelOut
|
|
|
|
// @Router /v1/labels/{id} [PUT]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelUpdate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID, data repo.LabelUpdate) (repo.LabelOut, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
data.ID = ID
|
|
|
|
return ctrl.repo.Labels.UpdateByGroup(auth, auth.GID, data)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.ActionID("id", fn, http.StatusOK)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|