2022-09-01 23:11:14 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-09-02 01:52:40 +00:00
|
|
|
|
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/ent"
|
|
|
|
"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-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/pkgs/server"
|
2022-09-03 18:38:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-09-01 23:11:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandleLabelsGetAll godoc
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary Get All Labels
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} server.Results{items=[]repo.LabelOut}
|
|
|
|
// @Router /v1/labels [GET]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelsGetAll() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-02 01:52:40 +00:00
|
|
|
user := services.UseUserCtx(r.Context())
|
2022-10-30 04:05:38 +00:00
|
|
|
labels, err := ctrl.repo.Labels.GetAll(r.Context(), user.GroupID)
|
2022-09-02 01:52:40 +00:00
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("error getting labels")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusOK, server.Results{Items: labels})
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLabelsCreate godoc
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary Create a new label
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.LabelCreate true "Label Data"
|
|
|
|
// @Success 200 {object} repo.LabelSummary
|
|
|
|
// @Router /v1/labels [POST]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelsCreate() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-27 23:52:13 +00:00
|
|
|
createData := repo.LabelCreate{}
|
2022-09-02 01:52:40 +00:00
|
|
|
if err := server.Decode(r, &createData); err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("error decoding label create data")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user := services.UseUserCtx(r.Context())
|
2022-10-30 04:05:38 +00:00
|
|
|
label, err := ctrl.repo.Labels.Create(r.Context(), user.GroupID, createData)
|
2022-09-02 01:52:40 +00:00
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("error creating label")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusCreated, label)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLabelDelete godocs
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary deletes a label
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Label ID"
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/labels/{id} [DELETE]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelDelete() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleLabelsGeneral()
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLabelGet godocs
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary Gets a label and fields
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Label ID"
|
|
|
|
// @Success 200 {object} repo.LabelOut
|
|
|
|
// @Router /v1/labels/{id} [GET]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelGet() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleLabelsGeneral()
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLabelUpdate godocs
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary updates a label
|
|
|
|
// @Tags Labels
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Label ID"
|
|
|
|
// @Success 200 {object} repo.LabelOut
|
|
|
|
// @Router /v1/labels/{id} [PUT]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLabelUpdate() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleLabelsGeneral()
|
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) handleLabelsGeneral() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-17 02:50:44 +00:00
|
|
|
ctx := services.NewContext(r.Context())
|
2022-10-30 02:15:35 +00:00
|
|
|
ID, err := ctrl.routeID(r)
|
2022-09-02 01:52:40 +00:00
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return err
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 02:50:44 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2022-10-30 04:05:38 +00:00
|
|
|
labels, err := ctrl.repo.Labels.GetOneByGroup(r.Context(), ctx.GID, ID)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
if ent.IsNotFound(err) {
|
|
|
|
log.Err(err).
|
|
|
|
Str("id", ID.String()).
|
|
|
|
Msg("label not found")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusNotFound)
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
|
|
|
log.Err(err).Msg("error getting label")
|
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, labels)
|
2022-10-17 02:50:44 +00:00
|
|
|
|
|
|
|
case http.MethodDelete:
|
2022-10-30 04:05:38 +00:00
|
|
|
err = ctrl.repo.Labels.DeleteByGroup(ctx, ctx.GID, ID)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("error deleting label")
|
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.StatusNoContent, nil)
|
2022-10-17 02:50:44 +00:00
|
|
|
|
|
|
|
case http.MethodPut:
|
|
|
|
body := repo.LabelUpdate{}
|
|
|
|
if err := server.Decode(r, &body); err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body.ID = ID
|
2022-10-30 04:05:38 +00:00
|
|
|
result, err := ctrl.repo.Labels.UpdateByGroup(ctx, ctx.GID, body)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
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, result)
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
|
|
|
}
|