mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-05 09:10:26 +00:00
refactor location routes
This commit is contained in:
parent
e78bbd8027
commit
0abe5fdc71
1 changed files with 49 additions and 55 deletions
|
@ -69,21 +69,8 @@ func (ctrl *V1Controller) HandleLocationCreate() http.HandlerFunc {
|
||||||
// @Router /v1/locations/{id} [DELETE]
|
// @Router /v1/locations/{id} [DELETE]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleLocationDelete() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleLocationDelete() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return ctrl.handleLocationGeneral()
|
||||||
ctx := services.NewContext(r.Context())
|
|
||||||
ID, err := ctrl.partialRouteID(w, r)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ctrl.svc.Location.Delete(r.Context(), ctx.GID, ID)
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg("failed to delete location")
|
|
||||||
server.RespondServerError(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
server.Respond(w, http.StatusNoContent, nil)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleLocationGet godocs
|
// HandleLocationGet godocs
|
||||||
|
@ -95,33 +82,7 @@ func (ctrl *V1Controller) HandleLocationDelete() http.HandlerFunc {
|
||||||
// @Router /v1/locations/{id} [GET]
|
// @Router /v1/locations/{id} [GET]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleLocationGet() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleLocationGet() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return ctrl.handleLocationGeneral()
|
||||||
ctx := services.NewContext(r.Context())
|
|
||||||
ID, err := ctrl.partialRouteID(w, r)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
location, err := ctrl.svc.Location.GetOne(r.Context(), ctx.GID, ID)
|
|
||||||
if err != nil {
|
|
||||||
if ent.IsNotFound(err) {
|
|
||||||
log.Err(err).
|
|
||||||
Str("id", ID.String()).
|
|
||||||
Str("gid", ctx.GID.String()).
|
|
||||||
Msg("location not found")
|
|
||||||
server.RespondError(w, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Err(err).
|
|
||||||
Str("id", ID.String()).
|
|
||||||
Str("gid", ctx.GID.String()).
|
|
||||||
Msg("failed to get location")
|
|
||||||
server.RespondServerError(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
server.Respond(w, http.StatusOK, location)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleLocationUpdate godocs
|
// HandleLocationUpdate godocs
|
||||||
|
@ -133,28 +94,61 @@ func (ctrl *V1Controller) HandleLocationGet() http.HandlerFunc {
|
||||||
// @Router /v1/locations/{id} [PUT]
|
// @Router /v1/locations/{id} [PUT]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleLocationUpdate() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleLocationUpdate() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return ctrl.handleLocationGeneral()
|
||||||
body := repo.LocationUpdate{}
|
}
|
||||||
if err := server.Decode(r, &body); err != nil {
|
|
||||||
log.Err(err).Msg("failed to decode location update data")
|
|
||||||
server.RespondError(w, http.StatusInternalServerError, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func (ctrl *V1Controller) handleLocationGeneral() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := services.NewContext(r.Context())
|
ctx := services.NewContext(r.Context())
|
||||||
ID, err := ctrl.partialRouteID(w, r)
|
ID, err := ctrl.partialRouteID(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
body.ID = ID
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
location, err := ctrl.svc.Location.GetOne(r.Context(), ctx.GID, ID)
|
||||||
|
if err != nil {
|
||||||
|
l := log.Err(err).
|
||||||
|
Str("ID", ID.String()).
|
||||||
|
Str("GID", ctx.GID.String())
|
||||||
|
|
||||||
result, err := ctrl.svc.Location.Update(r.Context(), ctx.GID, body)
|
if ent.IsNotFound(err) {
|
||||||
if err != nil {
|
l.Msg("location not found")
|
||||||
log.Err(err).Msg("failed to update location")
|
server.RespondError(w, http.StatusNotFound, err)
|
||||||
server.RespondServerError(w)
|
return
|
||||||
return
|
}
|
||||||
|
|
||||||
|
l.Msg("failed to get location")
|
||||||
|
server.RespondServerError(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server.Respond(w, http.StatusOK, location)
|
||||||
|
case http.MethodPut:
|
||||||
|
body := repo.LocationUpdate{}
|
||||||
|
if err := server.Decode(r, &body); err != nil {
|
||||||
|
log.Err(err).Msg("failed to decode location update data")
|
||||||
|
server.RespondError(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body.ID = ID
|
||||||
|
|
||||||
|
result, err := ctrl.svc.Location.Update(r.Context(), ctx.GID, body)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("failed to update location")
|
||||||
|
server.RespondServerError(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server.Respond(w, http.StatusOK, result)
|
||||||
|
case http.MethodDelete:
|
||||||
|
err = ctrl.svc.Location.Delete(r.Context(), ctx.GID, ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("failed to delete location")
|
||||||
|
server.RespondServerError(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server.Respond(w, http.StatusNoContent, nil)
|
||||||
}
|
}
|
||||||
server.Respond(w, http.StatusOK, result)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue