add get cities handler and improve db schema

This commit is contained in:
2025-10-29 16:14:25 +01:00
parent 5560fad92e
commit a5a2c5e24d
4 changed files with 39 additions and 4 deletions
+26 -3
View File
@@ -11,6 +11,7 @@ import (
type Repository interface {
InsertMeteoDataTX(ctx context.Context, params InsertMeteoDataParams) (*InsertMeteoDataResult, error)
UpdateBatchElapsedTime(ctx context.Context, batchID int, elapsedMS int) error
GetCities(ctx context.Context) []string
GetMeteoData(ctx context.Context, params GetMeteoData) ([]MeteoData, error)
}
@@ -150,10 +151,32 @@ func (pgx *pgxRepo) UpdateBatchElapsedTime(ctx context.Context, batchID int, ela
return nil
}
const getMeteoDataQuery = `
const getCities = `select distinct location_name from public.meteo_data order by location_name`
func (pgx *pgxRepo) GetCities(ctx context.Context) []string {
rows, err := pgx.Query(ctx, getCities)
if err != nil {
return []string{}
}
defer rows.Close()
var cities []string
for rows.Next() {
var city string
err := rows.Scan(&city)
if err != nil {
continue
}
cities = append(cities, city)
}
return cities
}
const getMeteoData = `
select location_name, date_of_register, max_temp, min_temp, rainfall, cloudiness
from public.meteo_data
where location_name = $1
where location_name_norm = unaccent(lower($1))
and date_of_register >= $2
and date_of_register <= $3
order by date_of_register desc
@@ -161,7 +184,7 @@ const getMeteoDataQuery = `
`
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)
rows, err := pgx.Query(ctx, getMeteoData, params.Location, params.From, params.To, params.Limit, params.Offset)
if err != nil {
return nil, fmt.Errorf("error querying meteo data: %w", err)
}