Add convective outlook Postgres read adapter
This commit is contained in:
92
internal/adapters/outbound/postgres/outlooks_read.go
Normal file
92
internal/adapters/outbound/postgres/outlooks_read.go
Normal file
@@ -0,0 +1,92 @@
|
||||
// outlooks_read.go executes outlook-run and outlook queries.
|
||||
// Layer: adapters/outbound/postgres outlook feature.
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
|
||||
)
|
||||
|
||||
func (r *Repository) LatestConvectiveOutlookRun(ctx context.Context) (*model.WeatherOutlookRun, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return nil, fmt.Errorf("postgres repository is not configured")
|
||||
}
|
||||
|
||||
var row outlookRunParentRow
|
||||
err := r.db.QueryRowContext(ctx, queryLatestConvectiveOutlookRun).Scan(
|
||||
&row.EventID,
|
||||
&row.LocationID,
|
||||
&row.LocationName,
|
||||
&row.Latitude,
|
||||
&row.Longitude,
|
||||
&row.AsOf,
|
||||
&row.IssuedAt,
|
||||
)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query latest convective outlook run: %w", err)
|
||||
}
|
||||
|
||||
run := mapOutlookRunParentRow(row)
|
||||
outlooks, err := r.loadOutlooks(ctx, row.EventID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
run.Outlooks = outlooks
|
||||
|
||||
return &run, nil
|
||||
}
|
||||
|
||||
func (r *Repository) loadOutlooks(ctx context.Context, eventID string) ([]model.WeatherOutlook, error) {
|
||||
rows, err := r.db.QueryContext(ctx, queryOutlooksForRun, eventID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query outlooks: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]model.WeatherOutlook, 0)
|
||||
for rows.Next() {
|
||||
var row outlookRow
|
||||
if err := rows.Scan(
|
||||
&row.OutlookIndex,
|
||||
&row.OutlookID,
|
||||
&row.Provider,
|
||||
&row.Product,
|
||||
&row.Day,
|
||||
&row.OutlookType,
|
||||
&row.Label,
|
||||
&row.LabelText,
|
||||
&row.SeverityRank,
|
||||
&row.ValidFrom,
|
||||
&row.ValidTo,
|
||||
&row.IssuedAt,
|
||||
&row.ExpiresAt,
|
||||
&row.Forecaster,
|
||||
&row.Headline,
|
||||
&row.Summary,
|
||||
&row.Discussion,
|
||||
&row.SourceURL,
|
||||
&row.ImageURL,
|
||||
&row.ContainsLocation,
|
||||
&row.GeometryJSON,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan outlook row: %w", err)
|
||||
}
|
||||
|
||||
outlook, err := mapOutlookRow(row)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("map outlook row: %w", err)
|
||||
}
|
||||
out = append(out, outlook)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate outlook rows: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user