2022-08-31 05:22:01 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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/ent"
|
|
|
|
"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-08-31 05:22:01 +00:00
|
|
|
)
|
|
|
|
|
2023-01-28 20:53:00 +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"
|
|
|
|
// @Success 200 {object} server.Results{items=[]repo.TreeItem}
|
|
|
|
// @Router /v1/locations/tree [GET]
|
|
|
|
// @Security Bearer
|
2023-01-28 20:53:00 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationTreeQuery() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
user := services.UseUserCtx(r.Context())
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
|
|
|
|
withItems := queryBool(q.Get("withItems"))
|
|
|
|
|
|
|
|
locTree, err := ctrl.repo.Locations.Tree(
|
|
|
|
r.Context(),
|
|
|
|
user.GroupID,
|
|
|
|
repo.TreeQuery{
|
|
|
|
WithItems: withItems,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to get locations tree")
|
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return server.Respond(w, http.StatusOK, server.Results{Items: locTree})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:32:03 +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"
|
|
|
|
// @Success 200 {object} server.Results{items=[]repo.LocationOutCount}
|
|
|
|
// @Router /v1/locations [GET]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationGetAll() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-08-31 05:22:01 +00:00
|
|
|
user := services.UseUserCtx(r.Context())
|
2022-11-02 19:54:43 +00:00
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
|
|
|
|
filter := repo.LocationQuery{
|
|
|
|
FilterChildren: queryBool(q.Get("filterChildren")),
|
|
|
|
}
|
|
|
|
|
|
|
|
locations, err := ctrl.repo.Locations.GetAll(r.Context(), user.GroupID, filter)
|
2022-08-31 05:22:01 +00:00
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to get locations")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusOK, server.Results{Items: locations})
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:32:03 +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
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationCreate() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-27 23:52:13 +00:00
|
|
|
createData := repo.LocationCreate{}
|
2022-08-31 05:22:01 +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 location create data")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user := services.UseUserCtx(r.Context())
|
2022-10-30 04:05:38 +00:00
|
|
|
location, err := ctrl.repo.Locations.Create(r.Context(), user.GroupID, createData)
|
2022-08-31 05:22:01 +00:00
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to create location")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusCreated, location)
|
2022-08-31 05:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 22:32:03 +00:00
|
|
|
|
|
|
|
// HandleLocationDelete godocs
|
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
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationDelete() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleLocationGeneral()
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLocationGet godocs
|
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
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationGet() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleLocationGeneral()
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLocationUpdate godocs
|
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
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleLocationUpdate() server.HandlerFunc {
|
2022-10-17 02:50:44 +00:00
|
|
|
return ctrl.handleLocationGeneral()
|
|
|
|
}
|
2022-09-01 22:32:03 +00:00
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) handleLocationGeneral() 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-01 22:32:03 +00:00
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return err
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 02:50:44 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2022-10-30 04:05:38 +00:00
|
|
|
location, err := ctrl.repo.Locations.GetOneByGroup(r.Context(), ctx.GID, ID)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
l := log.Err(err).
|
|
|
|
Str("ID", ID.String()).
|
|
|
|
Str("GID", ctx.GID.String())
|
|
|
|
|
|
|
|
if ent.IsNotFound(err) {
|
|
|
|
l.Msg("location not found")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusNotFound)
|
2022-10-17 02:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
l.Msg("failed to get location")
|
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, location)
|
2022-10-17 02:50:44 +00:00
|
|
|
case http.MethodPut:
|
|
|
|
body := repo.LocationUpdate{}
|
|
|
|
if err := server.Decode(r, &body); err != nil {
|
|
|
|
log.Err(err).Msg("failed to decode location update data")
|
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-09-01 22:32:03 +00:00
|
|
|
|
2022-10-30 04:05:38 +00:00
|
|
|
result, err := ctrl.repo.Locations.UpdateOneByGroup(r.Context(), ctx.GID, ID, body)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to update location")
|
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-10-17 02:50:44 +00:00
|
|
|
case http.MethodDelete:
|
2022-10-30 04:05:38 +00:00
|
|
|
err = ctrl.repo.Locations.DeleteByGroup(r.Context(), ctx.GID, ID)
|
2022-10-17 02:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to delete location")
|
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-09-01 22:32:03 +00:00
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
return nil
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
}
|