Files
weatherapi/internal/application/alerts/service.go
Eric Rakestraw 0d82e5d60e
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
Add omitempty to JSON fields in alert, forecast, and observation services
2026-03-17 09:03:23 -05:00

58 lines
1.3 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,omitempty"`
Expires *time.Time `json:"expires,omitempty"`
Severity *string `json:"severity,omitempty"`
Event *string `json:"event,omitempty"`
Headline *string `json:"headline,omitempty"`
Instruction *string `json:"instruction,omitempty"`
Description *string `json:"description,omitempty"`
}
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
}