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:
65
internal/adapters/postgres/alerts_repo.go
Normal file
65
internal/adapters/postgres/alerts_repo.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/weatherapi/internal/core/ports"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type AlertRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewAlertRepository(pool *pgxpool.Pool) *AlertRepository {
|
||||
return &AlertRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *AlertRepository) ListCurrentAlerts(ctx context.Context) ([]ports.AlertRecord, error) {
|
||||
rows, err := r.pool.Query(ctx, queryCurrentAlerts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query current alerts: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]ports.AlertRecord, 0)
|
||||
for rows.Next() {
|
||||
var effective sql.NullTime
|
||||
var expires sql.NullTime
|
||||
var severity sql.NullString
|
||||
var event sql.NullString
|
||||
var headline sql.NullString
|
||||
var instruction sql.NullString
|
||||
var description sql.NullString
|
||||
|
||||
if err := rows.Scan(
|
||||
&effective,
|
||||
&expires,
|
||||
&severity,
|
||||
&event,
|
||||
&headline,
|
||||
&instruction,
|
||||
&description,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan current alert row: %w", err)
|
||||
}
|
||||
|
||||
out = append(out, ports.AlertRecord{
|
||||
Effective: ptrTime(effective),
|
||||
Expires: ptrTime(expires),
|
||||
Severity: ptrString(severity),
|
||||
Event: ptrString(event),
|
||||
Headline: ptrString(headline),
|
||||
Instruction: ptrString(instruction),
|
||||
Description: ptrString(description),
|
||||
})
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate current alert rows: %w", err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user