Skip to content

Commit

Permalink
修正完了
Browse files Browse the repository at this point in the history
  • Loading branch information
puripuri2100 committed Oct 26, 2023
1 parent 861392d commit 4eb76eb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 117 deletions.
10 changes: 2 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ pub async fn app(bind: SocketAddr) -> Result<()> {
get({
info!("GET /get_spot");
let conn = Arc::clone(&conn);
move |query: Query<HashMap<String, String>>| {
let name = query.0.get("name").cloned();
spot::get_one_spot(name, conn)
}
move |Query(query)| spot::get_one_spot(query, conn)
}),
)
.route(
Expand All @@ -200,10 +197,7 @@ pub async fn app(bind: SocketAddr) -> Result<()> {
delete({
info!("DELETE /delete_spot");
let conn = Arc::clone(&conn);
move |query: Query<HashMap<String, String>>| {
let name = query.0.get("name").cloned();
spot::delte_spot(name, conn)
}
move |Query(query)| spot::delte_spot(query, conn)
}),
)
.route(
Expand Down
2 changes: 1 addition & 1 deletion src/app/lending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn get_lending_list(conn: Arc<Pool<Postgres>>) -> ReturnData<Vec<Lendi
info!("Try get lending list");
let res = crate::database::get_lending_list::get_lending_list(&*conn).await;
result_to_handler_with_log(
|_| Some(format!("Success get lending list")),
|_| Some("Success get lending list".to_string()),
|e| Some(e.to_string()),
&res,
)
Expand Down
156 changes: 59 additions & 97 deletions src/app/spot.rs
Original file line number Diff line number Diff line change
@@ -1,129 +1,91 @@
use crate::Spot;
use axum::{extract::Json, http::StatusCode};
use crate::{
error_handling::{result_to_handler_with_log, QrError, ReturnData},
Spot,
};
use axum::extract::Json;
use sqlx::{pool::Pool, postgres::Postgres};
use std::collections::HashMap;
use std::sync::Arc;
use tracing::*;

/// 地点情報の登録を行うエンドポイント
pub async fn insert_spot(Json(spot): Json<Spot>, conn: Arc<Pool<Postgres>>) -> StatusCode {
pub async fn insert_spot(Json(spot): Json<Spot>, conn: Arc<Pool<Postgres>>) -> 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<Spot>, conn: Arc<Pool<Postgres>>) -> StatusCode {
pub async fn update_spot(Json(spot): Json<Spot>, conn: Arc<Pool<Postgres>>) -> 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<String>, conn: Arc<Pool<Postgres>>) -> Json<Option<Spot>> {
match name {
pub async fn get_one_spot(
query: HashMap<String, String>,
conn: Arc<Pool<Postgres>>,
) -> ReturnData<Spot> {
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<Pool<Postgres>>) -> Json<Option<Vec<Spot>>> {
pub async fn get_spot_list(conn: Arc<Pool<Postgres>>) -> ReturnData<Vec<Spot>> {
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<String>, conn: Arc<Pool<Postgres>>) -> StatusCode {
match name {
pub async fn delte_spot(
query: HashMap<String, String>,
conn: Arc<Pool<Postgres>>,
) -> 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<Postgres>) {
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)
}
}
8 changes: 4 additions & 4 deletions src/database/delete_spot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
17 changes: 10 additions & 7 deletions src/database/get_one_spot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ use crate::{
Spot,
};

pub async fn get_one_spot<'a, E>(conn: E, name: &str) -> Result<Option<Spot>>
pub async fn get_one_spot<'a, E>(conn: E, name: &str) -> Result<Spot>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let spot_opt = sqlx::query_as!(Spot, "SELECT * FROM spot WHERE name = $1", name)
.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)]
Expand All @@ -33,10 +36,10 @@ mod tests {
.unwrap();

insert_spot(&pool, info).await.unwrap();
let result: Option<Spot> = 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<Spot> = get_one_spot(&pool, "test2").await.unwrap();
assert!(result.is_none());
let result = get_one_spot(&pool, "test2").await;
assert!(result.is_err());
}
}

0 comments on commit 4eb76eb

Please sign in to comment.