brianRingler / time-distance-tracker

https://time-distance-tracker.vercel.app
0 stars 1 forks source link

Update Route - `get-trips` #12

Closed brianRingler closed 11 months ago

brianRingler commented 1 year ago

I update the get-trips route by modifying the query so it now pulls in the location name.

    const queryStr = {
      text: `SELECT
        t.user_profiles_id, 
          json_agg(
            jsonb_build_object(
              'id', t.id, 
              'start_date', t.start_date, 
              'end_date', t.end_date, 
              'start_time', t.start_time, 
              'end_time', t.end_time, 
              'start_distance', t.start_distance, 
              'end_distance', t.end_distance, 
              'origin_location', jsonb_build_object(
                'id', lo.id, 
                'location_name', lo.location_name 
              ),
              'destination_location', jsonb_build_object(
                'id', ld.id, 
                'location_name', ld.location_name  
              )
            )
          ) AS trips_data
        FROM
          trips AS t 
        JOIN
          locations AS lo 
        ON
          t.origin_addr_id = lo.id 
        JOIN
          locations AS ld 
        ON
          t.destination_addr_id = ld.id 
        WHERE
          t.start_date >= $1::DATE 
          AND t.end_date <= $2::DATE 
          AND t.user_profiles_id = $3 
        GROUP BY
          t.user_profiles_id;`,
      values: ["2023-08-01", "2023-08-2", 1],
    };

I then had modify Trip.jsx based on the new format.:

[
  {
    id: 1,
    end_date: '2023-08-01',
    end_time: '10:00:00',
    start_date: '2023-08-01',
    start_time: '08:00:00',
    end_distance: 25.7,
    start_distance: 10.5,
    origin_location: { id: 1, location_name: 'Store 1' },
    destination_location: { id: 2, location_name: 'Store 2' }
  },
]

origin_location and destination_location now key to an object

Update to trips:

brianRingler commented 1 year ago

Also, forgot to checkout a branch and then do a pull request. I just pushed it from main. Will correct that habbit.

brianRingler commented 1 year ago

This is what it looks like: https://time-distance-tracker.vercel.app/trips. Forget to mention, I added a column in the locations table called location_name. This is what I pull instead of the full address.

imaricodes commented 1 year ago

Ok! All good!