From 19f4780a511ff1a186db39d2d339fe2f41e4ec84 Mon Sep 17 00:00:00 2001 From: Igor Bubelov Date: Fri, 16 Aug 2024 17:00:22 +0700 Subject: [PATCH] Refactor area model --- src/area/model.rs | 18 +++++------------- src/area/v2.rs | 36 +++++++++++++++++------------------- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/area/model.rs b/src/area/model.rs index 5c7006d..868d89f 100644 --- a/src/area/model.rs +++ b/src/area/model.rs @@ -26,14 +26,6 @@ impl AreaRepo { Self { pool: pool.clone() } } - pub async fn select_all(&self, limit: Option) -> Result> { - self.pool - .get() - .await? - .interact(move |conn| Area::select_all(limit, conn)) - .await? - } - pub async fn select_updated_since( &self, updated_since: &OffsetDateTime, @@ -433,14 +425,14 @@ mod test { #[test] async fn select_all() -> Result<()> { - let state = mock_state().await; + let conn = mock_conn(); assert_eq!( vec![ - Area::insert(&Map::new(), &state.conn)?, - Area::insert(&Map::new(), &state.conn)?, - Area::insert(&Map::new(), &state.conn)?, + Area::insert(&Map::new(), &conn)?, + Area::insert(&Map::new(), &conn)?, + Area::insert(&Map::new(), &conn)?, ], - state.area_repo.select_all(None).await?, + Area::select_all(None, &conn)?, ); Ok(()) } diff --git a/src/area/v2.rs b/src/area/v2.rs index 18d0d74..c6af0f6 100644 --- a/src/area/v2.rs +++ b/src/area/v2.rs @@ -8,10 +8,12 @@ use actix_web::web::Path; use actix_web::web::Query; use actix_web::web::Redirect; use actix_web::Either; +use deadpool_sqlite::Pool; use serde::Deserialize; use serde::Serialize; use serde_json::Map; use serde_json::Value; +use std::sync::Arc; use time::format_description::well_known::Rfc3339; use time::OffsetDateTime; @@ -59,28 +61,24 @@ impl Into> for Area { #[get("")] pub async fn get( args: Query, - repo: Data, + pool: Data>, ) -> Result>, Redirect>, Error> { if args.limit.is_none() && args.updated_since.is_none() { return Ok(Either::Right( Redirect::to("https://static.btcmap.org/api/v2/areas.json").permanent(), )); } - - Ok(Either::Left(Json(match &args.updated_since { - Some(updated_since) => repo - .select_updated_since(updated_since, args.limit) - .await? - .into_iter() - .map(|it| it.into()) - .collect(), - None => repo - .select_all(args.limit) - .await? - .into_iter() - .map(|it| it.into()) - .collect(), - }))) + let areas = pool + .get() + .await? + .interact(move |conn| match &args.updated_since { + Some(updated_since) => Area::select_updated_since(updated_since, args.limit, conn), + None => Area::select_all(args.limit, conn), + }) + .await??; + Ok(Either::Left(Json( + areas.into_iter().map(|it| it.into()).collect(), + ))) } #[get("{url_alias}")] @@ -110,7 +108,7 @@ mod tests { let state = mock_state().await; let app = test::init_service( App::new() - .app_data(Data::new(AreaRepo::new(&state.pool))) + .app_data(Data::new(state.pool)) .service(scope("/").service(super::get)), ) .await; @@ -128,7 +126,7 @@ mod tests { Area::insert(&tags, &state.conn)?; let app = test::init_service( App::new() - .app_data(Data::new(AreaRepo::new(&state.pool))) + .app_data(Data::new(state.pool)) .service(scope("/").service(super::get)), ) .await; @@ -148,7 +146,7 @@ mod tests { Area::insert(&tags, &state.conn)?; let app = test::init_service( App::new() - .app_data(Data::new(AreaRepo::new(&state.pool))) + .app_data(Data::new(state.pool)) .service(scope("/").service(super::get)), ) .await;