Skip to content

Commit

Permalink
get and set tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed Nov 2, 2024
1 parent f42a2cf commit 930d15d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
6 changes: 0 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,3 @@ pub fn metrolink_unix_fix(date: &str) -> u64 {

numbers.parse::<u64>().unwrap()
}

pub enum TileCategory {
IntercityRailShapesRaw = 0,
LocalRailShapesRaw = 1,
BusShapesRaw = 2,
}
4 changes: 2 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,5 +421,5 @@ pub struct TileStorage {
pub x: i32,
pub y: i32,
pub mvt_data: Vec<u8>,
pub added_time: chrono::DateTime<chrono::Utc>
}
pub added_time: chrono::DateTime<chrono::Utc>,
}
92 changes: 91 additions & 1 deletion src/tile_save_and_get.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,93 @@
pub fn insert_tile(z: i16, x: i32, y: i32) {
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
use diesel_async::AsyncPgConnection;
use diesel_async::RunQueryDsl;
use diesel::ExpressionMethods;
use diesel::QueryDsl;
use crate::models::TileStorage;
use diesel::SelectableHelper;

pub enum TileCategory {
IntercityRailShapesRaw,
LocalRailShapesRaw,
BusShapesRaw,
}

fn tile_enum_to_i16(x: TileCategory) -> i16 {
match x {
TileCategory::IntercityRailShapesRaw => 0,
TileCategory::LocalRailShapesRaw => 1,
TileCategory::BusShapesRaw => 2,
}
}

pub async fn insert_tile(
conn: &mut bb8::PooledConnection<'_, AsyncDieselConnectionManager<AsyncPgConnection>>,
category: TileCategory,
z: i16,
x: i32,
y: i32,
data: Vec<u8>,
) -> Result<(), anyhow::Error> {
let category_i16 = tile_enum_to_i16(category);

let _ = diesel::insert_into(crate::schema::gtfs::tile_storage::dsl::tile_storage)
.values(crate::models::TileStorage {
category: category_i16,
z: z,
x: x,
y: y,
mvt_data: data,
added_time: chrono::Utc::now()
})
.execute(conn)
.await?;

Ok(())
}

pub async fn delete_tile(
conn: &mut bb8::PooledConnection<'_, AsyncDieselConnectionManager<AsyncPgConnection>>,
category: TileCategory,
z: i16,
x: i32,
y: i32,
) -> Result<(), anyhow::Error> {
let category_i16 = tile_enum_to_i16(category);

let _ = diesel::delete(
crate::schema::gtfs::tile_storage::dsl::tile_storage
.filter(crate::schema::gtfs::tile_storage::dsl::category.eq(category_i16)
)
.filter(crate::schema::gtfs::tile_storage::dsl::z.eq(z)
)
.filter(crate::schema::gtfs::tile_storage::dsl::x.eq(x)
)
.filter(crate::schema::gtfs::tile_storage::dsl::y.eq(y)
)
);

Ok(())
}

pub async fn get_tile(
conn: &mut bb8::PooledConnection<'_, AsyncDieselConnectionManager<AsyncPgConnection>>,
category: TileCategory,
z: i16,
x: i32,
y: i32,
) -> diesel::result::QueryResult<crate::models::TileStorage> {
let category_i16 = tile_enum_to_i16(category);

crate::schema::gtfs::tile_storage::dsl::tile_storage
.filter(crate::schema::gtfs::tile_storage::dsl::category.eq(category_i16)
)
.filter(crate::schema::gtfs::tile_storage::dsl::z.eq(z)
)
.filter(crate::schema::gtfs::tile_storage::dsl::x.eq(x)
)
.filter(crate::schema::gtfs::tile_storage::dsl::y.eq(y)
)
.select(crate::models::TileStorage::as_select())
.first::<crate::models::TileStorage>(conn)
.await
}

0 comments on commit 930d15d

Please sign in to comment.