diff --git a/src/app.rs b/src/app.rs index 921b324..1e9620a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,7 +12,6 @@ use std::net::SocketAddr; use std::sync::Arc; use tower_http::cors::{Any, CorsLayer}; use tracing::*; -use uuid::Uuid; use crate::search_engine; @@ -84,10 +83,7 @@ pub async fn app(bind: SocketAddr) -> Result<()> { info!("DELETE /delete_fixtures"); let conn = Arc::clone(&conn); let context = Arc::clone(&search_fixtures_context); - move |query: Query>| { - let uuid_opt = query.0.get("id").and_then(|s| Uuid::parse_str(s).ok()); - fixtures::delete_fixtures(uuid_opt, conn, context) - } + move |Query(query)| fixtures::delete_fixtures(query, conn, context) }), ) .route( @@ -134,11 +130,9 @@ pub async fn app(bind: SocketAddr) -> Result<()> { post({ info!("POST /returned_lending"); let conn = Arc::clone(&conn); - move |query: Query>| { - let uuid_opt = query.0.get("id").and_then(|s| Uuid::parse_str(s).ok()); - let qr_id_opt = query.0.get("qr_id").cloned(); + move |Query(query)| { let now = Utc::now(); - lending::returned_lending(uuid_opt, qr_id_opt, now, conn) + lending::returned_lending(query, now, conn) } }), ) diff --git a/src/app/container.rs b/src/app/container.rs index 3b92d6b..d3cc456 100644 --- a/src/app/container.rs +++ b/src/app/container.rs @@ -1,5 +1,8 @@ -use crate::Container; -use axum::{extract::Json, http::StatusCode}; +use crate::{ + error_handling::{result_to_handler_with_log, ReturnData}, + Container, +}; +use axum::extract::Json; use sqlx::{pool::Pool, postgres::Postgres}; use std::sync::Arc; use tracing::*; @@ -7,16 +10,13 @@ use tracing::*; pub async fn insert_container( Json(container): Json, conn: Arc>, -) -> StatusCode { +) -> ReturnData<()> { info!("Try insert container: {container:?}"); - match crate::database::insert_container::insert_container(&*conn, container.clone()).await { - Ok(()) => { - info!("Success insert container[{}]", &container.id); - StatusCode::ACCEPTED - } - Err(err) => { - error!("Failed insert container[{}]: {err}", &container.id); - StatusCode::BAD_REQUEST - } - } + let res = crate::database::insert_container::insert_container(&*conn, container.clone()).await; + result_to_handler_with_log( + |_| Some(format!("Success insert container[{}]", &container.id)), + |e| Some(format!("{e} [{}]", &container.id)), + &res, + ) + .await } diff --git a/src/app/fixtures.rs b/src/app/fixtures.rs index 892ebc2..d47de68 100644 --- a/src/app/fixtures.rs +++ b/src/app/fixtures.rs @@ -1,7 +1,8 @@ use crate::database::get_one_fixtures::{get_one_fixtures, IdType}; +use crate::error_handling::{result_to_handler_with_log, QrError, ReturnData}; use crate::search_engine::{SearchFixtures, SearchResult}; use crate::Fixtures; -use axum::{extract::Json, http::StatusCode}; +use axum::extract::Json; use sqlx::{pool::Pool, postgres::Postgres}; use std::collections::HashMap; use std::sync::Arc; @@ -14,29 +15,33 @@ pub async fn insert_fixtures( Json(fixtures): Json, conn: Arc>, context: Arc, -) -> StatusCode { +) -> ReturnData<()> { info!("Try insert fixtures: {fixtures:?}"); - match crate::database::insert_fixtures::insert_fixtures(&*conn, fixtures.clone()).await { - Ok(()) => { - info!("Success insert fixtures(DB)[{}]", &fixtures.id); - match context.add_or_replace(&[fixtures.clone()]).await { - Ok(_) => { - info!("Success insert fixtures(Search Engine)[{}]", &fixtures.id); - StatusCode::ACCEPTED - } - Err(err) => { - error!( - "Failed insert fixtures(Search Engine)[{}]: {err}", - &fixtures.id - ); - StatusCode::BAD_REQUEST - } - } - } - Err(err) => { - error!("Failed insert fixtures(DB)[{}]: {err}", &fixtures.id); - StatusCode::BAD_REQUEST - } + let res = crate::database::insert_fixtures::insert_fixtures(&*conn, fixtures.clone()).await; + + // DBの処理が成功した時の結果 + let r1 = result_to_handler_with_log( + |_| Some(format!("Success insert fixtures(DB)[{}]", &fixtures.id)), + |e| Some(format!("{e}[{}]", &fixtures.id)), + &res, + ) + .await; + + if res.is_ok() { + let res = context.add_or_replace(&[fixtures.clone()]).await; + result_to_handler_with_log( + |_| { + Some(format!( + "Success insert fixtures(Search Engine)[{}]", + &fixtures.id + )) + }, + |e| Some(format!("{e}[{}]", &fixtures.id)), + &res, + ) + .await + } else { + r1 } } @@ -44,116 +49,111 @@ pub async fn update_fixtures( Json(fixtures): Json, conn: Arc>, context: Arc, -) -> StatusCode { +) -> ReturnData<()> { info!("Try update fixtures: {fixtures:?}"); - match crate::database::update_fixtures::update_fixtures(&*conn, fixtures.clone()).await { - Ok(()) => { - info!("Success update fixtures(DB)[{}]", &fixtures.id); - match context.add_or_replace(&[fixtures.clone()]).await { - Ok(_) => { - info!("Success update fixtures(Search Engine)[{}]", &fixtures.id); - StatusCode::ACCEPTED - } - Err(err) => { - error!( - "Failed insert fixtures(Search Engine)[{}]: {err}", - &fixtures.id - ); - StatusCode::BAD_REQUEST - } - } - } - Err(err) => { - error!("Failed insert fixtures(DB)[{}]: {err}", &fixtures.id); - StatusCode::BAD_REQUEST - } + let res = crate::database::update_fixtures::update_fixtures(&*conn, fixtures.clone()).await; + + // DBの処理が成功した時の結果 + let r1 = result_to_handler_with_log( + |_| Some(format!("Success update fixtures(DB)[{}]", &fixtures.id)), + |e| Some(format!("{e}[{}]", &fixtures.id)), + &res, + ) + .await; + + if res.is_ok() { + let res = context.add_or_replace(&[fixtures.clone()]).await; + result_to_handler_with_log( + |_| { + Some(format!( + "Success update fixtures(Search Engine)[{}]", + &fixtures.id + )) + }, + |e| Some(format!("{e}[{}]", &fixtures.id)), + &res, + ) + .await + } else { + r1 } } pub async fn delete_fixtures( - uuid: Option, + query: HashMap, conn: Arc>, context: Arc, -) -> StatusCode { - match uuid { - Some(uuid) => { +) -> ReturnData<()> { + let id_opt = query.get("id"); + if let Some(id) = id_opt { + let uuid_opt = Uuid::parse_str(id).ok(); + if let Some(uuid) = uuid_opt { info!("Try delete fixtures: {uuid}"); - match crate::database::delete_fixtures::delete_fixtures(&*conn, uuid).await { - Ok(()) => { - info!("Success delete fixtures(DB)[{uuid}]"); - let context = &*context; - match context.delete(&[uuid]).await { - Ok(()) => { - info!("Success delete fixtures(Search Engine)[{uuid}]"); - StatusCode::ACCEPTED - } - Err(err) => { - error!("Failed insert fixtures(Search Engine)[{uuid}]: {err}"); - StatusCode::BAD_REQUEST - } - } - } - Err(err) => { - error!("Failed insert fixtures(DB)[{uuid}]: {err}"); - StatusCode::BAD_REQUEST - } + let res = crate::database::delete_fixtures::delete_fixtures(&*conn, uuid).await; + + // DBの処理が成功した時の結果 + let r1 = result_to_handler_with_log( + |_| Some(format!("Success delete fixtures(DB)[{uuid}]")), + |e| Some(format!("{e}[{uuid}]")), + &res, + ) + .await; + + if res.is_ok() { + let res = context.delete(&[uuid]).await; + result_to_handler_with_log( + |_| Some(format!("Success delete fixtures(Search Engine)[{uuid}]")), + |e| Some(format!("{e}[{uuid}]")), + &res, + ) + .await + } else { + r1 } + } else { + let err = Err(QrError::BrokenUuid(id.to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } - None => { - error!("Not found uuid"); - StatusCode::BAD_REQUEST - } + } else { + let err = Err(QrError::UrlQuery("id".to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } pub async fn get_fixtures( query: HashMap, conn: Arc>, -) -> Json> { +) -> ReturnData { match (query.get("id"), query.get("qr_id")) { (Some(id), _) => { let uuid_opt = Uuid::parse_str(id).ok(); if let Some(uuid) = uuid_opt { info!("Try get fixtures with uuid: {uuid}"); - match get_one_fixtures(&*conn, IdType::FixturesId(uuid)).await { - Ok(f) => { - if f.is_some() { - info!("Success get fixtures with uuid[{uuid}]"); - } else { - info!("Not found fixtures[{uuid}]"); - } - Json(f) - } - Err(err) => { - error!("Failed get fixtures with uuid[{uuid}]: {err}"); - Json(None) - } - } + let res = get_one_fixtures(&*conn, IdType::FixturesId(uuid)).await; + result_to_handler_with_log( + |_| Some(format!("Success get fixtures with uuid[{uuid}]")), + |e| Some(format!("{e}[{uuid}]")), + &res, + ) + .await } else { - error!("Break uuid: {id}"); - Json(None) + let err = Err(QrError::BrokenUuid(id.to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } (_, Some(qr_id)) => { info!("Try get fixtures with qr_id: {qr_id}"); - match get_one_fixtures(&*conn, IdType::QrId(qr_id.clone())).await { - Ok(f) => { - if f.is_some() { - info!("Success get fixtures with qr_id[{qr_id}]"); - } else { - info!("Failed get fixtures with qr_id[{qr_id}]"); - } - Json(f) - } - Err(err) => { - error!("Failed get fixtures[{qr_id}]: {err}"); - Json(None) - } - } + let res = get_one_fixtures(&*conn, IdType::QrId(qr_id.clone())).await; + result_to_handler_with_log( + |_| Some(format!("Success get fixtures with qr_id[{qr_id}]")), + |e| Some(format!("{e}[{qr_id}]")), + &res, + ) + .await } _ => { - error!("Invalid query"); - Json(None) + let err = Err(QrError::UrlQuery("qr_id, id".to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } } @@ -161,21 +161,18 @@ pub async fn get_fixtures( pub async fn search_fixtures( keywords_str: String, context: Arc, -) -> Json>>> { +) -> ReturnData>> { let keywords = keywords_str .split(',') // カンマ区切りであることを要求する .map(|s| s.to_string()) .collect::>(); let context = &*context; info!("Try search fixtures: {keywords:?}"); - match context.search(&keywords).await { - Ok(res) => { - info!("Success search fixtures[{keywords:?}]"); - Json(Some(res)) - } - Err(err) => { - error!("Failed search fixtures[{keywords:?}]: {err}"); - Json(None) - } - } + let res = context.search(&keywords).await; + result_to_handler_with_log( + |_| Some(format!("Success search fixtures[{keywords:?}]")), + |e| Some(format!("{e}[{keywords:?}]")), + &res, + ) + .await } diff --git a/src/app/lending.rs b/src/app/lending.rs index 1d0e641..8aec740 100644 --- a/src/app/lending.rs +++ b/src/app/lending.rs @@ -1,5 +1,8 @@ -use crate::Lending; -use axum::{extract::Json, http::StatusCode}; +use crate::{ + error_handling::{result_to_handler_with_log, QrError, ReturnData}, + Lending, +}; +use axum::extract::Json; use chrono::{DateTime, Utc}; use sqlx::{pool::Pool, postgres::Postgres}; use std::collections::HashMap; @@ -9,92 +12,89 @@ use uuid::Uuid; /// 備品情報の登録を行うエンドポイント /// - https://github.com/sohosai/qr-backend/issues/11 -pub async fn insert_lending(Json(lending): Json, conn: Arc>) -> StatusCode { +pub async fn insert_lending( + Json(lending): Json, + conn: Arc>, +) -> ReturnData<()> { info!("Try insert lending: {lending:?}"); - match crate::database::insert_lending::insert_lending(&*conn, lending.clone()).await { - Ok(()) => { - info!("Success insert lending[{}]", &lending.id); - StatusCode::ACCEPTED - } - Err(err) => { - error!("Failed insert lending[{}]: {err}", &lending.id); - StatusCode::BAD_REQUEST - } - } + let res = crate::database::insert_lending::insert_lending(&*conn, lending.clone()).await; + result_to_handler_with_log( + |_| Some(format!("Success insert lending[{}]", &lending.id)), + |e| Some(format!("{e}[{}]", &lending.id)), + &res, + ) + .await } pub async fn returned_lending( - id: Option, - qr_id: Option, + query: HashMap, returned_at: DateTime, conn: Arc>, -) -> StatusCode { +) -> ReturnData<()> { use crate::database::get_one_fixtures::*; use crate::database::returned_lending::*; - match id { - Some(id) => { - info!("Try returned lending with uuid[{}]", { id }); - match returned_lending(&*conn, id, returned_at).await { - Ok(()) => { - info!("Success returned lending with uuid[{}]", id); - StatusCode::ACCEPTED - } - Err(err) => { - error!("Failed retunred lending with uuid[{}]: {err}", id); - StatusCode::BAD_REQUEST - } + match (query.get("id"), query.get("qr_id")) { + (Some(id), _) => { + let uuid_opt = Uuid::parse_str(id).ok(); + if let Some(uuid) = uuid_opt { + info!("Try get fixtures with uuid: {uuid}"); + let res = returned_lending(&*conn, uuid, returned_at).await; + result_to_handler_with_log( + |_| Some(format!("Success returned lending with uuid[{uuid}]")), + |e| Some(format!("{e} uuid[{uuid}]")), + &res, + ) + .await + } else { + let err = Err(QrError::BrokenUuid(id.to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } - None => match qr_id { - // QRのIDに該当する物品情報を検索する - Some(qr_id) => { - info!("Try returned lending with qr_id[{}]", qr_id); - match get_one_fixtures(&*conn, IdType::QrId(qr_id.clone())).await { - Ok(Some(fixtures)) => { - let id = fixtures.id; - match returned_lending(&*conn, id, returned_at).await { - Ok(()) => { - info!("Success returned lending with qr_id[{}]", qr_id); - StatusCode::ACCEPTED - } - Err(err) => { - error!("Failed retunred lending with qr_id[{}]: {err}", qr_id); - StatusCode::BAD_REQUEST - } - } - } - _ => { - error!("Not found fixtures: {}", qr_id); - StatusCode::BAD_REQUEST - } + (_, Some(qr_id)) => { + info!("Try get fixtures with qr_id: {qr_id}"); + let fixtures = get_one_fixtures(&*conn, IdType::QrId(qr_id.clone())).await; + match fixtures { + Ok(fixtures) => { + let res = returned_lending(&*conn, fixtures.id, returned_at).await; + result_to_handler_with_log( + |_| Some(format!("Success returned lending with qr_id[{qr_id}]")), + |e| Some(format!("{e} qr_id[{qr_id}]")), + &res, + ) + .await + } + Err(e) => { + result_to_handler_with_log( + |_| None, + |e| Some(format!("{e} qr_id[{qr_id}]")), + &Err(e), + ) + .await } } - None => { - error!("Not found lending id"); - StatusCode::BAD_REQUEST - } - }, + } + _ => { + let err = Err(QrError::UrlQuery("qr_id, id".to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await + } } } -pub async fn get_lending_list(conn: Arc>) -> Json>> { +pub async fn get_lending_list(conn: Arc>) -> ReturnData> { info!("Try get lending list"); - match crate::database::get_lending_list::get_lending_list(&*conn).await { - Ok(v) => { - info!("Success get lending list"); - axum::Json(Some(v)) - } - Err(err) => { - error!("Failed get lending list: {err}"); - axum::Json(None) - } - } + let res = crate::database::get_lending_list::get_lending_list(&*conn).await; + result_to_handler_with_log( + |_| Some(format!("Success get lending list")), + |e| Some(e.to_string()), + &res, + ) + .await } pub async fn get_one_lending( query: HashMap, conn: Arc>, -) -> Json> { +) -> ReturnData { use crate::database::get_one_lending::*; match ( query.get("lending_id"), @@ -105,56 +105,53 @@ pub async fn get_one_lending( info!("Try get one lending info with lending_id[{lending_id}]"); let uuid_opt = Uuid::parse_str(lending_id).ok(); if let Some(uuid) = uuid_opt { - match get_one_lending(&*conn, IdType::LendingId(uuid)).await { - Ok(v) => { - info!("Success get lending with lending_id[{lending_id}]"); - axum::Json(v) - } - Err(err) => { - error!("Failed get lending with lending_id[{lending_id}]: {err}"); - axum::Json(None) - } - } + let res = get_one_lending(&*conn, IdType::LendingId(uuid)).await; + result_to_handler_with_log( + |_| Some(format!("Success get lending with lending_id[{lending_id}]")), + |e| Some(format!("{e} lending_id[{lending_id}]")), + &res, + ) + .await } else { - error!("Break uuid: {lending_id}"); - Json(None) + let err = Err(QrError::BrokenUuid(lending_id.to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } (_, Some(fixtures_id), _) => { info!("Try get one lending info with fixtures_id[{fixtures_id}]"); let uuid_opt = Uuid::parse_str(fixtures_id).ok(); if let Some(uuid) = uuid_opt { - match get_one_lending(&*conn, IdType::FixturesId(uuid)).await { - Ok(v) => { - info!("Success get lending with fixtures_id[{fixtures_id}]"); - axum::Json(v) - } - Err(err) => { - error!("Failed get lending with fixtures_id[{fixtures_id}]: {err}"); - axum::Json(None) - } - } + let res = get_one_lending(&*conn, IdType::FixturesId(uuid)).await; + result_to_handler_with_log( + |_| { + Some(format!( + "Success get lending with fixtures_id[{fixtures_id}]" + )) + }, + |e| Some(format!("{e} fixtures_id[{fixtures_id}]")), + &res, + ) + .await } else { - error!("Break uuid: {fixtures_id}"); - Json(None) + let err = Err(QrError::BrokenUuid(fixtures_id.to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } - (_, _, Some(qr_id)) => match { + (_, _, Some(qr_id)) => { info!("Try get one lending info with fixtures_qr_id[{qr_id}]"); - get_one_lending(&*conn, IdType::QrId(qr_id.to_string())).await - } { - Ok(v) => { - info!("Success get lending with fixtures_qr_id[{qr_id}]"); - axum::Json(v) - } - Err(err) => { - error!("Failed get lending with fixtures_qr_id[{qr_id}]: {err}"); - axum::Json(None) - } - }, + let res = get_one_lending(&*conn, IdType::QrId(qr_id.to_string())).await; + result_to_handler_with_log( + |_| Some(format!("Success get lending with fixtures_qr_id[{qr_id}]")), + |e| Some(format!("{e} fixtures_qr_id[{qr_id}]")), + &res, + ) + .await + } _ => { - error!("Invalid query"); - Json(None) + let err = Err(QrError::UrlQuery( + "lending_id, fixtures_id, fixtures_qr_id".to_string(), + )); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } } @@ -162,7 +159,7 @@ pub async fn get_one_lending( pub async fn get_is_lending( query: HashMap, conn: Arc>, -) -> Json { +) -> ReturnData { use crate::database::get_one_lending::*; info!("Check exist lending info"); match ( @@ -174,84 +171,60 @@ pub async fn get_is_lending( let uuid_opt = Uuid::parse_str(lending_id).ok(); if let Some(uuid) = uuid_opt { match get_one_lending(&*conn, IdType::LendingId(uuid)).await { - Ok(Some(_)) => axum::Json(true), - _ => axum::Json(false), + Ok(_) => result_to_handler_with_log(|_| None, |_| None, &Ok(true)).await, + Err(QrError::DatabaseNotFound(_)) => { + result_to_handler_with_log(|_| None, |_| None, &Ok(false)).await + } + Err(e) => result_to_handler_with_log(|_| None, |_| None, &Err(e)).await, } } else { - error!("Break uuid: {lending_id}"); - Json(false) + let err = Err(QrError::BrokenUuid(lending_id.to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } (_, Some(fixtures_id), _) => { let uuid_opt = Uuid::parse_str(fixtures_id).ok(); if let Some(uuid) = uuid_opt { match get_one_lending(&*conn, IdType::FixturesId(uuid)).await { - Ok(Some(_)) => axum::Json(true), - _ => axum::Json(false), + Ok(_) => result_to_handler_with_log(|_| None, |_| None, &Ok(true)).await, + Err(QrError::DatabaseNotFound(_)) => { + result_to_handler_with_log(|_| None, |_| None, &Ok(false)).await + } + Err(e) => result_to_handler_with_log(|_| None, |_| None, &Err(e)).await, } } else { - error!("Break uuid: {fixtures_id}"); - Json(false) + let err = Err(QrError::BrokenUuid(fixtures_id.to_string())); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await + } + } + (_, _, Some(qr_id)) => { + match get_one_lending(&*conn, IdType::QrId(qr_id.to_string())).await { + Ok(_) => result_to_handler_with_log(|_| None, |_| None, &Ok(true)).await, + Err(QrError::DatabaseNotFound(_)) => { + result_to_handler_with_log(|_| None, |_| None, &Ok(false)).await + } + Err(e) => result_to_handler_with_log(|_| None, |_| None, &Err(e)).await, } } - (_, _, Some(qr_id)) => match get_one_lending(&*conn, IdType::QrId(qr_id.to_string())).await - { - Ok(Some(_)) => axum::Json(true), - _ => axum::Json(false), - }, _ => { - error!("Invalid query"); - Json(false) + let err = Err(QrError::UrlQuery( + "lending_id, fixtures_id, fixtures_qr_id".to_string(), + )); + result_to_handler_with_log(|_| None, |e| Some(e.to_string()), &err).await } } } -pub async fn update_lending(Json(lending): Json, conn: Arc>) -> StatusCode { +pub async fn update_lending( + Json(lending): Json, + conn: Arc>, +) -> ReturnData<()> { info!("Try update lending: {lending:?}"); - match crate::database::update_lending::update_lending(&*conn, lending.clone()).await { - Ok(()) => { - info!("Success update lending[{}]", lending.id); - StatusCode::ACCEPTED - } - Err(err) => { - error!("Failed update lending[{}]: {err}", lending.id); - StatusCode::BAD_REQUEST - } - } -} - -#[cfg(test)] -mod tests { - use axum::{extract::Json, http::StatusCode}; - use serde_json::json; - use sqlx::{pool::Pool, Postgres}; - use std::sync::Arc; - use uuid::uuid; - - use crate::app::lending::insert_lending; - - #[sqlx::test(migrations = "./migrations")] - async fn test_insert_lending(pool: Pool) { - let conn = Arc::new(pool); - let id = uuid!("550e8400-e29b-41d4-a716-446655440000"); - let fixtures_id = uuid!("550e8400-e29b-41d4-a716-446655440001"); - let status_code = insert_lending( - Json( - serde_json::from_value(json!({ - "id": id, - "fixtures_id": fixtures_id, - "fixtures_qr_id": "x234", - "spot_name": "test", - "lending_at": "2023-08-07 15:56:35 UTC", - "borrower_name": "test", - "borrower_number": 202200000, - "borrower_org": "jsys" - })) - .unwrap(), - ), - conn, - ) - .await; - assert_eq!(status_code, StatusCode::ACCEPTED) - } + let res = crate::database::update_lending::update_lending(&*conn, lending.clone()).await; + result_to_handler_with_log( + |_| Some(format!("Success update lending[{}]", lending.id)), + |e| Some(format!("{e} lending[{}]", lending.id)), + &res, + ) + .await } diff --git a/src/database/delete_fixtures.rs b/src/database/delete_fixtures.rs index f590d03..e89be53 100644 --- a/src/database/delete_fixtures.rs +++ b/src/database/delete_fixtures.rs @@ -40,11 +40,11 @@ mod tests { .unwrap(); insert_fixtures(&pool, info).await.unwrap(); - let result = get_one_fixtures(&pool, FixturesId(uuid)).await.unwrap(); - assert!(result.is_some()); + let result = get_one_fixtures(&pool, FixturesId(uuid)).await; + assert!(result.is_ok()); delete_fixtures(&pool, uuid).await.unwrap(); - let result = get_one_fixtures(&pool, FixturesId(uuid)).await.unwrap(); - assert!(result.is_none()); + let result = get_one_fixtures(&pool, FixturesId(uuid)).await; + assert!(result.is_err()); } } diff --git a/src/database/get_one_fixtures.rs b/src/database/get_one_fixtures.rs index faef8e1..657c693 100644 --- a/src/database/get_one_fixtures.rs +++ b/src/database/get_one_fixtures.rs @@ -11,7 +11,7 @@ pub enum IdType { QrId(String), } -pub async fn get_one_fixtures<'a, E>(conn: E, id: IdType) -> Result> +pub async fn get_one_fixtures<'a, E>(conn: E, id: IdType) -> Result where E: sqlx::Executor<'a, Database = sqlx::Postgres>, { @@ -22,8 +22,11 @@ where .fetch_optional(conn) .await .map_err(|_| QrError::DatabaseGet("fixtures".to_string()))?; - - Ok(fixtures_opt) + if let Some(fixtures) = fixtures_opt { + Ok(fixtures) + } else { + Err(QrError::DatabaseNotFound(id.to_string())) + } } IdType::QrId(id) => { let fixtures_opt = @@ -31,8 +34,11 @@ where .fetch_optional(conn) .await .map_err(|_| QrError::DatabaseGet("fixtures".to_string()))?; - - Ok(fixtures_opt) + if let Some(fixtures) = fixtures_opt { + Ok(fixtures) + } else { + Err(QrError::DatabaseNotFound(id.to_string())) + } } } } @@ -64,16 +70,12 @@ mod tests { .unwrap(); insert_fixtures(&pool, info).await.unwrap(); - let result: Option = get_one_fixtures(&pool, FixturesId(uuid)).await.unwrap(); - assert!(result.is_some()); - let result: Option = get_one_fixtures(&pool, QrId("test".to_string())) - .await - .unwrap(); - assert!(result.is_some()); + let result = get_one_fixtures(&pool, FixturesId(uuid)).await; + assert!(result.is_ok()); + let result = get_one_fixtures(&pool, QrId("test".to_string())).await; + assert!(result.is_ok()); - let result: Option = get_one_fixtures(&pool, FixturesId(dummy_uuid)) - .await - .unwrap(); - assert!(result.is_none()); + let result = get_one_fixtures(&pool, FixturesId(dummy_uuid)).await; + assert!(result.is_err()); } } diff --git a/src/database/get_one_lending.rs b/src/database/get_one_lending.rs index 6f73977..62be6e7 100644 --- a/src/database/get_one_lending.rs +++ b/src/database/get_one_lending.rs @@ -12,7 +12,7 @@ pub enum IdType { QrId(String), } -pub async fn get_one_lending<'a, E>(conn: E, id: IdType) -> Result> +pub async fn get_one_lending<'a, E>(conn: E, id: IdType) -> Result where E: sqlx::Executor<'a, Database = sqlx::Postgres>, { @@ -26,8 +26,11 @@ where .fetch_optional(conn) .await .map_err(|_| QrError::DatabaseGet("lending".to_string()))?; - - Ok(lending_opt) + if let Some(lending) = lending_opt { + Ok(lending) + } else { + Err(QrError::DatabaseNotFound(id.to_string())) + } } IdType::FixturesId(id) => { let lending_opt = sqlx::query_as!( @@ -38,8 +41,11 @@ where .fetch_optional(conn) .await .map_err(|_| QrError::DatabaseGet("lending".to_string()))?; - - Ok(lending_opt) + if let Some(lending) = lending_opt { + Ok(lending) + } else { + Err(QrError::DatabaseNotFound(id.to_string())) + } } IdType::QrId(id) => { let lending_opt = sqlx::query_as!( @@ -50,8 +56,11 @@ where .fetch_optional(conn) .await .map_err(|_| QrError::DatabaseGet("lending".to_string()))?; - - Ok(lending_opt) + if let Some(lending) = lending_opt { + Ok(lending) + } else { + Err(QrError::DatabaseNotFound(id.to_string())) + } } } } @@ -82,27 +91,19 @@ mod tests { insert_lending(&pool, info).await.unwrap(); - let result = get_one_lending(&pool, LendingId(id)).await.unwrap(); - assert!(result.is_some()); + let result = get_one_lending(&pool, LendingId(id)).await; + assert!(result.is_ok()); - let result = get_one_lending(&pool, FixturesId(fixtures_id)) - .await - .unwrap(); - assert!(result.is_some()); + let result = get_one_lending(&pool, FixturesId(fixtures_id)).await; + assert!(result.is_ok()); - let result = get_one_lending(&pool, QrId("x234".to_string())) - .await - .unwrap(); - assert!(result.is_some()); + let result = get_one_lending(&pool, QrId("x234".to_string())).await; + assert!(result.is_ok()); - let result = get_one_lending(&pool, FixturesId(dummy_fixtures_id)) - .await - .unwrap(); - assert!(result.is_none()); + let result = get_one_lending(&pool, FixturesId(dummy_fixtures_id)).await; + assert!(result.is_err()); - let result = get_one_lending(&pool, QrId("x235".to_string())) - .await - .unwrap(); - assert!(result.is_none()); + let result = get_one_lending(&pool, QrId("x235".to_string())).await; + assert!(result.is_err()); } } diff --git a/src/database/insert_lending.rs b/src/database/insert_lending.rs index 6f97013..0d9ce71 100644 --- a/src/database/insert_lending.rs +++ b/src/database/insert_lending.rs @@ -23,11 +23,11 @@ where // 物品IDとQR IDを元に二重貸し出しにならないかを確認する let is_lending1 = get_one_lending(conn.clone(), IdType::FixturesId(fixtures_id)) - .await? - .is_some(); + .await + .is_ok(); let is_lending2 = get_one_lending(conn.clone(), IdType::QrId(fixtures_qr_id.clone())) - .await? - .is_some(); + .await + .is_ok(); if !is_lending1 && !is_lending2 { sqlx::query!( diff --git a/src/database/returned_lending.rs b/src/database/returned_lending.rs index 58551f8..cbf0dfa 100644 --- a/src/database/returned_lending.rs +++ b/src/database/returned_lending.rs @@ -51,6 +51,6 @@ mod tests { assert!(res.is_ok()); let res = get_one_lending(&pool, IdType::FixturesId(fixtures_id)).await; - assert!(res.unwrap().is_none()); + assert!(res.is_err()); } } diff --git a/src/database/update_fixtures.rs b/src/database/update_fixtures.rs index 12fc69b..1a4c51e 100644 --- a/src/database/update_fixtures.rs +++ b/src/database/update_fixtures.rs @@ -102,7 +102,6 @@ mod tests { let result = get_one_fixtures(&pool, IdType::FixturesId(uuid)) .await - .unwrap() .unwrap(); assert_eq!(result.qr_id, "test2".to_string()) }