2022-09-03 09:17:48 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-01-01 21:50:48 +00:00
|
|
|
"database/sql"
|
2023-02-26 02:54:40 +00:00
|
|
|
"encoding/csv"
|
2023-01-01 21:50:48 +00:00
|
|
|
"errors"
|
2022-09-03 09:17:48 +00:00
|
|
|
"net/http"
|
2023-02-05 21:12:54 +00:00
|
|
|
"strings"
|
2022-09-03 09:17:48 +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"
|
2022-10-30 02:15:35 +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"
|
|
|
|
"github.com/hay-kot/safeserve/errchain"
|
|
|
|
"github.com/hay-kot/safeserve/server"
|
2022-09-03 18:38:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-09-03 09:17:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandleItemsGetAll godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Query 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
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemsGetAll() errchain.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
extractQuery := func(r *http.Request) repo.ItemQuery {
|
|
|
|
params := r.URL.Query()
|
|
|
|
|
2023-02-05 21:12:54 +00:00
|
|
|
filterFieldItems := func(raw []string) []repo.FieldQuery {
|
|
|
|
var items []repo.FieldQuery
|
|
|
|
|
|
|
|
for _, v := range raw {
|
|
|
|
parts := strings.SplitN(v, "=", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
items = append(items, repo.FieldQuery{
|
|
|
|
Name: parts[0],
|
|
|
|
Value: parts[1],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
v := 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")),
|
2023-02-05 21:12:54 +00:00
|
|
|
Fields: filterFieldItems(params["fields"]),
|
2023-04-02 06:01:21 +00:00
|
|
|
OrderBy: params.Get("orderBy"),
|
2023-02-05 21:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(v.Search, "#") {
|
|
|
|
aidStr := strings.TrimPrefix(v.Search, "#")
|
|
|
|
|
|
|
|
aid, ok := repo.ParseAssetID(aidStr)
|
|
|
|
if ok {
|
|
|
|
v.Search = ""
|
|
|
|
v.AssetID = aid
|
|
|
|
}
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
2023-02-05 21:12:54 +00:00
|
|
|
|
|
|
|
return v
|
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())
|
2023-02-05 21:12:54 +00:00
|
|
|
|
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) {
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusOK, repo.PaginationResult[repo.ItemSummary]{
|
2023-01-01 21:50:48 +00:00
|
|
|
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
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusOK, items)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemsCreate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Create Item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.ItemCreate true "Item Data"
|
2023-03-21 04:32:10 +00:00
|
|
|
// @Success 201 {object} repo.ItemSummary
|
2023-03-07 06:18:58 +00:00
|
|
|
// @Router /v1/items [POST]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemsCreate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, body repo.ItemCreate) (repo.ItemOut, error) {
|
|
|
|
return ctrl.svc.Items.Create(services.NewContext(r.Context()), body)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Action(fn, http.StatusCreated)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemGet godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get Item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Success 200 {object} repo.ItemOut
|
|
|
|
// @Router /v1/items/{id} [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemGet() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID) (repo.ItemOut, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
|
|
|
|
return ctrl.repo.Items.GetOneByGroup(auth, auth.GID, ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.CommandID("id", fn, http.StatusOK)
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
2022-09-03 09:17:48 +00:00
|
|
|
|
2022-10-17 02:50:44 +00:00
|
|
|
// HandleItemDelete godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Delete Item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/items/{id} [DELETE]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemDelete() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID) (any, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
err := ctrl.repo.Items.DeleteByGroup(auth, auth.GID, ID)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.CommandID("id", fn, http.StatusNoContent)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemUpdate godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Update 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
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemUpdate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID, body repo.ItemUpdate) (repo.ItemOut, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
2022-10-17 02:50:44 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
body.ID = ID
|
|
|
|
return ctrl.repo.Items.UpdateByGroup(auth, auth.GID, body)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.ActionID("id", fn, http.StatusOK)
|
2022-09-03 09:17:48 +00:00
|
|
|
}
|
2022-09-06 19:15:07 +00:00
|
|
|
|
2023-02-05 21:12:54 +00:00
|
|
|
// HandleGetAllCustomFieldNames godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get All Custom Field Names
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200
|
|
|
|
// @Router /v1/items/fields [GET]
|
|
|
|
// @Success 200 {object} []string
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleGetAllCustomFieldNames() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request) ([]string, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Items.GetAllCustomFieldNames(auth, auth.GID)
|
2023-02-05 21:12:54 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
2023-02-05 21:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleGetAllCustomFieldValues godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get All Custom Field Values
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200
|
|
|
|
// @Router /v1/items/fields/values [GET]
|
|
|
|
// @Success 200 {object} []string
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleGetAllCustomFieldValues() errchain.HandlerFunc {
|
|
|
|
type query struct {
|
|
|
|
Field string `schema:"field" validate:"required"`
|
|
|
|
}
|
2023-02-05 21:12:54 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
fn := func(r *http.Request, q query) ([]string, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Items.GetAllCustomFieldValues(auth, auth.GID, q.Field)
|
2023-02-05 21:12:54 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Action(fn, http.StatusOK)
|
|
|
|
|
2023-02-05 21:12:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 19:15:07 +00:00
|
|
|
// HandleItemsImport godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Import Items
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Success 204
|
|
|
|
// @Param csv formData file true "Image to upload"
|
|
|
|
// @Router /v1/items/import [Post]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemsImport() errchain.HandlerFunc {
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
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
|
|
|
}
|
|
|
|
|
2023-02-26 02:54:40 +00:00
|
|
|
user := services.UseUserCtx(r.Context())
|
|
|
|
|
|
|
|
_, err = ctrl.svc.Items.CsvImport(r.Context(), user.GroupID, file)
|
2022-09-06 19:15:07 +00:00
|
|
|
if err != nil {
|
2023-02-26 02:54:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusNoContent, nil)
|
2023-02-26 02:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-06 19:15:07 +00:00
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// HandleItemsExport godocs
|
|
|
|
//
|
|
|
|
// @Summary Export Items
|
|
|
|
// @Tags Items
|
|
|
|
// @Success 200 {string} string "text/csv"
|
|
|
|
// @Router /v1/items/export [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemsExport() errchain.HandlerFunc {
|
2023-02-26 02:54:40 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
|
|
|
|
csvData, err := ctrl.svc.Items.ExportTSV(r.Context(), ctx.GID)
|
2022-09-06 19:15:07 +00:00
|
|
|
if err != nil {
|
2023-02-26 02:54:40 +00:00
|
|
|
log.Err(err).Msg("failed to export items")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 02:54:40 +00:00
|
|
|
w.Header().Set("Content-Type", "text/tsv")
|
|
|
|
w.Header().Set("Content-Disposition", "attachment;filename=homebox-items.tsv")
|
2023-04-01 22:07:44 +00:00
|
|
|
|
2023-02-26 02:54:40 +00:00
|
|
|
writer := csv.NewWriter(w)
|
2023-04-01 22:07:44 +00:00
|
|
|
writer.Comma = '\t'
|
2023-02-26 02:54:40 +00:00
|
|
|
return writer.WriteAll(csvData)
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
}
|