2023-01-14 18:24:11 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
|
|
|
"github.com/hay-kot/httpkit/server"
|
2023-01-14 18:24:11 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// HandleAssetGet godocs
|
|
|
|
//
|
|
|
|
// @Summary Get Item by Asset ID
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Asset ID"
|
|
|
|
// @Success 200 {object} repo.PaginationResult[repo.ItemSummary]{}
|
|
|
|
// @Router /v1/assets/{id} [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleAssetGet() errchain.HandlerFunc {
|
2023-01-14 18:24:11 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
assetIdParam := chi.URLParam(r, "id")
|
|
|
|
assetIdParam = strings.ReplaceAll(assetIdParam, "-", "") // Remove dashes
|
|
|
|
// Convert the asset ID to an int64
|
|
|
|
assetId, err := strconv.ParseInt(assetIdParam, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pageParam := r.URL.Query().Get("page")
|
|
|
|
var page int64 = -1
|
|
|
|
if pageParam != "" {
|
|
|
|
page, err = strconv.ParseInt(pageParam, 10, 64)
|
|
|
|
if err != nil {
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusBadRequest, "Invalid page number")
|
2023-01-14 18:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pageSizeParam := r.URL.Query().Get("pageSize")
|
|
|
|
var pageSize int64 = -1
|
|
|
|
if pageSizeParam != "" {
|
|
|
|
pageSize, err = strconv.ParseInt(pageSizeParam, 10, 64)
|
|
|
|
if err != nil {
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusBadRequest, "Invalid page size")
|
2023-01-14 18:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := ctrl.repo.Items.QueryByAssetID(r.Context(), ctx.GID, repo.AssetID(assetId), int(page), int(pageSize))
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to get item")
|
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusOK, items)
|
2023-01-14 18:24:11 +00:00
|
|
|
}
|
|
|
|
}
|