mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-03 08:10:28 +00:00
cleanup items endpoints
This commit is contained in:
parent
434f1fa411
commit
72e1ad3405
2 changed files with 95 additions and 86 deletions
|
@ -32,3 +32,14 @@ func (ctrl *V1Controller) partialParseIdAndUser(w http.ResponseWriter, r *http.R
|
||||||
user := services.UseUserCtx(r.Context())
|
user := services.UseUserCtx(r.Context())
|
||||||
return uid, user, nil
|
return uid, user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctrl *V1Controller) partialRouteID(w http.ResponseWriter, r *http.Request) (uuid.UUID, error) {
|
||||||
|
ID, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("failed to parse id")
|
||||||
|
server.RespondError(w, http.StatusBadRequest, err)
|
||||||
|
return uuid.Nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ID, nil
|
||||||
|
}
|
||||||
|
|
|
@ -13,41 +13,6 @@ import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func uuidList(params url.Values, key string) []uuid.UUID {
|
|
||||||
var ids []uuid.UUID
|
|
||||||
for _, id := range params[key] {
|
|
||||||
uid, err := uuid.Parse(id)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ids = append(ids, uid)
|
|
||||||
}
|
|
||||||
return ids
|
|
||||||
}
|
|
||||||
|
|
||||||
func intOrNegativeOne(s string) int {
|
|
||||||
i, err := strconv.Atoi(s)
|
|
||||||
if err != nil {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
|
|
||||||
func extractQuery(r *http.Request) repo.ItemQuery {
|
|
||||||
params := r.URL.Query()
|
|
||||||
|
|
||||||
page := intOrNegativeOne(params.Get("page"))
|
|
||||||
perPage := intOrNegativeOne(params.Get("perPage"))
|
|
||||||
|
|
||||||
return repo.ItemQuery{
|
|
||||||
Page: page,
|
|
||||||
PageSize: perPage,
|
|
||||||
Search: params.Get("q"),
|
|
||||||
LocationIDs: uuidList(params, "locations"),
|
|
||||||
LabelIDs: uuidList(params, "labels"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleItemsGetAll godoc
|
// HandleItemsGetAll godoc
|
||||||
// @Summary Get All Items
|
// @Summary Get All Items
|
||||||
// @Tags Items
|
// @Tags Items
|
||||||
|
@ -61,6 +26,38 @@ func extractQuery(r *http.Request) repo.ItemQuery {
|
||||||
// @Router /v1/items [GET]
|
// @Router /v1/items [GET]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleItemsGetAll() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleItemsGetAll() http.HandlerFunc {
|
||||||
|
uuidList := func(params url.Values, key string) []uuid.UUID {
|
||||||
|
var ids []uuid.UUID
|
||||||
|
for _, id := range params[key] {
|
||||||
|
uid, err := uuid.Parse(id)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ids = append(ids, uid)
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
|
intOrNegativeOne := func(s string) int {
|
||||||
|
i, err := strconv.Atoi(s)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
extractQuery := func(r *http.Request) repo.ItemQuery {
|
||||||
|
params := r.URL.Query()
|
||||||
|
|
||||||
|
return repo.ItemQuery{
|
||||||
|
Page: intOrNegativeOne(params.Get("page")),
|
||||||
|
PageSize: intOrNegativeOne(params.Get("perPage")),
|
||||||
|
Search: params.Get("q"),
|
||||||
|
LocationIDs: uuidList(params, "locations"),
|
||||||
|
LabelIDs: uuidList(params, "labels"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := services.NewContext(r.Context())
|
ctx := services.NewContext(r.Context())
|
||||||
items, err := ctrl.svc.Items.Query(ctx, extractQuery(r))
|
items, err := ctrl.svc.Items.Query(ctx, extractQuery(r))
|
||||||
|
@ -102,31 +99,6 @@ func (ctrl *V1Controller) HandleItemsCreate() http.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
func (ctrl *V1Controller) HandleItemDelete() http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
uid, user, err := ctrl.partialParseIdAndUser(w, r)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ctrl.svc.Items.Delete(r.Context(), user.GroupID, uid)
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg("failed to delete item")
|
|
||||||
server.RespondServerError(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
server.Respond(w, http.StatusNoContent, nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleItemGet godocs
|
// HandleItemGet godocs
|
||||||
// @Summary Gets a item and fields
|
// @Summary Gets a item and fields
|
||||||
// @Tags Items
|
// @Tags Items
|
||||||
|
@ -136,20 +108,19 @@ func (ctrl *V1Controller) HandleItemDelete() http.HandlerFunc {
|
||||||
// @Router /v1/items/{id} [GET]
|
// @Router /v1/items/{id} [GET]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleItemGet() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleItemGet() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return ctrl.handleItemsGeneral()
|
||||||
uid, user, err := ctrl.partialParseIdAndUser(w, r)
|
}
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
items, err := ctrl.svc.Items.GetOne(r.Context(), user.GroupID, uid)
|
// HandleItemDelete godocs
|
||||||
if err != nil {
|
// @Summary deletes a item
|
||||||
log.Err(err).Msg("failed to get item")
|
// @Tags Items
|
||||||
server.RespondServerError(w)
|
// @Produce json
|
||||||
return
|
// @Param id path string true "Item ID"
|
||||||
}
|
// @Success 204
|
||||||
server.Respond(w, http.StatusOK, items)
|
// @Router /v1/items/{id} [DELETE]
|
||||||
}
|
// @Security Bearer
|
||||||
|
func (ctrl *V1Controller) HandleItemDelete() http.HandlerFunc {
|
||||||
|
return ctrl.handleItemsGeneral()
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleItemUpdate godocs
|
// HandleItemUpdate godocs
|
||||||
|
@ -162,26 +133,53 @@ func (ctrl *V1Controller) HandleItemGet() http.HandlerFunc {
|
||||||
// @Router /v1/items/{id} [PUT]
|
// @Router /v1/items/{id} [PUT]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleItemUpdate() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleItemUpdate() http.HandlerFunc {
|
||||||
|
return ctrl.handleItemsGeneral()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctrl *V1Controller) handleItemsGeneral() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
body := repo.ItemUpdate{}
|
ctx := services.NewContext(r.Context())
|
||||||
if err := server.Decode(r, &body); err != nil {
|
ID, err := ctrl.partialRouteID(w, r)
|
||||||
log.Err(err).Msg("failed to decode request body")
|
|
||||||
server.RespondError(w, http.StatusInternalServerError, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
uid, user, err := ctrl.partialParseIdAndUser(w, r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
body.ID = uid
|
switch r.Method {
|
||||||
result, err := ctrl.svc.Items.Update(r.Context(), user.GroupID, body)
|
case http.MethodGet:
|
||||||
if err != nil {
|
items, err := ctrl.svc.Items.GetOne(r.Context(), ctx.GID, ID)
|
||||||
log.Err(err).Msg("failed to update item")
|
if err != nil {
|
||||||
server.RespondServerError(w)
|
log.Err(err).Msg("failed to get item")
|
||||||
|
server.RespondServerError(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server.Respond(w, http.StatusOK, items)
|
||||||
return
|
return
|
||||||
|
case http.MethodDelete:
|
||||||
|
err = ctrl.svc.Items.Delete(r.Context(), ctx.GID, ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("failed to delete item")
|
||||||
|
server.RespondServerError(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server.Respond(w, http.StatusNoContent, nil)
|
||||||
|
return
|
||||||
|
case http.MethodPut:
|
||||||
|
body := repo.ItemUpdate{}
|
||||||
|
if err := server.Decode(r, &body); err != nil {
|
||||||
|
log.Err(err).Msg("failed to decode request body")
|
||||||
|
server.RespondError(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
body.ID = ID
|
||||||
|
result, err := ctrl.svc.Items.Update(r.Context(), ctx.GID, body)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("failed to update item")
|
||||||
|
server.RespondServerError(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server.Respond(w, http.StatusOK, result)
|
||||||
}
|
}
|
||||||
server.Respond(w, http.StatusOK, result)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue