diff --git a/crates/manager-core/src/app_repo.rs b/crates/manager-core/src/app_repo.rs index c2523a2..21dcbf0 100644 --- a/crates/manager-core/src/app_repo.rs +++ b/crates/manager-core/src/app_repo.rs @@ -8,6 +8,7 @@ use async_trait::async_trait; use picloud_shared::{AdminUserId, App, AppId}; use sqlx::PgPool; +use uuid::Uuid; use crate::repo::ScriptRepositoryError; @@ -20,6 +21,32 @@ pub struct AppLookup { pub redirected: bool, } +/// Resolve a free-form path param (UUID *or* slug *or* historical slug) +/// to an `AppLookup`. UUID lookups never set `redirected`; slug lookups +/// fall through to `app_slug_history` and set `redirected: true` when +/// they hit it. +/// +/// Returns `Ok(None)` when nothing matches — callers map that to their +/// own not-found error variant. +/// +/// # Errors +/// Propagates any underlying repository error. +pub async fn resolve_app( + apps: &dyn AppRepository, + ident: &str, +) -> Result, ScriptRepositoryError> { + if let Ok(uuid) = ident.parse::() { + return Ok(apps + .get_by_id(AppId::from(uuid)) + .await? + .map(|app| AppLookup { + app, + redirected: false, + })); + } + apps.get_by_slug_or_history(ident).await +} + #[async_trait] pub trait AppRepository: Send + Sync { /// Every app on the instance. For owner/admin callers — `member` diff --git a/crates/manager-core/src/apps_api.rs b/crates/manager-core/src/apps_api.rs index b63c633..9b7849d 100644 --- a/crates/manager-core/src/apps_api.rs +++ b/crates/manager-core/src/apps_api.rs @@ -454,16 +454,7 @@ async fn resolve_app( apps: &dyn AppRepository, ident: &str, ) -> Result { - if let Ok(uuid) = ident.parse::() { - if let Some(app) = apps.get_by_id(AppId::from(uuid)).await? { - return Ok(crate::app_repo::AppLookup { - app, - redirected: false, - }); - } - return Err(AppsApiError::AppNotFound(ident.to_string())); - } - apps.get_by_slug_or_history(ident) + crate::app_repo::resolve_app(apps, ident) .await? .ok_or_else(|| AppsApiError::AppNotFound(ident.to_string())) } diff --git a/crates/manager-core/src/lib.rs b/crates/manager-core/src/lib.rs index 1d090e7..b2c249f 100644 --- a/crates/manager-core/src/lib.rs +++ b/crates/manager-core/src/lib.rs @@ -51,7 +51,7 @@ pub use app_members_repo::{ AppMembersRepository, AppMembersRepositoryError, AppMembershipDetail, AppMembershipRow, PostgresAppMembersRepository, }; -pub use app_repo::{AppLookup, AppRepository, PostgresAppRepository}; +pub use app_repo::{resolve_app, AppLookup, AppRepository, PostgresAppRepository}; pub use apps_api::{apps_router, AppsState}; pub use auth_api::auth_router; pub use auth_bootstrap::{