- Added all seven issued service trees without omissions. - Preserved the supplied runtime configuration, binaries, and assets.
170 lines
4.9 KiB
Rust
Executable File
170 lines
4.9 KiB
Rust
Executable File
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
use crate::{
|
|
error::{ApiError, ApiResult},
|
|
models::File,
|
|
};
|
|
|
|
pub struct FileRepository;
|
|
|
|
impl FileRepository {
|
|
pub async fn create(
|
|
pool: &PgPool,
|
|
name: &str,
|
|
filename: &str,
|
|
owner_id: Uuid,
|
|
workspace_id: Option<Uuid>,
|
|
size: i64,
|
|
is_public: bool,
|
|
) -> ApiResult<File> {
|
|
sqlx::query_as::<_, File>(
|
|
r#"
|
|
WITH inserted AS (
|
|
INSERT INTO files (name, filename, owner_id, workspace_id, size, is_public)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
RETURNING id, name, filename, owner_id, workspace_id, size, created_at, is_public
|
|
)
|
|
SELECT inserted.id, inserted.name, inserted.filename, inserted.owner_id,
|
|
inserted.workspace_id, inserted.size, inserted.created_at,
|
|
inserted.is_public, users.username AS owner
|
|
FROM inserted
|
|
JOIN users ON users.id = inserted.owner_id
|
|
"#,
|
|
)
|
|
.bind(name)
|
|
.bind(filename)
|
|
.bind(owner_id)
|
|
.bind(workspace_id)
|
|
.bind(size)
|
|
.bind(is_public)
|
|
.fetch_one(pool)
|
|
.await
|
|
.map_err(|err| {
|
|
println!("[file-upload] database insert failed: {err:?}");
|
|
ApiError::Database
|
|
})
|
|
}
|
|
|
|
pub async fn list_for_workspace(
|
|
pool: &PgPool,
|
|
workspace_id: Uuid,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> ApiResult<Vec<File>> {
|
|
sqlx::query_as::<_, File>(
|
|
r#"
|
|
SELECT f.id, f.name, f.filename, f.owner_id, f.workspace_id, f.size,
|
|
f.created_at, f.is_public, u.username AS owner
|
|
FROM files f
|
|
JOIN users u ON u.id = f.owner_id
|
|
WHERE f.workspace_id = $1
|
|
ORDER BY f.created_at DESC
|
|
LIMIT $2 OFFSET $3
|
|
"#,
|
|
)
|
|
.bind(workspace_id)
|
|
.bind(limit)
|
|
.bind(offset)
|
|
.fetch_all(pool)
|
|
.await
|
|
.map_err(|_| ApiError::Database)
|
|
}
|
|
|
|
pub async fn list_personal(
|
|
pool: &PgPool,
|
|
owner_id: Uuid,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> ApiResult<Vec<File>> {
|
|
sqlx::query_as::<_, File>(
|
|
r#"
|
|
SELECT f.id, f.name, f.filename, f.owner_id, f.workspace_id, f.size,
|
|
f.created_at, f.is_public, u.username AS owner
|
|
FROM files f
|
|
JOIN users u ON u.id = f.owner_id
|
|
WHERE f.workspace_id IS NULL AND f.owner_id = $1
|
|
ORDER BY f.created_at DESC
|
|
LIMIT $2 OFFSET $3
|
|
"#,
|
|
)
|
|
.bind(owner_id)
|
|
.bind(limit)
|
|
.bind(offset)
|
|
.fetch_all(pool)
|
|
.await
|
|
.map_err(|_| ApiError::Database)
|
|
}
|
|
|
|
pub async fn list_public(pool: &PgPool, limit: i64, offset: i64) -> ApiResult<Vec<File>> {
|
|
sqlx::query_as::<_, File>(
|
|
r#"
|
|
SELECT f.id, f.name, f.filename, f.owner_id, f.workspace_id, f.size,
|
|
f.created_at, f.is_public, u.username AS owner
|
|
FROM files f
|
|
JOIN users u ON u.id = f.owner_id
|
|
WHERE f.is_public = TRUE
|
|
ORDER BY f.created_at DESC
|
|
LIMIT $1 OFFSET $2
|
|
"#,
|
|
)
|
|
.bind(limit)
|
|
.bind(offset)
|
|
.fetch_all(pool)
|
|
.await
|
|
.map_err(|_| ApiError::Database)
|
|
}
|
|
|
|
pub async fn find_by_workspace_and_name(
|
|
pool: &PgPool,
|
|
workspace_id: Uuid,
|
|
name: &str,
|
|
limit: i64,
|
|
) -> ApiResult<Vec<File>> {
|
|
sqlx::query_as::<_, File>(
|
|
r#"
|
|
SELECT f.id, f.name, f.filename, f.owner_id, f.workspace_id, f.size,
|
|
f.created_at, f.is_public, u.username AS owner
|
|
FROM files f
|
|
JOIN users u ON u.id = f.owner_id
|
|
WHERE f.workspace_id = $1 AND f.name = $2
|
|
ORDER BY f.created_at DESC
|
|
LIMIT $3
|
|
"#,
|
|
)
|
|
.bind(workspace_id)
|
|
.bind(name)
|
|
.bind(limit)
|
|
.fetch_all(pool)
|
|
.await
|
|
.map_err(|_| ApiError::Database)
|
|
}
|
|
|
|
pub async fn get(pool: &PgPool, file_id: Uuid) -> ApiResult<File> {
|
|
sqlx::query_as::<_, File>(
|
|
r#"
|
|
SELECT f.id, f.name, f.filename, f.owner_id, f.workspace_id, f.size,
|
|
f.created_at, f.is_public, u.username AS owner
|
|
FROM files f
|
|
JOIN users u ON u.id = f.owner_id
|
|
WHERE f.id = $1
|
|
"#,
|
|
)
|
|
.bind(file_id)
|
|
.fetch_optional(pool)
|
|
.await
|
|
.map_err(|_| ApiError::Database)?
|
|
.ok_or(ApiError::NotFound)
|
|
}
|
|
|
|
pub async fn delete(pool: &PgPool, file_id: Uuid) -> ApiResult<()> {
|
|
sqlx::query("DELETE FROM files WHERE id = $1")
|
|
.bind(file_id)
|
|
.execute(pool)
|
|
.await
|
|
.map_err(|_| ApiError::Database)?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|