2022-08-31 05:22:01 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
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"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/web/adapters"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
2022-08-31 05:22:01 +00:00
|
|
|
)
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
// HandleLocationTreeQuery godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get Locations Tree
|
|
|
|
// @Tags Locations
|
|
|
|
// @Produce json
|
|
|
|
// @Param withItems query bool false "include items in response tree"
|
2023-03-21 05:48:22 +00:00
|
|
|
// @Success 200 {object} []repo.TreeItem
|
2023-03-07 06:18:58 +00:00
|
|
|
// @Router /v1/locations/tree [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationTreeQuery() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, query repo.TreeQuery) ([]repo.TreeItem, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Locations.Tree(auth, auth.GID, query)
|
2023-01-28 20:53:00 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Query(fn, http.StatusOK)
|
2023-01-28 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
// HandleLocationGetAll godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get All Locations
|
|
|
|
// @Tags Locations
|
|
|
|
// @Produce json
|
|
|
|
// @Param filterChildren query bool false "Filter locations with parents"
|
2023-03-21 05:48:22 +00:00
|
|
|
// @Success 200 {object} []repo.LocationOutCount
|
2023-03-07 06:18:58 +00:00
|
|
|
// @Router /v1/locations [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationGetAll() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, q repo.LocationQuery) ([]repo.LocationOutCount, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Locations.GetAll(auth, auth.GID, q)
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Query(fn, http.StatusOK)
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
// HandleLocationCreate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Create Location
|
|
|
|
// @Tags Locations
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.LocationCreate true "Location Data"
|
|
|
|
// @Success 200 {object} repo.LocationSummary
|
|
|
|
// @Router /v1/locations [POST]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationCreate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, createData repo.LocationCreate) (repo.LocationOut, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Locations.Create(auth, auth.GID, createData)
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Action(fn, http.StatusCreated)
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
2022-09-01 22:32:03 +00:00
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
// HandleLocationDelete godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Delete Location
|
|
|
|
// @Tags Locations
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Location ID"
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/locations/{id} [DELETE]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationDelete() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID) (any, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
err := ctrl.repo.Locations.DeleteByGroup(auth, auth.GID, ID)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.CommandID("id", fn, http.StatusNoContent)
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
// HandleLocationGet godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get Location
|
|
|
|
// @Tags Locations
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Location ID"
|
|
|
|
// @Success 200 {object} repo.LocationOut
|
|
|
|
// @Router /v1/locations/{id} [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationGet() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID) (repo.LocationOut, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.Locations.GetOneByGroup(auth, auth.GID, ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.CommandID("id", fn, http.StatusOK)
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
// HandleLocationUpdate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Update Location
|
|
|
|
// @Tags Locations
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Location ID"
|
|
|
|
// @Param payload body repo.LocationUpdate true "Location Data"
|
|
|
|
// @Success 200 {object} repo.LocationOut
|
|
|
|
// @Router /v1/locations/{id} [PUT]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationUpdate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID, body repo.LocationUpdate) (repo.LocationOut, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
body.ID = ID
|
|
|
|
return ctrl.repo.Locations.UpdateByGroup(auth, auth.GID, ID, body)
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.ActionID("id", fn, http.StatusOK)
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|