Skip to content

Commit

Permalink
Add delete bar endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SchutteJan committed Nov 15, 2023
1 parent e2aca03 commit 6ae4254
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
9 changes: 9 additions & 0 deletions backend/requests/locations.http
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions backend/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pub struct NewLocation {
pub imageurl: Option<String>,
}

#[derive(Deserialize)]
pub struct DeleteRequest {
pub id: i32,
}

// TODO: Implement traits for Serde and JsonSchema for Point type
impl From<Point> for Coordinate {
fn from(value: Point) -> Self {
Expand Down
25 changes: 23 additions & 2 deletions backend/src/routes/bars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Location>, Error> {
Expand Down Expand Up @@ -54,6 +56,25 @@ async fn add_bar(conn: Db, bar: Json<NewLocation>) -> Json<LocationResponse> {
Json(LocationResponse::from(&in_db.expect("Inserted")))
}

#[delete("/bar", data = "<bar>")]
async fn delete_bar(conn: Db, bar: Json<DeleteRequest>) -> 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<rocket::Route> {
routes![add_bar, bars]
routes![add_bar, bars, delete_bar]
}

0 comments on commit 6ae4254

Please sign in to comment.