Skip to content

models/krate: Add async_owners() fn #9901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/controllers/krate/owners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,24 @@ use tokio::runtime::Handle;

/// Handles the `GET /crates/:crate_id/owners` route.
pub async fn owners(state: AppState, Path(crate_name): Path<String>) -> AppResult<Json<Value>> {
let conn = state.db_read().await?;
spawn_blocking(move || {
use diesel::RunQueryDsl;
use diesel_async::RunQueryDsl;

let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into();
let mut conn = state.db_read().await?;

let krate: Crate = Crate::by_name(&crate_name)
.first(conn)
.optional()?
.ok_or_else(|| crate_not_found(&crate_name))?;
let krate: Crate = Crate::by_name(&crate_name)
.first(&mut conn)
.await
.optional()?
.ok_or_else(|| crate_not_found(&crate_name))?;

let owners = krate
.owners(conn)?
.into_iter()
.map(Owner::into)
.collect::<Vec<EncodableOwner>>();
let owners = krate
.async_owners(&mut conn)
.await?
.into_iter()
.map(Owner::into)
.collect::<Vec<EncodableOwner>>();

Ok(Json(json!({ "users": owners })))
})
.await
Ok(Json(json!({ "users": owners })))
}

/// Handles the `GET /crates/:crate_id/owner_team` route.
Expand Down
24 changes: 24 additions & 0 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,30 @@ impl Crate {
))
}

pub async fn async_owners(&self, conn: &mut AsyncPgConnection) -> QueryResult<Vec<Owner>> {
use diesel_async::RunQueryDsl;

let users = CrateOwner::by_owner_kind(OwnerKind::User)
.filter(crate_owners::crate_id.eq(self.id))
.inner_join(users::table)
.select(User::as_select())
.load(conn)
.await?
.into_iter()
.map(Owner::User);

let teams = CrateOwner::by_owner_kind(OwnerKind::Team)
.filter(crate_owners::crate_id.eq(self.id))
.inner_join(teams::table)
.select(Team::as_select())
.load(conn)
.await?
.into_iter()
.map(Owner::Team);

Ok(users.chain(teams).collect())
}

pub fn owners(&self, conn: &mut impl Conn) -> QueryResult<Vec<Owner>> {
use diesel::RunQueryDsl;

Expand Down