Initial MVP commit
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
57
internal/application/alerts/service.go
Normal file
57
internal/application/alerts/service.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package alerts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/weatherapi/internal/core/ports"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo ports.AlertRepository
|
||||
}
|
||||
|
||||
func NewService(repo ports.AlertRepository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Alerts []AlertResponse `json:"alerts"`
|
||||
}
|
||||
|
||||
type AlertResponse struct {
|
||||
Effective *time.Time `json:"effective"`
|
||||
Expires *time.Time `json:"expires"`
|
||||
Severity *string `json:"severity"`
|
||||
Event *string `json:"event"`
|
||||
Headline *string `json:"headline"`
|
||||
Instruction *string `json:"instruction"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
|
||||
func (s *Service) GetCurrent(ctx context.Context) (Response, error) {
|
||||
if s == nil {
|
||||
return Response{}, fmt.Errorf("alerts service is nil")
|
||||
}
|
||||
|
||||
rows, err := s.repo.ListCurrentAlerts(ctx)
|
||||
if err != nil {
|
||||
return Response{}, err
|
||||
}
|
||||
|
||||
alerts := make([]AlertResponse, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
alerts = append(alerts, AlertResponse{
|
||||
Effective: row.Effective,
|
||||
Expires: row.Expires,
|
||||
Severity: row.Severity,
|
||||
Event: row.Event,
|
||||
Headline: row.Headline,
|
||||
Instruction: row.Instruction,
|
||||
Description: row.Description,
|
||||
})
|
||||
}
|
||||
|
||||
return Response{Alerts: alerts}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user