Skip to content

Commit

Permalink
Improve async interop
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Nov 19, 2024
1 parent 58c9d97 commit 9ed6524
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
27 changes: 20 additions & 7 deletions src/area/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,18 @@ impl Area {
}

pub async fn select_updated_since_async(
updated_since: &OffsetDateTime,
updated_since: OffsetDateTime,
limit: Option<i64>,
pool: &Pool,
) -> Result<Vec<Area>> {
let updated_since = updated_since.clone();
pool.get()
.await?
.interact(move |conn| Area::select_updated_since(&updated_since, limit, conn))
.interact(move |conn| Area::select_updated_since(updated_since, limit, conn))
.await?
}

pub fn select_updated_since(
updated_since: &OffsetDateTime,
updated_since: OffsetDateTime,
limit: Option<i64>,
conn: &Connection,
) -> Result<Vec<Area>> {
Expand All @@ -126,7 +125,21 @@ impl Area {
.map_err(Into::into)
}

pub fn select_by_search_query(search_query: &str, conn: &Connection) -> Result<Vec<Area>> {
pub async fn select_by_search_query_async(
search_query: impl Into<String>,
pool: &Pool,
) -> Result<Vec<Area>> {
let search_query = search_query.into();
pool.get()
.await?
.interact(move |conn| Area::select_by_search_query(&search_query, conn))
.await?
}

pub fn select_by_search_query(
search_query: impl Into<String>,
conn: &Connection,
) -> Result<Vec<Area>> {
let sql = format!(
r#"
SELECT {MAPPER_PROJECTION}
Expand All @@ -136,7 +149,7 @@ impl Area {
"#
);
conn.prepare(&sql)?
.query_map(named_params! { ":query": search_query }, mapper())?
.query_map(named_params! { ":query": search_query.into() }, mapper())?
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
}
Expand Down Expand Up @@ -404,7 +417,7 @@ mod test {
Area::set_updated_at(area_3.id, &datetime!(2020-01-03 00:00 UTC), &conn)?.unwrap();
assert_eq!(
vec![area_2, area_3],
Area::select_updated_since(&datetime!(2020-01-01 00:00 UTC), None, &conn)?,
Area::select_updated_since(datetime!(2020-01-01 00:00 UTC), None, &conn)?,
);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/area/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn get(
));
}
let areas = Area::select_updated_since_async(
&args.updated_since.unwrap_or(datetime!(2000-01-01 0:00 UTC)),
args.updated_since.unwrap_or(datetime!(2000-01-01 0:00 UTC)),
args.limit,
&pool,
)
Expand Down
2 changes: 1 addition & 1 deletion src/area/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub async fn get(
pool: Data<Pool>,
) -> Result<Json<Vec<GetItem>>, Error> {
let areas =
Area::select_updated_since_async(&args.updated_since, Some(args.limit), &pool).await?;
Area::select_updated_since_async(args.updated_since, Some(args.limit), &pool).await?;
req.extensions_mut()
.insert(RequestExtension::new(areas.len()));
Ok(Json(areas.into_iter().map(|it| it.into()).collect()))
Expand Down
6 changes: 1 addition & 5 deletions src/rpc/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ pub struct Res {

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Vec<Res>> {
admin::service::check_rpc(args.password, NAME, &pool).await?;
let areas = pool
.get()
.await?
.interact(move |conn| Area::select_by_search_query(&args.query, conn))
.await??;
let areas = Area::select_by_search_query_async(args.query, &pool).await?;
let res = areas
.into_iter()
.map(|it| Res {
Expand Down

0 comments on commit 9ed6524

Please sign in to comment.