1
0
Fork 0
forked from 2sys/phonograph
phonograph/interim-models/src/workspace.rs

134 lines
3.2 KiB
Rust
Raw Normal View History

2025-08-04 13:59:42 -07:00
use derive_builder::Builder;
use redact::Secret;
2025-08-04 13:59:42 -07:00
use sqlx::query_as;
use url::Url;
2025-08-04 13:59:42 -07:00
use uuid::Uuid;
use crate::client::AppDbClient;
/// A workspace is 1:1 with a Postgres "database".
2025-08-04 13:59:42 -07:00
#[derive(Clone, Debug)]
pub struct Workspace {
/// Primary key (defaults to UUIDv7).
2025-08-04 13:59:42 -07:00
pub id: Uuid,
/// Human friendly name for the workspace.
2025-08-04 13:59:42 -07:00
pub name: String,
/// `postgresql://` URL of the instance and database hosting this workspace.
// TODO: Encrypt values in Postgres using `pgp_sym_encrypt()`.
pub url: Secret<String>,
/// ID of the user account that created this workspace.
2025-08-04 13:59:42 -07:00
pub owner_id: Uuid,
}
impl Workspace {
/// Build an insert statement to create a new workspace.
pub fn insert() -> InsertableWorkspaceBuilder {
InsertableWorkspaceBuilder::default()
2025-08-04 13:59:42 -07:00
}
/// Build a single-field query by workspace ID.
2025-08-04 13:59:42 -07:00
pub fn with_id(id: Uuid) -> WithIdQuery {
WithIdQuery { id }
}
/// Build a query for workspaces filtered by a user's Phono permissions.
2025-08-04 13:59:42 -07:00
pub fn with_permission_in<I: IntoIterator<Item = &'static str>>(
perms: I,
) -> WithPermissionInQueryPartial {
let perms: Vec<String> = perms.into_iter().map(ToOwned::to_owned).collect();
WithPermissionInQueryPartial { perms }
}
}
pub struct WithPermissionInQueryPartial {
perms: Vec<String>,
}
impl WithPermissionInQueryPartial {
pub fn for_user(self, user_id: Uuid) -> WithPermissionInQuery {
WithPermissionInQuery {
perms: self.perms,
user_id,
}
}
}
pub struct WithPermissionInQuery {
perms: Vec<String>,
user_id: Uuid,
}
impl WithPermissionInQuery {
pub async fn fetch_all(self, app_db: &mut AppDbClient) -> Result<Vec<Workspace>, sqlx::Error> {
2025-08-04 13:59:42 -07:00
query_as!(
Workspace,
2025-08-04 13:59:42 -07:00
"
select workspaces.*
from workspaces inner join workspace_user_perms as p
on p.workspace_id = workspaces.id
2025-08-04 13:59:42 -07:00
where p.user_id = $1 and perm = ANY($2)
",
self.user_id,
self.perms.as_slice(),
)
.fetch_all(&mut *app_db.conn)
.await
}
}
pub struct WithIdQuery {
id: Uuid,
}
impl WithIdQuery {
pub async fn fetch_optional(
self,
app_db: &mut AppDbClient,
) -> Result<Option<Workspace>, sqlx::Error> {
query_as!(
Workspace,
"select * from workspaces where id = $1",
&self.id
)
.fetch_optional(&mut *app_db.conn)
.await
2025-08-04 13:59:42 -07:00
}
pub async fn fetch_one(self, app_db: &mut AppDbClient) -> Result<Workspace, sqlx::Error> {
query_as!(
Workspace,
"select * from workspaces where id = $1",
&self.id
)
.fetch_one(&mut *app_db.conn)
.await
2025-08-04 13:59:42 -07:00
}
}
#[derive(Builder)]
pub struct InsertableWorkspace {
url: Url,
2025-08-04 13:59:42 -07:00
owner_id: Uuid,
}
impl InsertableWorkspace {
pub async fn insert(self, app_db: &mut AppDbClient) -> Result<Workspace, sqlx::Error> {
2025-08-04 13:59:42 -07:00
query_as!(
Workspace,
2025-08-04 13:59:42 -07:00
"
insert into workspaces
(url, owner_id)
values ($1, $2)
2025-08-04 13:59:42 -07:00
returning *
",
self.url.to_string(),
2025-08-04 13:59:42 -07:00
self.owner_id
)
.fetch_one(&mut *app_db.conn)
.await
}
}