mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-05 09:10:26 +00:00
refactor labels routes
This commit is contained in:
parent
512ec6cb9e
commit
bd06d8fb96
1 changed files with 50 additions and 50 deletions
|
@ -56,7 +56,6 @@ func (ctrl *V1Controller) HandleLabelsCreate() http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
server.Respond(w, http.StatusCreated, label)
|
server.Respond(w, http.StatusCreated, label)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,20 +68,7 @@ func (ctrl *V1Controller) HandleLabelsCreate() http.HandlerFunc {
|
||||||
// @Router /v1/labels/{id} [DELETE]
|
// @Router /v1/labels/{id} [DELETE]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleLabelDelete() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleLabelDelete() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return ctrl.handleLabelsGeneral()
|
||||||
uid, user, err := ctrl.partialParseIdAndUser(w, r)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ctrl.svc.Labels.Delete(r.Context(), user.GroupID, uid)
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg("error deleting label")
|
|
||||||
server.RespondServerError(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
server.Respond(w, http.StatusNoContent, nil)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleLabelGet godocs
|
// HandleLabelGet godocs
|
||||||
|
@ -94,27 +80,7 @@ func (ctrl *V1Controller) HandleLabelDelete() http.HandlerFunc {
|
||||||
// @Router /v1/labels/{id} [GET]
|
// @Router /v1/labels/{id} [GET]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleLabelGet() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleLabelGet() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return ctrl.handleLabelsGeneral()
|
||||||
uid, user, err := ctrl.partialParseIdAndUser(w, r)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
labels, err := ctrl.svc.Labels.Get(r.Context(), user.GroupID, uid)
|
|
||||||
if err != nil {
|
|
||||||
if ent.IsNotFound(err) {
|
|
||||||
log.Err(err).
|
|
||||||
Str("id", uid.String()).
|
|
||||||
Msg("label not found")
|
|
||||||
server.RespondError(w, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Err(err).Msg("error getting label")
|
|
||||||
server.RespondServerError(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
server.Respond(w, http.StatusOK, labels)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleLabelUpdate godocs
|
// HandleLabelUpdate godocs
|
||||||
|
@ -126,25 +92,59 @@ func (ctrl *V1Controller) HandleLabelGet() http.HandlerFunc {
|
||||||
// @Router /v1/labels/{id} [PUT]
|
// @Router /v1/labels/{id} [PUT]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleLabelUpdate() http.HandlerFunc {
|
func (ctrl *V1Controller) HandleLabelUpdate() http.HandlerFunc {
|
||||||
|
return ctrl.handleLabelsGeneral()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctrl *V1Controller) handleLabelsGeneral() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
body := repo.LabelUpdate{}
|
ctx := services.NewContext(r.Context())
|
||||||
if err := server.Decode(r, &body); err != nil {
|
ID, err := ctrl.partialRouteID(w, r)
|
||||||
log.Err(err).Msg("error decoding label update data")
|
|
||||||
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.Labels.Update(r.Context(), user.GroupID, body)
|
case http.MethodGet:
|
||||||
if err != nil {
|
labels, err := ctrl.svc.Labels.Get(r.Context(), ctx.GID, ID)
|
||||||
log.Err(err).Msg("error updating label")
|
if err != nil {
|
||||||
server.RespondServerError(w)
|
if ent.IsNotFound(err) {
|
||||||
return
|
log.Err(err).
|
||||||
|
Str("id", ID.String()).
|
||||||
|
Msg("label not found")
|
||||||
|
server.RespondError(w, http.StatusNotFound, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Err(err).Msg("error getting label")
|
||||||
|
server.RespondServerError(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server.Respond(w, http.StatusOK, labels)
|
||||||
|
|
||||||
|
case http.MethodDelete:
|
||||||
|
err = ctrl.svc.Labels.Delete(r.Context(), ctx.GID, ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("error deleting label")
|
||||||
|
server.RespondServerError(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server.Respond(w, http.StatusNoContent, nil)
|
||||||
|
|
||||||
|
case http.MethodPut:
|
||||||
|
body := repo.LabelUpdate{}
|
||||||
|
if err := server.Decode(r, &body); err != nil {
|
||||||
|
log.Err(err).Msg("error decoding label update data")
|
||||||
|
server.RespondError(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body.ID = ID
|
||||||
|
result, err := ctrl.svc.Labels.Update(r.Context(), ctx.GID, body)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("error updating label")
|
||||||
|
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