From e2aca03330c0bd9c0a550b4feac6d3ec0c389464 Mon Sep 17 00:00:00 2001 From: Jan Schutte Date: Wed, 15 Nov 2023 22:19:24 +0100 Subject: [PATCH] Move /bar routes into subdir --- backend/src/main.rs | 62 +++----------------------------------- backend/src/routes/bars.rs | 59 ++++++++++++++++++++++++++++++++++++ backend/src/routes/mod.rs | 1 + 3 files changed, 64 insertions(+), 58 deletions(-) create mode 100644 backend/src/routes/bars.rs create mode 100644 backend/src/routes/mod.rs diff --git a/backend/src/main.rs b/backend/src/main.rs index 905af9b..54209fd 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,71 +1,17 @@ -#[macro_use] -extern crate rocket; extern crate diesel; extern crate kroeg; +#[macro_use] +extern crate rocket; +mod routes; -use diesel::result::Error; -use diesel::SelectableHelper; -use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; use kroeg::db::Db; -use kroeg::models::{Location, LocationResponse, NewLocation}; use rocket::fairing::{Fairing, Info, Kind}; use rocket::fs::FileServer; use rocket::http::Header; -use rocket::serde::json::Json; use rocket::{Request, Response}; use serde::Deserialize; - pub struct CORS; -async fn get_bars(conn: Db) -> Result, Error> { - use kroeg::schema::locations::dsl::*; - - conn.run(|c| locations.filter(published.eq(true)).load(c)) - .await -} - -#[get("/bars")] -async fn bars(conn: Db) -> Json> { - let bars: Vec = get_bars(conn).await.expect("has values"); - - let response = bars.iter().map(|l| LocationResponse::from(l)).collect(); - - Json(response) -} - -#[post("/bar", data = "")] -async fn add_bar(conn: Db, bar: Json) -> Json { - // TODO: Find a better way of processing all of these structures - use kroeg::models::Point; - use kroeg::schema::locations; - - let coordinate = Point { - x: bar.coordinates.x, - y: bar.coordinates.y, - srid: Some(4326), - }; - let new_bar = NewLocation { - name: bar.name.clone(), - coordinates: coordinate, - published: bar.published, - description: bar.description.clone(), - osm_node_id: bar.osm_node_id.clone(), - google_place_id: bar.google_place_id.clone(), - imageurl: bar.imageurl.clone(), - }; - - let in_db = conn - .run(|conn| { - diesel::insert_into(locations::table) - .values(new_bar) - .returning(Location::as_returning()) - .get_result(conn) - }) - .await; - - Json(LocationResponse::from(&in_db.expect("Inserted"))) -} - #[rocket::async_trait] impl Fairing for CORS { fn info(&self) -> Info { @@ -104,6 +50,6 @@ fn rocket() -> _ { rocket .attach(Db::fairing()) .attach(CORS) - .mount("/", routes![bars, add_bar]) + .mount("/", routes::bars::routes()) .mount("/", FileServer::from(config.static_file_path)) } diff --git a/backend/src/routes/bars.rs b/backend/src/routes/bars.rs new file mode 100644 index 0000000..90ef0b1 --- /dev/null +++ b/backend/src/routes/bars.rs @@ -0,0 +1,59 @@ +use diesel::result::Error; +use diesel::SelectableHelper; +use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; +use kroeg::db::Db; +use kroeg::models::{Location, LocationResponse, NewLocation}; +use rocket::serde::json::Json; + +async fn get_bars(conn: Db) -> Result, Error> { + use kroeg::schema::locations::dsl::*; + + conn.run(|c| locations.filter(published.eq(true)).load(c)) + .await +} + +#[get("/bars")] +async fn bars(conn: Db) -> Json> { + let bars: Vec = get_bars(conn).await.expect("has values"); + + let response = bars.iter().map(|l| LocationResponse::from(l)).collect(); + + Json(response) +} + +#[post("/bar", data = "")] +async fn add_bar(conn: Db, bar: Json) -> Json { + // TODO: Find a better way of processing all of these structures + use kroeg::models::Point; + use kroeg::schema::locations; + + let coordinate = Point { + x: bar.coordinates.x, + y: bar.coordinates.y, + srid: Some(4326), + }; + let new_bar = NewLocation { + name: bar.name.clone(), + coordinates: coordinate, + published: bar.published, + description: bar.description.clone(), + osm_node_id: bar.osm_node_id.clone(), + google_place_id: bar.google_place_id.clone(), + imageurl: bar.imageurl.clone(), + }; + + let in_db = conn + .run(|conn| { + diesel::insert_into(locations::table) + .values(new_bar) + .returning(Location::as_returning()) + .get_result(conn) + }) + .await; + + Json(LocationResponse::from(&in_db.expect("Inserted"))) +} + +pub fn routes() -> Vec { + routes![add_bar, bars] +} diff --git a/backend/src/routes/mod.rs b/backend/src/routes/mod.rs new file mode 100644 index 0000000..6608278 --- /dev/null +++ b/backend/src/routes/mod.rs @@ -0,0 +1 @@ +pub mod bars;