package db import ( "context" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "github.com/jacek/pamietnik/backend/internal/domain" ) type StopStore struct { pool *pgxpool.Pool } func NewStopStore(pool *pgxpool.Pool) *StopStore { return &StopStore{pool: pool} } func (s *StopStore) ListByDate(ctx context.Context, userID, date string) ([]domain.Stop, error) { rows, err := s.pool.Query(ctx, ` SELECT st.stop_id, st.device_id, st.trip_id, st.start_ts, st.end_ts, st.center_lat, st.center_lon, st.duration_s, COALESCE(st.place_label, ''), st.place_details FROM stops st JOIN devices d ON d.device_id = st.device_id WHERE d.user_id = $1 AND DATE(st.start_ts AT TIME ZONE 'UTC') = $2::date ORDER BY st.start_ts`, userID, date, ) if err != nil { return nil, err } defer rows.Close() return pgx.CollectRows(rows, func(row pgx.CollectableRow) (domain.Stop, error) { var st domain.Stop err := row.Scan( &st.StopID, &st.DeviceID, &st.TripID, &st.StartTS, &st.EndTS, &st.CenterLat, &st.CenterLon, &st.DurationS, &st.PlaceLabel, &st.PlaceDetails, ) return st, err }) } func (s *StopStore) Insert(ctx context.Context, st domain.Stop) error { _, err := s.pool.Exec(ctx, ` INSERT INTO stops (stop_id, device_id, trip_id, start_ts, end_ts, center_lat, center_lon, duration_s, place_label, place_details) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) ON CONFLICT (stop_id) DO NOTHING`, st.StopID, st.DeviceID, st.TripID, st.StartTS, st.EndTS, st.CenterLat, st.CenterLon, st.DurationS, st.PlaceLabel, st.PlaceDetails, ) return err }