|
| 1 | +package incidents |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/labstack/echo/v4" |
| 7 | + "github.com/opsway-io/backend/internal/entities" |
| 8 | + hs "github.com/opsway-io/backend/internal/rest/handlers" |
| 9 | + "github.com/opsway-io/backend/internal/rest/helpers" |
| 10 | +) |
| 11 | + |
| 12 | +type GetIncidentsRequest struct { |
| 13 | + TeamID uint `param:"teamId" validate:"required,numeric,gte=0"` |
| 14 | + Offset *int `query:"offset" validate:"omitempty,numeric,gte=0"` |
| 15 | + Limit *int `query:"limit" validate:"omitempty,numeric,gte=0,max=255"` |
| 16 | +} |
| 17 | + |
| 18 | +type GetIncidentsResponse struct { |
| 19 | + Checks []GetIncidentsResponseIncident `json:"incidents"` |
| 20 | +} |
| 21 | + |
| 22 | +type GetIncidentsResponseIncident struct { |
| 23 | + ID uint `json:"id"` |
| 24 | + TeamID uint `json:"teamId"` |
| 25 | + MonitorID uint `json:"monitorId"` |
| 26 | + Title string `json:"title"` |
| 27 | + Description string `json:"description"` |
| 28 | + CreatedAt string `json:"createdAt"` |
| 29 | +} |
| 30 | + |
| 31 | +func (h *Handlers) GetIncidents(c hs.AuthenticatedContext) error { |
| 32 | + req, err := helpers.Bind[GetIncidentsRequest](c) |
| 33 | + if err != nil { |
| 34 | + c.Log.WithError(err).Debug("failed to bind GetIncidentsRequest") |
| 35 | + |
| 36 | + return echo.ErrBadRequest |
| 37 | + } |
| 38 | + |
| 39 | + ctx := c.Request().Context() |
| 40 | + |
| 41 | + incidents, err := h.IncidentService.GetByTeamIDPaginated( |
| 42 | + ctx, |
| 43 | + req.TeamID, |
| 44 | + req.Offset, |
| 45 | + req.Limit) |
| 46 | + if err != nil { |
| 47 | + c.Log.WithError(err).Error("failed to get incidents") |
| 48 | + |
| 49 | + return echo.ErrInternalServerError |
| 50 | + } |
| 51 | + |
| 52 | + resp := h.newGetIncidentResponse(incidents) |
| 53 | + |
| 54 | + return c.JSON(http.StatusOK, resp) |
| 55 | +} |
| 56 | + |
| 57 | +func (h *Handlers) newGetIncidentResponse(incidents *[]entities.Incident) *GetIncidentsResponse { |
| 58 | + resp := &GetIncidentsResponse{ |
| 59 | + Checks: make([]GetIncidentsResponseIncident, len(*incidents)), |
| 60 | + } |
| 61 | + |
| 62 | + for i, incident := range *incidents { |
| 63 | + resp.Checks[i] = GetIncidentsResponseIncident{ |
| 64 | + ID: incident.ID, |
| 65 | + TeamID: incident.TeamID, |
| 66 | + MonitorID: incident.MonitorID, |
| 67 | + Title: incident.Title, |
| 68 | + Description: *incident.Description, |
| 69 | + CreatedAt: incident.CreatedAt.Format("2006-01-02T15:04:05Z07:00"), |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return resp |
| 74 | +} |
| 75 | + |
| 76 | +type GetIncidentOverviewRequest struct { |
| 77 | + TeamID uint `param:"teamId" validate:"required,numeric,gte=0"` |
| 78 | + Offset *int `query:"offset" validate:"omitempty,numeric,gte=0"` |
| 79 | + Limit *int `query:"limit" validate:"omitempty,numeric,gte=0,max=255"` |
| 80 | +} |
| 81 | + |
| 82 | +type GetIncidentOverviewResponse struct { |
| 83 | + Checks []GetIncidentOverviewResponseIncident `json:"incidents"` |
| 84 | +} |
| 85 | + |
| 86 | +type GetIncidentOverviewResponseIncident struct { |
| 87 | + ID uint `json:"id"` |
| 88 | + TeamID uint `json:"teamId"` |
| 89 | + MonitorID uint `json:"monitorId"` |
| 90 | + CreatedAt string `json:"createdAt"` |
| 91 | + Count int `json:"count"` |
| 92 | +} |
| 93 | + |
| 94 | +func (h *Handlers) GetIncidentOverview(c hs.AuthenticatedContext) error { |
| 95 | + req, err := helpers.Bind[GetIncidentOverviewRequest](c) |
| 96 | + if err != nil { |
| 97 | + c.Log.WithError(err).Debug("failed to bind GetIncidentsRequest") |
| 98 | + |
| 99 | + return echo.ErrBadRequest |
| 100 | + } |
| 101 | + |
| 102 | + ctx := c.Request().Context() |
| 103 | + |
| 104 | + incidents, err := h.IncidentService.GetByTeamIDPaginated( |
| 105 | + ctx, |
| 106 | + req.TeamID, |
| 107 | + req.Offset, |
| 108 | + req.Limit) |
| 109 | + if err != nil { |
| 110 | + c.Log.WithError(err).Error("failed to get incidents") |
| 111 | + |
| 112 | + return echo.ErrInternalServerError |
| 113 | + } |
| 114 | + |
| 115 | + resp := h.newGetIncidentOverviewResponse(incidents) |
| 116 | + |
| 117 | + return c.JSON(http.StatusOK, resp) |
| 118 | +} |
| 119 | + |
| 120 | +func (h *Handlers) newGetIncidentOverviewResponse(incidents *[]entities.Incident) *GetIncidentOverviewResponse { |
| 121 | + |
| 122 | + resp := &GetIncidentOverviewResponse{ |
| 123 | + Checks: make([]GetIncidentOverviewResponseIncident, len(*incidents)), |
| 124 | + } |
| 125 | + |
| 126 | + for i, incident := range *incidents { |
| 127 | + resp.Checks[i] = GetIncidentOverviewResponseIncident{ |
| 128 | + ID: incident.ID, |
| 129 | + TeamID: incident.TeamID, |
| 130 | + MonitorID: incident.MonitorID, |
| 131 | + Count: 0, |
| 132 | + CreatedAt: incident.CreatedAt.Format("2006-01-02T15:04:05Z07:00"), |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return resp |
| 137 | +} |
0 commit comments