From 6ae4254589eb7d46cd23dd2e28067d0eda7c9a3b Mon Sep 17 00:00:00 2001 From: Jan Schutte Date: Wed, 15 Nov 2023 23:11:03 +0100 Subject: [PATCH] Add delete bar endpoint --- backend/requests/locations.http | 9 +++++++++ backend/src/models.rs | 5 +++++ backend/src/routes/bars.rs | 25 +++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/backend/requests/locations.http b/backend/requests/locations.http index ca9f96f..afb6e25 100644 --- a/backend/requests/locations.http +++ b/backend/requests/locations.http @@ -20,3 +20,12 @@ Content-Type: application/json "google_place_id": "123ASD", "imageurl": "http://placekitten.com/g/400/400" } + +### Remove location +DELETE {{base}}/bar +Content-Type: application/json +Accept: application/json + +{ + "id": 1 +} diff --git a/backend/src/models.rs b/backend/src/models.rs index 3fb4322..6bb86a7 100644 --- a/backend/src/models.rs +++ b/backend/src/models.rs @@ -45,6 +45,11 @@ pub struct NewLocation { pub imageurl: Option, } +#[derive(Deserialize)] +pub struct DeleteRequest { + pub id: i32, +} + // TODO: Implement traits for Serde and JsonSchema for Point type impl From for Coordinate { fn from(value: Point) -> Self { diff --git a/backend/src/routes/bars.rs b/backend/src/routes/bars.rs index 90ef0b1..f45a841 100644 --- a/backend/src/routes/bars.rs +++ b/backend/src/routes/bars.rs @@ -2,7 +2,9 @@ use diesel::result::Error; use diesel::SelectableHelper; use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; use kroeg::db::Db; -use kroeg::models::{Location, LocationResponse, NewLocation}; +use kroeg::models::{DeleteRequest, Location, LocationResponse, NewLocation}; +use kroeg::schema::locations::id; +use rocket::http::Status; use rocket::serde::json::Json; async fn get_bars(conn: Db) -> Result, Error> { @@ -54,6 +56,25 @@ async fn add_bar(conn: Db, bar: Json) -> Json { Json(LocationResponse::from(&in_db.expect("Inserted"))) } +#[delete("/bar", data = "")] +async fn delete_bar(conn: Db, bar: Json) -> Status { + use kroeg::schema::locations; + + let deleted_location = conn + .run(move |conn| { + diesel::delete(locations::table) + .filter(id.eq(bar.id)) + .execute(conn) + }) + .await; + + match deleted_location { + Ok(0) => Status::NotFound, + Ok(_n) => Status::Ok, + Err(..) => Status::InternalServerError, + } +} + pub fn routes() -> Vec { - routes![add_bar, bars] + routes![add_bar, bars, delete_bar] }