2022-09-01 23:11:14 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/google/uuid"
|
2022-10-30 02:15:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2022-09-01 23:11:14 +00:00
|
|
|
)
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
// routeID extracts the ID from the request URL. If the ID is not in a valid
|
|
|
|
// format, an error is returned. If a error is returned, it can be directly returned
|
|
|
|
// from the handler. the validate.ErrInvalidID error is known by the error middleware
|
|
|
|
// and will be handled accordingly.
|
|
|
|
//
|
|
|
|
// Example: /api/v1/ac614db5-d8b8-4659-9b14-6e913a6eb18a -> uuid.UUID{ac614db5-d8b8-4659-9b14-6e913a6eb18a}
|
|
|
|
func (ctrl *V1Controller) routeID(r *http.Request) (uuid.UUID, error) {
|
|
|
|
return ctrl.routeUUID(r, "id")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctrl *V1Controller) routeUUID(r *http.Request, key string) (uuid.UUID, error) {
|
|
|
|
ID, err := uuid.Parse(chi.URLParam(r, key))
|
2022-09-01 23:11:14 +00:00
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return uuid.Nil, validate.NewInvalidRouteKeyError(key)
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|
2022-10-17 02:50:44 +00:00
|
|
|
return ID, nil
|
2022-09-01 23:11:14 +00:00
|
|
|
}
|