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 21, 2024
1 parent 57e5ed6 commit e80d220
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
19 changes: 15 additions & 4 deletions src/area/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,34 @@ impl Area {
r#"
SELECT {MAPPER_PROJECTION}
FROM {TABLE_NAME}
WHERE {COL_ID} = :{COL_ID}
WHERE {COL_ID} = :{COL_ID};
"#
);
conn.query_row(&sql, named_params! { ":id": id }, mapper())
.optional()
.map_err(Into::into)
}

pub fn select_by_alias(alias: &str, conn: &Connection) -> Result<Option<Area>> {
pub async fn select_by_alias_async(
alias: impl Into<String>,
pool: &Pool,
) -> Result<Option<Area>> {
let alias = alias.into();
pool.get()
.await?
.interact(|conn| Area::select_by_alias(alias, conn))
.await?
}

pub fn select_by_alias(alias: impl Into<String>, conn: &Connection) -> Result<Option<Area>> {
let sql = format!(
r#"
SELECT {MAPPER_PROJECTION}
FROM {TABLE_NAME}
WHERE {COL_ALIAS} = :alias
WHERE {COL_ALIAS} = :{COL_ALIAS};
"#
);
conn.query_row(&sql, named_params! { ":alias": alias }, mapper())
conn.query_row(&sql, named_params! { ":alias": alias.into() }, mapper())
.optional()
.map_err(Into::into)
}
Expand Down
7 changes: 1 addition & 6 deletions src/area/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ pub async fn get_by_url_alias(
url_alias: Path<String>,
pool: Data<Pool>,
) -> Result<Json<GetItem>, Error> {
let cloned_url_alias = url_alias.clone();
let area = pool
.get()
.await?
.interact(move |conn| Area::select_by_alias(&cloned_url_alias, conn))
.await??;
let area = Area::select_by_alias_async(url_alias.to_string(), &pool).await?;
area.ok_or(Error::NotFound(format!(
"Area with url_alias = {url_alias} doesn't exist"
)))
Expand Down

0 comments on commit e80d220

Please sign in to comment.