diff options
Diffstat (limited to 'backend/internal/api')
| -rw-r--r-- | backend/internal/api/handler.go | 91 | ||||
| -rw-r--r-- | backend/internal/api/router.go | 5 |
2 files changed, 94 insertions, 2 deletions
diff --git a/backend/internal/api/handler.go b/backend/internal/api/handler.go index f304351..5f934da 100644 --- a/backend/internal/api/handler.go +++ b/backend/internal/api/handler.go @@ -142,12 +142,40 @@ func (h *Handler) GetHistory(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusBadRequest, map[string]string{"error": "id invalido"}) return } - history, err := h.store.GetHistory(id, 50) + + page, _ := strconv.Atoi(r.URL.Query().Get("page")) + if page < 1 { + page = 1 + } + perPage, _ := strconv.Atoi(r.URL.Query().Get("per_page")) + if perPage < 1 || perPage > 100 { + perPage = 50 + } + + offset := (page - 1) * perPage + history, err := h.store.GetHistory(id, perPage, offset) if err != nil { writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) return } - writeJSON(w, http.StatusOK, history) + + total, err := h.store.CountHeartbeats(id) + if err != nil { + total = len(history) + } + + totalPages := (total + perPage - 1) / perPage + if totalPages < 1 { + totalPages = 1 + } + + writeJSON(w, http.StatusOK, map[string]any{ + "data": history, + "page": page, + "per_page": perPage, + "total": total, + "total_pages": totalPages, + }) } func (h *Handler) GetActiveMaintenance(w http.ResponseWriter, r *http.Request) { @@ -163,6 +191,65 @@ func (h *Handler) GetActiveMaintenance(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, map[string]any{"maintenance": m}) } +func (h *Handler) ListMaintenances(w http.ResponseWriter, r *http.Request) { + maintenances, err := h.store.ListMaintenances() + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + writeJSON(w, http.StatusOK, maintenances) +} + +func (h *Handler) CreateMaintenance(w http.ResponseWriter, r *http.Request) { + var m model.Maintenance + if err := json.NewDecoder(r.Body).Decode(&m); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "json invalido: " + err.Error()}) + return + } + if m.Title == "" { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "titulo obrigatorio"}) + return + } + created, err := h.store.AddMaintenance(m) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + writeJSON(w, http.StatusCreated, created) +} + +func (h *Handler) UpdateMaintenance(w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(r.PathValue("id")) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "id invalido"}) + return + } + var m model.Maintenance + if err := json.NewDecoder(r.Body).Decode(&m); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "json invalido: " + err.Error()}) + return + } + updated, err := h.store.UpdateMaintenance(id, m) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + writeJSON(w, http.StatusOK, updated) +} + +func (h *Handler) DeleteMaintenance(w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(r.PathValue("id")) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "id invalido"}) + return + } + if err := h.store.DeleteMaintenance(id); err != nil { + writeJSON(w, http.StatusNotFound, map[string]string{"error": err.Error()}) + return + } + w.WriteHeader(http.StatusNoContent) +} + func (h *Handler) BadgeSVG(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.PathValue("id")) if err != nil { diff --git a/backend/internal/api/router.go b/backend/internal/api/router.go index 9a18cc5..8188b25 100644 --- a/backend/internal/api/router.go +++ b/backend/internal/api/router.go @@ -23,6 +23,11 @@ func NewRouter(h *Handler, stream *StreamHandler, auth *AuthHandler) http.Handle mux.HandleFunc("GET /api/active-maintenance", h.GetActiveMaintenance) + mux.HandleFunc("GET /api/maintenances", h.ListMaintenances) + mux.HandleFunc("POST /api/maintenances", h.CreateMaintenance) + mux.HandleFunc("PUT /api/maintenances/{id}", h.UpdateMaintenance) + mux.HandleFunc("DELETE /api/maintenances/{id}", h.DeleteMaintenance) + mux.Handle("GET /api/stream", stream) return corsMiddleware(auth.Middleware(mux)) |