package api import ( "errors" "net/http" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/jackc/pgx/v5" "git.zb-server.de/ZB-Server/uptimesebi/internal/authz" "git.zb-server.de/ZB-Server/uptimesebi/internal/store" ) type monitorOrderRequest struct { // MonitorIDs in the order wanted; usually the monitors of one group. MonitorIDs []uuid.UUID `json:"monitor_ids" binding:"required,min=1,max=1000"` } // orderMonitors stores the organization's own order of the monitors given, // which the monitor list follows when sorted by its own order. It needs // write access to every one of them. func (s *Server) orderMonitors(c *gin.Context) { var req monitorOrderRequest if !bind(c, &req) { return } p := current(c) ctx := c.Request.Context() t, err := s.loadGroupTree(ctx, p.scope()) if err != nil { internalError(c, err) return } errDenied := errors.New("denied") seen := map[uuid.UUID]bool{} err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error { for i, id := range req.MonitorIDs { if seen[id] { return pgx.ErrNoRows } seen[id] = true m, err := q.GetMonitor(ctx, id) if err != nil { return err } if !canInGroup(p, authz.MonitorsWrite, t, m.GroupID) { return errDenied } if _, err := q.SetMonitorPosition(ctx, store.SetMonitorPositionParams{ID: id, Position: new(int32(i))}); err != nil { return err } } return nil }) switch { case errors.Is(err, errDenied): fail(c, http.StatusForbidden, "forbidden") case errors.Is(err, pgx.ErrNoRows): fail(c, http.StatusUnprocessableEntity, "invalid_monitor") case err != nil: internalError(c, err) default: c.Status(http.StatusNoContent) } }