2022-09-03 09:17:48 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-01-01 21:50:48 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2022-09-03 09:17:48 +00:00
|
|
|
"net/http"
|
|
|
|
|
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-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-03 09:17:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandleItemsGetAll godoc
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary Get All Items
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param q query string false "search string"
|
|
|
|
// @Param page query int false "page number"
|
|
|
|
// @Param pageSize query int false "items per page"
|
|
|
|
// @Param labels query []string false "label Ids" collectionFormat(multi)
|
|
|
|
// @Param locations query []string false "location Ids" collectionFormat(multi)
|
|
|
|
// @Success 200 {object} repo.PaginationResult[repo.ItemSummary]{}
|
|
|
|
// @Router /v1/items [GET]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemsGetAll() server.HandlerFunc {
|
2022-11-01 07:30:42 +00:00
|
|
|
|
2022-10-17 02:50:44 +00:00
|
|
|
extractQuery := func(r *http.Request) repo.ItemQuery {
|
|
|
|
params := r.URL.Query()
|
|
|
|
|
|
|
|
return repo.ItemQuery{
|
2022-11-02 19:54:43 +00:00
|
|
|
Page: queryIntOrNegativeOne(params.Get("page")),
|
2022-12-30 01:19:15 +00:00
|
|
|
PageSize: queryIntOrNegativeOne(params.Get("pageSize")),
|
2022-11-01 07:30:42 +00:00
|
|
|
Search: params.Get("q"),
|
2022-11-02 19:54:43 +00:00
|
|
|
LocationIDs: queryUUIDList(params, "locations"),
|
|
|
|
LabelIDs: queryUUIDList(params, "labels"),
|
|
|
|
IncludeArchived: queryBool(params.Get("includeArchived")),
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-13 05:13:07 +00:00
|
|
|
ctx := services.NewContext(r.Context())
|
2022-10-30 04:05:38 +00:00
|
|
|
items, err := ctrl.repo.Items.QueryByGroup(ctx, ctx.GID, extractQuery(r))
|
2022-09-03 09:17:48 +00:00
|
|
|
if err != nil {
|
2023-01-01 21:50:48 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return server.Respond(w, http.StatusOK, repo.PaginationResult[repo.ItemSummary]{
|
|
|
|
Items: []repo.ItemSummary{},
|
|
|
|
})
|
|
|
|
}
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to get items")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusOK, items)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemsCreate godoc
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary Create a new item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.ItemCreate true "Item Data"
|
|
|
|
// @Success 200 {object} repo.ItemSummary
|
|
|
|
// @Router /v1/items [POST]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemsCreate() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-27 23:52:13 +00:00
|
|
|
createData := repo.ItemCreate{}
|
2022-09-03 09:17:48 +00:00
|
|
|
if err := server.Decode(r, &createData); err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to decode request body")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:17:55 +00:00
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
item, err := ctrl.svc.Items.Create(ctx, createData)
|
2022-09-03 09:17:48 +00:00
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to create item")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusCreated, item)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemGet godocs
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary Gets a item and fields
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Success 200 {object} repo.ItemOut
|
|
|
|
// @Router /v1/items/{id} [GET]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemGet() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleItemsGeneral()
|
|
|
|
}
|
2022-09-03 09:17:48 +00:00
|
|
|
|
2022-10-17 02:50:44 +00:00
|
|
|
// HandleItemDelete godocs
|
|
|
|
// @Summary deletes a item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/items/{id} [DELETE]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemDelete() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleItemsGeneral()
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemUpdate godocs
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary updates a item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Param payload body repo.ItemUpdate true "Item Data"
|
|
|
|
// @Success 200 {object} repo.ItemOut
|
|
|
|
// @Router /v1/items/{id} [PUT]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemUpdate() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleItemsGeneral()
|
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) handleItemsGeneral() 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-03 09:17:48 +00:00
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return err
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 02:50:44 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2022-10-30 04:05:38 +00:00
|
|
|
items, err := ctrl.repo.Items.GetOneByGroup(r.Context(), ctx.GID, ID)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to get item")
|
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, items)
|
2022-10-17 02:50:44 +00:00
|
|
|
case http.MethodDelete:
|
2022-10-30 04:05:38 +00:00
|
|
|
err = ctrl.repo.Items.DeleteByGroup(r.Context(), ctx.GID, ID)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to delete item")
|
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.ItemUpdate{}
|
|
|
|
if err := server.Decode(r, &body); err != nil {
|
|
|
|
log.Err(err).Msg("failed to decode request body")
|
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.Items.UpdateByGroup(r.Context(), ctx.GID, body)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to update item")
|
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-03 09:17:48 +00:00
|
|
|
}
|
2022-10-17 02:50:44 +00:00
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return nil
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-06 19:15:07 +00:00
|
|
|
|
|
|
|
// HandleItemsImport godocs
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary imports items into the database
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Success 204
|
|
|
|
// @Param csv formData file true "Image to upload"
|
|
|
|
// @Router /v1/items/import [Post]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemsImport() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-06 19:15:07 +00:00
|
|
|
|
2022-10-07 02:54:09 +00:00
|
|
|
err := r.ParseMultipartForm(ctrl.maxUploadSize << 20)
|
2022-09-09 18:22:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to parse multipart form")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-09 18:22:51 +00:00
|
|
|
}
|
2022-09-06 19:15:07 +00:00
|
|
|
|
|
|
|
file, _, err := r.FormFile("csv")
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to get file from form")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 03:06:47 +00:00
|
|
|
data, err := services.ReadCsv(file)
|
2022-09-06 19:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to read csv")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user := services.UseUserCtx(r.Context())
|
|
|
|
|
2022-10-16 01:46:57 +00:00
|
|
|
_, err = ctrl.svc.Items.CsvImport(r.Context(), user.GroupID, data)
|
2022-09-06 19:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to import items")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusNoContent, nil)
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
}
|