Files
weatherapi/internal/application/alerts/service.go
Eric Rakestraw 27817f9e43
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
Initial MVP commit
2026-03-17 08:35:16 -05:00

58 lines
1.2 KiB
Go

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
}