add get cities handler and improve db schema
This commit is contained in:
@@ -79,6 +79,7 @@ func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h *Handler) GetCities(w http.ResponseWriter, r *http.Request) {
|
||||
h.ToJSON(w, http.StatusOK, app.H{"cities": h.s.GetCities(r.Context())})
|
||||
}
|
||||
|
||||
func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -49,6 +49,10 @@ func (s *Service) UpdateElapsedMS(ctx context.Context, batchID, elapsedMS int) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) GetCities(ctx context.Context) []string {
|
||||
return s.repo.GetCities(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) GetMeteoData(ctx context.Context, params GetMeteoData) ([]MeteoData, error) {
|
||||
return s.repo.GetMeteoData(ctx, params)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user