Skip to content

Commit

Permalink
Move /bar routes into subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
SchutteJan committed Nov 15, 2023
1 parent 408efe7 commit e2aca03
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 58 deletions.
62 changes: 4 additions & 58 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<Location>, 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<Vec<LocationResponse>> {
let bars: Vec<Location> = get_bars(conn).await.expect("has values");

let response = bars.iter().map(|l| LocationResponse::from(l)).collect();

Json(response)
}

#[post("/bar", data = "<bar>")]
async fn add_bar(conn: Db, bar: Json<NewLocation>) -> Json<LocationResponse> {
// 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 {
Expand Down Expand Up @@ -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))
}
59 changes: 59 additions & 0 deletions backend/src/routes/bars.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<Location>, 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<Vec<LocationResponse>> {
let bars: Vec<Location> = get_bars(conn).await.expect("has values");

let response = bars.iter().map(|l| LocationResponse::from(l)).collect();

Json(response)
}

#[post("/bar", data = "<bar>")]
async fn add_bar(conn: Db, bar: Json<NewLocation>) -> Json<LocationResponse> {
// 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<rocket::Route> {
routes![add_bar, bars]
}
1 change: 1 addition & 0 deletions backend/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bars;

0 comments on commit e2aca03

Please sign in to comment.