diff --git a/src/app.rs b/src/app.rs index 1e9620a..54fd3a6 100644 --- a/src/app.rs +++ b/src/app.rs @@ -181,10 +181,7 @@ pub async fn app(bind: SocketAddr) -> Result<()> { get({ info!("GET /get_spot"); let conn = Arc::clone(&conn); - move |query: Query>| { - let name = query.0.get("name").cloned(); - spot::get_one_spot(name, conn) - } + move |Query(query)| spot::get_one_spot(query, conn) }), ) .route( @@ -200,10 +197,7 @@ pub async fn app(bind: SocketAddr) -> Result<()> { delete({ info!("DELETE /delete_spot"); let conn = Arc::clone(&conn); - move |query: Query>| { - let name = query.0.get("name").cloned(); - spot::delte_spot(name, conn) - } + move |Query(query)| spot::delte_spot(query, conn) }), ) .route( diff --git a/src/app/lending.rs b/src/app/lending.rs index 8aec740..d2a533f 100644 --- a/src/app/lending.rs +++ b/src/app/lending.rs @@ -84,7 +84,7 @@ pub async fn get_lending_list(conn: Arc>) -> ReturnData, conn: Arc>) -> StatusCode { +pub async fn insert_spot(Json(spot): Json, conn: Arc>) -> ReturnData<()> { info!("Try insert spot: {spot:?}"); - match crate::database::insert_spot::insert_spot(&*conn, spot.clone()).await { - Ok(()) => { - info!("Success insert spot[{}]", &spot.name); - StatusCode::ACCEPTED - } - Err(err) => { - error!("Failed insert spot[{}]: {err}", &spot.name); - StatusCode::BAD_REQUEST - } - } + let res = crate::database::insert_spot::insert_spot(&*conn, spot.clone()).await; + result_to_handler_with_log( + |_| Some(format!("Success insert spot[{}]", &spot.name)), + |e| Some(format!("{e} spot[{}]", &spot.name)), + &res, + ) + .await } /// 地点情報の更新を行うエンドポイント -pub async fn update_spot(Json(spot): Json, conn: Arc>) -> StatusCode { +pub async fn update_spot(Json(spot): Json, conn: Arc>) -> ReturnData<()> { info!("Try update spot: {spot:?}"); - match crate::database::update_spot::update_spot(&*conn, spot.clone()).await { - Ok(()) => { - info!("Success update spot[{}]", &spot.name); - StatusCode::ACCEPTED - } - Err(err) => { - error!("Failed update spot[{}]: {err}", &spot.name); - StatusCode::BAD_REQUEST - } - } + let res = crate::database::update_spot::update_spot(&*conn, spot.clone()).await; + result_to_handler_with_log( + |_| Some(format!("Success update spot[{}]", &spot.name)), + |e| Some(format!("{e} spot[{}]", &spot.name)), + &res, + ) + .await } /// 地点情報の取得を行うエンドポイント -pub async fn get_one_spot(name: Option, conn: Arc>) -> Json> { - match name { +pub async fn get_one_spot( + query: HashMap, + conn: Arc>, +) -> ReturnData { + match query.get("name") { Some(name) => { info!("Try get one spot info: {name}"); - match crate::database::get_one_spot::get_one_spot(&*conn, &name).await { - Ok(spot) => { - if spot.is_some() { - info!("Success get spot with name[{}]", &name); - } else { - info!("Not found spot[{}]", &name); - } - Json(spot) - } - Err(err) => { - error!("Failed get spot[{}]: {err}", &name); - Json(None) - } - } + let res = crate::database::get_one_spot::get_one_spot(&*conn, name).await; + result_to_handler_with_log( + |_| Some(format!("Success get spot with name[{name}]")), + |e| Some(format!("{e} spot[{name}]")), + &res, + ) + .await } None => { - error!("Not found spot name"); - Json(None) + let err = Err(QrError::UrlQuery("name".to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } } /// 地点情報一覧の取得を行うエンドポイント -pub async fn get_spot_list(conn: Arc>) -> Json>> { +pub async fn get_spot_list(conn: Arc>) -> ReturnData> { info!("Try get spot list"); - match crate::database::get_spot_list::get_spot_list(&*conn).await { - Ok(spot) => { - info!("Success get spot list"); - Json(Some(spot)) - } - Err(err) => { - error!("Failed get spot list: {err}"); - Json(None) - } - } + let res = crate::database::get_spot_list::get_spot_list(&*conn).await; + result_to_handler_with_log( + |_| Some("Success get spot list".to_string()), + |e| Some(e.to_string()), + &res, + ) + .await } /// 地点情報の削除を行うエンドポイント -pub async fn delte_spot(name: Option, conn: Arc>) -> StatusCode { - match name { +pub async fn delte_spot( + query: HashMap, + conn: Arc>, +) -> ReturnData<()> { + match query.get("name") { Some(name) => { - info!("Try delete spot: {name}"); - match crate::database::delete_spot::delete_spot(&*conn, &name).await { - Ok(()) => { - info!("Success delete spot[{name}]"); - StatusCode::OK - } - Err(err) => { - error!("Failed delete spot[{name}]: {err}"); - StatusCode::BAD_REQUEST - } - } + info!("Try get one spot info: {name}"); + let res = crate::database::delete_spot::delete_spot(&*conn, name).await; + result_to_handler_with_log( + |_| Some(format!("Success delete spot[{name}]")), + |e| Some(format!("{e} spot[{name}]")), + &res, + ) + .await } None => { - error!("Not found spot name"); - StatusCode::BAD_REQUEST + let err = Err(QrError::UrlQuery("name".to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } } - -#[cfg(test)] -mod tests { - use axum::{extract::Json, http::StatusCode}; - use serde_json::json; - use sqlx::{pool::Pool, Postgres}; - use std::sync::Arc; - - use crate::app::spot::insert_spot; - - #[sqlx::test(migrations = "./migrations")] - async fn test_insert_spot(pool: Pool) { - let conn = Arc::new(pool); - let status_code = insert_spot( - Json( - serde_json::from_value(json!({ - "name": "test", - "area": "area1", - "building": "3C", - "floor": 2, - })) - .unwrap(), - ), - conn, - ) - .await; - assert_eq!(status_code, StatusCode::ACCEPTED) - } -} diff --git a/src/database/delete_spot.rs b/src/database/delete_spot.rs index 0bd409c..58d8763 100644 --- a/src/database/delete_spot.rs +++ b/src/database/delete_spot.rs @@ -31,11 +31,11 @@ mod tests { .unwrap(); insert_spot(&pool, info).await.unwrap(); - let result = get_one_spot(&pool, name).await.unwrap(); - assert!(result.is_some()); + let result = get_one_spot(&pool, name).await; + assert!(result.is_ok()); delete_spot(&pool, name).await.unwrap(); - let result = get_one_spot(&pool, name).await.unwrap(); - assert!(result.is_none()); + let result = get_one_spot(&pool, name).await; + assert!(result.is_err()); } } diff --git a/src/database/get_one_spot.rs b/src/database/get_one_spot.rs index b591303..0b1b28e 100644 --- a/src/database/get_one_spot.rs +++ b/src/database/get_one_spot.rs @@ -3,7 +3,7 @@ use crate::{ Spot, }; -pub async fn get_one_spot<'a, E>(conn: E, name: &str) -> Result> +pub async fn get_one_spot<'a, E>(conn: E, name: &str) -> Result where E: sqlx::Executor<'a, Database = sqlx::Postgres>, { @@ -11,8 +11,11 @@ where .fetch_optional(conn) .await .map_err(|_| QrError::DatabaseGet("spot".to_string()))?; - - Ok(spot_opt) + if let Some(spot) = spot_opt { + Ok(spot) + } else { + Err(QrError::DatabaseNotFound(name.to_string())) + } } #[cfg(test)] @@ -33,10 +36,10 @@ mod tests { .unwrap(); insert_spot(&pool, info).await.unwrap(); - let result: Option = get_one_spot(&pool, "test1").await.unwrap(); - assert!(result.is_some()); + let result = get_one_spot(&pool, "test1").await; + assert!(result.is_ok()); - let result: Option = get_one_spot(&pool, "test2").await.unwrap(); - assert!(result.is_none()); + let result = get_one_spot(&pool, "test2").await; + assert!(result.is_err()); } }