From 11e50e202cb142c553efa7ede1eb643f5c7ecb72 Mon Sep 17 00:00:00 2001 From: Jan Schutte <4732389+SchutteJan@users.noreply.github.com> Date: Thu, 18 Jul 2024 18:50:10 +0200 Subject: [PATCH] feat: add area name to bar responses --- backend/src/db/locations.rs | 28 +++++++++++++++++++++++++--- backend/src/models/locations.rs | 2 ++ backend/src/routes/bars.rs | 4 +--- frontend/src/lib/BarItem.svelte | 5 ++++- frontend/src/models/schemas.ts | 1 + 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/backend/src/db/locations.rs b/backend/src/db/locations.rs index 09daa96..10f33c0 100644 --- a/backend/src/db/locations.rs +++ b/backend/src/db/locations.rs @@ -3,21 +3,41 @@ use diesel::{ BoolExpressionMethods, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper, }; +use postgis_diesel::functions::st_contains; use crate::db::DbConn; use crate::models::locations::{Location, LocationResponse, NewLocation, UpdateLocation}; -pub async fn get_bars(conn: &DbConn) -> Result, Error> { +pub async fn get_bars(conn: &DbConn) -> Result, Error> { + use crate::schema::areas; use crate::schema::locations::dsl::*; - conn.run(|c| locations.filter(published.eq(true)).load(c)) - .await + conn.run(|c| { + locations + .filter(published.eq(true)) + .left_join(areas::table.on(st_contains(areas::area, coordinates))) + .select(( + id, + name, + description, + coordinates, + imageurl, + address_line, + diesel::dsl::sql::>( + "NULL", + ), + areas::name.nullable(), + )) + .load(c) + }) + .await } pub async fn get_bars_with_visits( user_id: i32, conn: &DbConn, ) -> Result, Error> { + use crate::schema::areas; use crate::schema::locations; use crate::schema::visits; @@ -28,6 +48,7 @@ pub async fn get_bars_with_visits( .eq(locations::id) .and(visits::user_id.eq(user_id))), ) + .left_join(areas::table.on(st_contains(areas::area, locations::coordinates))) .select(( locations::id, locations::name, @@ -36,6 +57,7 @@ pub async fn get_bars_with_visits( locations::imageurl, locations::address_line, visits::visited_at.nullable(), + areas::name.nullable(), )) .load(c) }) diff --git a/backend/src/models/locations.rs b/backend/src/models/locations.rs index 67abcc8..1906f61 100644 --- a/backend/src/models/locations.rs +++ b/backend/src/models/locations.rs @@ -31,6 +31,7 @@ pub struct LocationResponse { pub imageurl: Option, pub address_line: String, pub visited_at: Option, + pub area_name: Option, } #[derive(Deserialize, Insertable)] @@ -88,6 +89,7 @@ impl From<&Location> for LocationResponse { imageurl: l.imageurl.clone(), visited_at: None, address_line: l.address_line.clone(), + area_name: None, } } } diff --git a/backend/src/routes/bars.rs b/backend/src/routes/bars.rs index cc70ed1..ab8f293 100644 --- a/backend/src/routes/bars.rs +++ b/backend/src/routes/bars.rs @@ -12,9 +12,7 @@ async fn bars(conn: DbConn) -> Result>, Status> { let bars = locations::get_bars(&conn).await; match bars { - Ok(bar_list) => Ok(Json( - bar_list.iter().map(|l| LocationResponse::from(l)).collect(), - )), + Ok(bar_list) => Ok(Json(bar_list)), Err(_) => Err(Status::InternalServerError), } } diff --git a/frontend/src/lib/BarItem.svelte b/frontend/src/lib/BarItem.svelte index 6a659c2..2df0a29 100644 --- a/frontend/src/lib/BarItem.svelte +++ b/frontend/src/lib/BarItem.svelte @@ -34,7 +34,10 @@

{bar.name}

-

{bar.address_line} • Oud-Oost

+

+ {bar.address_line} • + {bar.area_name ? bar.area_name : 'Unknown Area'} +

{#if isLoggedIn} {#if bar.visited_at} diff --git a/frontend/src/models/schemas.ts b/frontend/src/models/schemas.ts index f33a16f..00dc79d 100644 --- a/frontend/src/models/schemas.ts +++ b/frontend/src/models/schemas.ts @@ -41,6 +41,7 @@ export interface Point { } export interface LocationResponse { address_line: string + area_name?: string | null coordinates: Point description?: string | null id: number