add get raw meteo data query

This commit is contained in:
Pedro Pérez 2025-10-28 22:37:31 +01:00
parent b3ab98f9a2
commit e0024cc257
2 changed files with 60 additions and 15 deletions

View File

@ -33,6 +33,7 @@ type GetMeteoData struct {
To string
Page int
Limit int
Offset int
}
func (mt *GetMeteoData) Validate() error {
@ -56,6 +57,8 @@ func (mt *GetMeteoData) Validate() error {
return ErrMissingOrInvalidToDate
}
mt.Offset = (mt.Page - 1) * mt.Limit
return nil
}

View File

@ -8,6 +8,22 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
)
type Repository interface {
InsertMeteoDataTX(ctx context.Context, params InsertMeteoDataParams) (*InsertMeteoDataResult, error)
UpdateBatchElapsedTime(ctx context.Context, batchID int, elapsedMS int) error
GetMeteoData(ctx context.Context, params GetMeteoData) ([]MeteoData, error)
}
type pgxRepo struct {
*pgxpool.Pool
}
func NewPGXRepo(pool *pgxpool.Pool) Repository {
return &pgxRepo{
pool,
}
}
type InsertMeteoDataParams struct {
FileChecksum string
Accepted []MeteoData
@ -20,21 +36,6 @@ type InsertMeteoDataResult struct {
RejectedCount int
}
type Repository interface {
InsertMeteoDataTX(ctx context.Context, params InsertMeteoDataParams) (*InsertMeteoDataResult, error)
UpdateBatchElapsedTime(ctx context.Context, batchID int, elapsedMS int) error
}
type pgxRepo struct {
*pgxpool.Pool
}
func NewPGXRepo(pool *pgxpool.Pool) Repository {
return &pgxRepo{
pool,
}
}
func (pgx *pgxRepo) InsertMeteoDataTX(ctx context.Context, params InsertMeteoDataParams) (*InsertMeteoDataResult, error) {
tx, err := pgx.Begin(ctx)
if err != nil {
@ -148,3 +149,44 @@ func (pgx *pgxRepo) UpdateBatchElapsedTime(ctx context.Context, batchID int, ela
}
return nil
}
const getMeteoDataQuery = `
select location_name, date_of_register, max_temp, min_temp, rainfall, cloudiness
from public.meteo_data
where location_name = $1
and date_of_register >= $2
and date_of_register <= $3
order by date_of_register desc
limit $4 offset $5
`
func (pgx *pgxRepo) GetMeteoData(ctx context.Context, params GetMeteoData) ([]MeteoData, error) {
rows, err := pgx.Query(ctx, getMeteoDataQuery, params.Location, params.From, params.To, params.Limit, params.Offset)
if err != nil {
return nil, fmt.Errorf("error querying meteo data: %w", err)
}
defer rows.Close()
var results []MeteoData
for rows.Next() {
var data MeteoData
err := rows.Scan(
&data.Location,
&data.Timestamp,
&data.MaxTemp,
&data.MinTemp,
&data.Rainfall,
&data.Cloudiness,
)
if err != nil {
return nil, fmt.Errorf("error scanning row: %w", err)
}
results = append(results, data)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating rows: %w", err)
}
return results, nil
}