Skip to content

Commit 7ce790e

Browse files
feat(user page): Recently published
1 parent b65bdf2 commit 7ce790e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

api/src/api/users.rs

+24
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ use crate::util::ApiResult;
1313
use crate::util::RequestIdExt;
1414

1515
use super::ApiError;
16+
use super::ApiPackage;
1617
use super::ApiScope;
1718
use super::ApiUser;
1819

1920
pub fn users_router() -> Router<Body, ApiError> {
2021
Router::builder()
2122
.get("/:id", util::json(get_handler))
2223
.get("/:id/scopes", util::json(get_scopes_handler))
24+
.get("/:id/packages", util::json(get_packages_handler))
2325
.build()
2426
.unwrap()
2527
}
@@ -54,3 +56,25 @@ pub async fn get_scopes_handler(
5456

5557
Ok(scopes.into_iter().map(ApiScope::from).collect())
5658
}
59+
60+
#[instrument(name = "GET /api/users/:id/packages", skip(req), err, fields(id))]
61+
pub async fn get_packages_handler(
62+
req: Request<Body>,
63+
) -> ApiResult<Vec<ApiPackage>> {
64+
let id = req.param_uuid("id")?;
65+
Span::current().record("id", field::display(id));
66+
67+
let db = req.data::<Database>().unwrap();
68+
db.get_user_public(id)
69+
.await?
70+
.ok_or(ApiError::UserNotFound)?;
71+
72+
let packages = db.get_recent_packages_by_user(&id).await?;
73+
74+
Ok(
75+
packages
76+
.into_iter()
77+
.map(|package| ApiPackage::from((package, None, Default::default())))
78+
.collect(),
79+
)
80+
}

api/src/db/database.rs

+33
Original file line numberDiff line numberDiff line change
@@ -4858,6 +4858,39 @@ impl Database {
48584858

48594859
Ok((total_scopes as usize, scopes))
48604860
}
4861+
4862+
pub async fn get_recent_packages_by_user(
4863+
&self,
4864+
user_id: &uuid::Uuid,
4865+
) -> Result<Vec<Package>> {
4866+
let packages = sqlx::query_as!(
4867+
Package,
4868+
r#"
4869+
SELECT DISTINCT ON (packages.scope, packages.name)
4870+
packages.scope as "scope: ScopeName",
4871+
packages.name as "name: PackageName",
4872+
packages.description,
4873+
packages.github_repository_id,
4874+
packages.runtime_compat as "runtime_compat: RuntimeCompat",
4875+
packages.when_featured,
4876+
packages.is_archived,
4877+
packages.updated_at,
4878+
packages.created_at,
4879+
(SELECT COUNT(created_at) FROM package_versions WHERE scope = packages.scope AND name = packages.name) as "version_count!",
4880+
(SELECT version FROM package_versions WHERE scope = packages.scope AND name = packages.name AND version NOT LIKE '%-%' AND is_yanked = false ORDER BY version DESC LIMIT 1) as "latest_version"
4881+
FROM packages
4882+
JOIN scope_members ON packages.scope = scope_members.scope
4883+
WHERE scope_members.user_id = $1
4884+
ORDER BY packages.scope, packages.name, packages.created_at DESC
4885+
LIMIT 10;
4886+
"#,
4887+
user_id
4888+
)
4889+
.fetch_all(&self.pool)
4890+
.await?;
4891+
4892+
Ok(packages)
4893+
}
48614894
}
48624895

48634896
async fn finalize_package_creation(

0 commit comments

Comments
 (0)