From 5b869846456274114fa213d4d51ed144b05e11ce Mon Sep 17 00:00:00 2001 From: Franklin Date: Sun, 19 Mar 2023 17:06:21 -0400 Subject: [PATCH] Backend tests done. Everything looks great --- src/routes/main_router.rs | 28 ++++++++++++++++++---------- src/routes/read.rs | 2 +- src/services/admin.rs | 24 ++++++++++++++++-------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/routes/main_router.rs b/src/routes/main_router.rs index 1c08ed3..e5ce445 100644 --- a/src/routes/main_router.rs +++ b/src/routes/main_router.rs @@ -19,16 +19,24 @@ pub async fn start_all_routes(start_time: i64, db_conn: Arc) -> Result<( .app_data(client_state.clone()) .app_data(web::Data::new(db_conn.clone())) .service( - web::scope("/admin"), /*.service(super::admin::create_new_agent_profile) - .service(super::admin::create_new_location) - .service(super::admin::create_new_property) - .service(super::admin::update_agent_info) - .service(super::admin::update_listing)) - .service(web::scope("/read") - .service(super::read::get_all_agents) - .service(super::read::get_listing_container) - .service(super::read::get_property_listings_paged)*/ - ) + web::scope("/admin") + .service(super::admin::create_new_agent_profile) + .service(super::admin::create_new_location) + .service(super::admin::create_new_project) + .service(super::admin::create_new_unit) + .service(super::admin::update_agent) + .service(super::admin::update_project) + .service(super::admin::update_unit) + .service(super::admin::delete_agent) + .service(super::admin::delete_location) + .service(super::admin::delete_project) + .service(super::admin::delete_unit)) + .service(web::scope("/read") + .service(super::read::get_all_agents) + .service(super::read::get_all_locations) + .service(super::read::get_locations_in_city) + .service(super::read::get_projects_paged) + .service(super::read::get_project_data)) }) .bind((HOST_ADDR, HOST_PORT))? .run(); diff --git a/src/routes/read.rs b/src/routes/read.rs index 9a2fb84..1367b1b 100644 --- a/src/routes/read.rs +++ b/src/routes/read.rs @@ -51,7 +51,7 @@ pub async fn get_projects_paged( services::read::get_projects_paged(&db_conn, &page, &filters).await } -#[get("/projects/{project_id}")] +#[get("/project/{project_id}")] pub async fn get_project_data( db_conn: web::Data>, project_id: Path, diff --git a/src/services/admin.rs b/src/services/admin.rs index aa5eb49..1c22953 100644 --- a/src/services/admin.rs +++ b/src/services/admin.rs @@ -138,34 +138,42 @@ pub async fn update_unit( pub async fn delete_project(conn: &PgPool, project_id: &Uuid) -> TypedHttpResponse<()> { let mut tx = handle_tx!(conn.begin()); //let persisted_project = unwrap_or_not_found!(handle_db_read_op!(dao::project::get_with_id(conn, project_id)), "projects"); - // TODO: Test to see if this gives an error if no units are found handle_db_write_op!(dao::unit::delete_all_from_project(&mut tx, project_id), tx); // handle_db_write_op!(dao::location::delete_location(&mut tx, persisted_project.location_id)); - handle_db_write_op!(dao::project::delete(&mut tx, project_id), tx); + let res = handle_db_write_op!(dao::project::delete(&mut tx, project_id), tx); + if res.rows_affected() == 0 { + return TypedHttpResponse::return_empty_response(404); + } handle_tx!(tx.commit()); success!(()) } pub async fn delete_location(conn: &PgPool, location_id: &Uuid) -> TypedHttpResponse<()> { let mut tx = handle_tx!(conn.begin()); - // TODO: Test to see if this gives an error if no locations are found - handle_db_write_op!(dao::location::delete_location(&mut tx, location_id), tx); + let res = handle_db_write_op!(dao::location::delete_location(&mut tx, location_id), tx); + if res.rows_affected() == 0 { + return TypedHttpResponse::return_empty_response(404); + } handle_tx!(tx.commit()); success!(()) } pub async fn delete_agent(conn: &PgPool, agent_id: &Uuid) -> TypedHttpResponse<()> { let mut tx = handle_tx!(conn.begin()); - // TODO: Test to see if this gives an error if no agents are found - handle_db_write_op!(dao::agent::delete_agent(&mut tx, agent_id), tx); + let res = handle_db_write_op!(dao::agent::delete_agent(&mut tx, agent_id), tx); + if res.rows_affected() == 0 { + return TypedHttpResponse::return_empty_response(404); + } handle_tx!(tx.commit()); success!(()) } pub async fn delete_unit(conn: &PgPool, unit_id: &Uuid) -> TypedHttpResponse<()> { let mut tx = handle_tx!(conn.begin()); - // TODO: Test to see if this gives an error if no units are found - handle_db_write_op!(dao::unit::delete(&mut tx, unit_id), tx); + let res = handle_db_write_op!(dao::unit::delete(&mut tx, unit_id), tx); + if res.rows_affected() == 0 { + return TypedHttpResponse::return_empty_response(404); + } handle_tx!(tx.commit()); success!(()) }