Skip to content

Commit

Permalink
Refactor area model
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Aug 16, 2024
1 parent 18d3762 commit 19f4780
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
18 changes: 5 additions & 13 deletions src/area/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ impl AreaRepo {
Self { pool: pool.clone() }
}

pub async fn select_all(&self, limit: Option<i64>) -> Result<Vec<Area>> {
self.pool
.get()
.await?
.interact(move |conn| Area::select_all(limit, conn))
.await?
}

pub async fn select_updated_since(
&self,
updated_since: &OffsetDateTime,
Expand Down Expand Up @@ -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(())
}
Expand Down
36 changes: 17 additions & 19 deletions src/area/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -59,28 +61,24 @@ impl Into<Json<GetItem>> for Area {
#[get("")]
pub async fn get(
args: Query<GetArgs>,
repo: Data<AreaRepo>,
pool: Data<Arc<Pool>>,
) -> Result<Either<Json<Vec<GetItem>>, 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}")]
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 19f4780

Please sign in to comment.