get_structure endpoint
This commit is contained in:
parent
32d207a991
commit
f6ed06de48
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "SELECT folder_id, owner_id, folder_name, created_at FROM folders WHERE parent_folder_id = $1",
|
"query": "SELECT folder_id, owner_id, folder_name, created_at FROM folders WHERE folder_id = $1",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@ -36,5 +36,5 @@
|
|||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "9cc887509746b773ebbc8c130331b768f9a1deeab34d56aa3b0a833d718114fe"
|
"hash": "3028a7c8ec616933e490ed267967b1406552c2b7c69f4f1f02a147df5411e692"
|
||||||
}
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT\n f.folder_id,\n owner_id,\n folder_name,\n created_at\nFROM\n folders f\n JOIN permissions p ON f.folder_id = p.folder_id\nWHERE\n parent_folder_id = $1\n AND p.user_id = $2",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "folder_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "owner_id",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "folder_name",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "created_at",
|
||||||
|
"type_info": "Timestamp"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Int4"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "b11a87b3b9f6289e831b1f0cb0e8f35283687a9b13d050ac15f16e2a8cec046f"
|
||||||
|
}
|
13
Cargo.lock
generated
13
Cargo.lock
generated
@ -321,9 +321,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytes"
|
name = "bytes"
|
||||||
version = "1.7.0"
|
version = "1.7.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fca2be1d5c43812bae364ee3f30b3afcb7877cf59f4aeb94c66f313a41d2fac9"
|
checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
@ -1042,9 +1042,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "2.2.6"
|
version = "2.3.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
|
checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
@ -1962,9 +1962,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.121"
|
version = "1.0.122"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4ab380d7d9f22ef3f21ad3e6c1ebe8e4fc7a2000ccba2e4d71fc96f15b2cb609"
|
checksum = "784b6203951c57ff748476b126ccb5e8e2959a5c19e5c617ab1956be3dbc68da"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"memchr",
|
"memchr",
|
||||||
@ -2478,6 +2478,7 @@ dependencies = [
|
|||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
|
@ -41,7 +41,7 @@ sqlx = { version = "0.8", features = [
|
|||||||
"chrono",
|
"chrono",
|
||||||
"uuid",
|
"uuid",
|
||||||
] }
|
] }
|
||||||
tokio = { version = "1", features = ["rt-multi-thread"] }
|
tokio = { version = "1", features = ["parking_lot", "rt-multi-thread"] }
|
||||||
tokio-util = { version = "0.7" }
|
tokio-util = { version = "0.7" }
|
||||||
tower = { version = "0.4" }
|
tower = { version = "0.4" }
|
||||||
tower-http = { version = "0.5", features = [
|
tower-http = { version = "0.5", features = [
|
||||||
|
11
sql/get_folders.sql
Normal file
11
sql/get_folders.sql
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
SELECT
|
||||||
|
f.folder_id,
|
||||||
|
owner_id,
|
||||||
|
folder_name,
|
||||||
|
created_at
|
||||||
|
FROM
|
||||||
|
folders f
|
||||||
|
JOIN permissions p ON f.folder_id = p.folder_id
|
||||||
|
WHERE
|
||||||
|
parent_folder_id = $1
|
||||||
|
AND p.user_id = $2
|
@ -5,21 +5,16 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::AppState;
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct Claims {
|
pub struct Claims {
|
||||||
pub user_id: i32,
|
pub user_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[axum::async_trait]
|
#[axum::async_trait]
|
||||||
impl FromRequestParts<AppState> for Claims {
|
impl<T> FromRequestParts<T> for Claims {
|
||||||
type Rejection = StatusCode;
|
type Rejection = StatusCode;
|
||||||
|
|
||||||
async fn from_request_parts(
|
async fn from_request_parts(parts: &mut Parts, _state: &T) -> Result<Self, Self::Rejection> {
|
||||||
parts: &mut Parts,
|
|
||||||
_state: &AppState,
|
|
||||||
) -> Result<Self, Self::Rejection> {
|
|
||||||
match parts.extract().await {
|
match parts.extract().await {
|
||||||
Ok(Query(claims)) => Ok(claims),
|
Ok(Query(claims)) => Ok(claims),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -33,12 +33,12 @@ pub async fn update(file_id: Uuid, size: i64, hash: Vec<u8>, pool: &Pool) -> sql
|
|||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
#[allow(clippy::struct_field_names, clippy::module_name_repetitions)]
|
#[allow(clippy::struct_field_names, clippy::module_name_repetitions)]
|
||||||
pub struct FileWithoutParentId {
|
pub struct FileWithoutParentId {
|
||||||
file_id: Uuid,
|
pub file_id: Uuid,
|
||||||
file_name: String,
|
pub file_name: String,
|
||||||
file_size: i64,
|
pub file_size: i64,
|
||||||
sha512: String,
|
pub sha512: String,
|
||||||
created_at: chrono::NaiveDateTime,
|
pub created_at: chrono::NaiveDateTime,
|
||||||
updated_at: chrono::NaiveDateTime,
|
pub updated_at: chrono::NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_files(folder_id: Uuid, pool: &Pool) -> sqlx::Result<Vec<FileWithoutParentId>> {
|
pub async fn get_files(folder_id: Uuid, pool: &Pool) -> sqlx::Result<Vec<FileWithoutParentId>> {
|
||||||
|
@ -33,7 +33,7 @@ pub async fn get_root(user_id: i32, pool: &Pool) -> sqlx::Result<Uuid> {
|
|||||||
.map(|row| row.folder_id)
|
.map(|row| row.folder_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_by_id(id: Option<Uuid>, user_id: i32, pool: &Pool) -> sqlx::Result<Option<Uuid>> {
|
pub async fn process_id(id: Option<Uuid>, user_id: i32, pool: &Pool) -> sqlx::Result<Option<Uuid>> {
|
||||||
match id {
|
match id {
|
||||||
Some(id) => get_permissions(id, user_id, pool)
|
Some(id) => get_permissions(id, user_id, pool)
|
||||||
.await
|
.await
|
||||||
@ -45,25 +45,44 @@ pub async fn get_by_id(id: Option<Uuid>, user_id: i32, pool: &Pool) -> sqlx::Res
|
|||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
#[allow(clippy::struct_field_names, clippy::module_name_repetitions)]
|
#[allow(clippy::struct_field_names, clippy::module_name_repetitions)]
|
||||||
pub struct FolderWithoutParentId {
|
pub struct FolderWithoutParentId {
|
||||||
folder_id: Uuid,
|
pub folder_id: Uuid,
|
||||||
owner_id: i32,
|
pub owner_id: i32,
|
||||||
folder_name: String,
|
pub folder_name: String,
|
||||||
created_at: chrono::NaiveDateTime,
|
pub created_at: chrono::NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_folders(
|
pub async fn get_by_id(
|
||||||
parent_folder_id: Uuid,
|
folder_id: Uuid,
|
||||||
pool: &Pool,
|
pool: &Pool,
|
||||||
) -> sqlx::Result<Vec<FolderWithoutParentId>> {
|
) -> sqlx::Result<Option<FolderWithoutParentId>> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
FolderWithoutParentId,
|
FolderWithoutParentId,
|
||||||
"SELECT folder_id, owner_id, folder_name, created_at FROM folders WHERE parent_folder_id = $1",
|
"SELECT folder_id, owner_id, folder_name, created_at FROM folders WHERE folder_id = $1",
|
||||||
parent_folder_id,
|
folder_id
|
||||||
)
|
)
|
||||||
.fetch_all(pool)
|
.fetch_optional(pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get folders that user can read
|
||||||
|
///
|
||||||
|
/// # Warning
|
||||||
|
///
|
||||||
|
/// This function doesn't check that the user can read the parent folder itself
|
||||||
|
pub fn get_folders(
|
||||||
|
parent_folder_id: Uuid,
|
||||||
|
user_id: i32,
|
||||||
|
pool: &Pool,
|
||||||
|
) -> impl Stream<Item = sqlx::Result<FolderWithoutParentId>> + '_ {
|
||||||
|
sqlx::query_file_as!(
|
||||||
|
FolderWithoutParentId,
|
||||||
|
"sql/get_folders.sql",
|
||||||
|
parent_folder_id,
|
||||||
|
user_id
|
||||||
|
)
|
||||||
|
.fetch(pool)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn name_exists(parent_folder_id: Uuid, name: &str, pool: &Pool) -> sqlx::Result<bool> {
|
pub async fn name_exists(parent_folder_id: Uuid, name: &str, pool: &Pool) -> sqlx::Result<bool> {
|
||||||
sqlx::query_file!("sql/name_exists.sql", parent_folder_id, name)
|
sqlx::query_file!("sql/name_exists.sql", parent_folder_id, name)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
|
62
src/endpoints/folder/get_structure.rs
Normal file
62
src/endpoints/folder/get_structure.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
use futures::TryStreamExt;
|
||||||
|
use tokio::try_join;
|
||||||
|
|
||||||
|
use super::list::Params;
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Serialize, Debug)]
|
||||||
|
pub struct FolderStructure {
|
||||||
|
#[serde(flatten)]
|
||||||
|
folder_base: db::folder::FolderWithoutParentId,
|
||||||
|
folders: Vec<FolderStructure>,
|
||||||
|
files: Vec<db::file::FileWithoutParentId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<db::folder::FolderWithoutParentId> for FolderStructure {
|
||||||
|
fn from(value: db::folder::FolderWithoutParentId) -> Self {
|
||||||
|
FolderStructure {
|
||||||
|
folder_base: value,
|
||||||
|
folders: Vec::new(),
|
||||||
|
files: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
folder_id: Uuid,
|
||||||
|
structure: FolderStructure,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn structure(
|
||||||
|
Query(params): Query<Params>,
|
||||||
|
State(pool): State<Pool>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<Response>, StatusCode> {
|
||||||
|
let folder_id = db::folder::process_id(params.folder_id, claims.user_id, &pool)
|
||||||
|
.await
|
||||||
|
.handle_internal()?
|
||||||
|
.ok_or(StatusCode::NOT_FOUND)?;
|
||||||
|
let folder = db::folder::get_by_id(folder_id, &pool)
|
||||||
|
.await
|
||||||
|
.handle_internal()?
|
||||||
|
.ok_or(StatusCode::NOT_FOUND)?;
|
||||||
|
let mut response = Response {
|
||||||
|
folder_id,
|
||||||
|
structure: folder.into(),
|
||||||
|
};
|
||||||
|
let mut stack: Vec<&mut FolderStructure> = vec![&mut response.structure];
|
||||||
|
while let Some(folder) = stack.pop() {
|
||||||
|
let (files, folders) = try_join!(
|
||||||
|
db::file::get_files(folder_id, &pool),
|
||||||
|
db::folder::get_folders(folder_id, claims.user_id, &pool)
|
||||||
|
.map_ok(Into::into)
|
||||||
|
.try_collect()
|
||||||
|
)
|
||||||
|
.handle_internal()?;
|
||||||
|
folder.folders = folders;
|
||||||
|
folder.files = files;
|
||||||
|
stack.extend(folder.folders.iter_mut());
|
||||||
|
}
|
||||||
|
Ok(Json(response))
|
||||||
|
}
|
@ -1,10 +1,11 @@
|
|||||||
|
use futures::TryStreamExt;
|
||||||
use tokio::try_join;
|
use tokio::try_join;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
folder_id: Option<Uuid>,
|
pub(super) folder_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -19,14 +20,14 @@ pub async fn list(
|
|||||||
State(pool): State<Pool>,
|
State(pool): State<Pool>,
|
||||||
claims: Claims,
|
claims: Claims,
|
||||||
) -> Result<Json<Response>, StatusCode> {
|
) -> Result<Json<Response>, StatusCode> {
|
||||||
let folder_id = db::folder::get_by_id(params.folder_id, claims.user_id, &pool)
|
let folder_id = db::folder::process_id(params.folder_id, claims.user_id, &pool)
|
||||||
.await
|
.await
|
||||||
.handle_internal()?
|
.handle_internal()?
|
||||||
.ok_or(StatusCode::NOT_FOUND)?;
|
.ok_or(StatusCode::NOT_FOUND)?;
|
||||||
|
|
||||||
let (files, folders) = try_join!(
|
let (files, folders) = try_join!(
|
||||||
db::file::get_files(folder_id, &pool),
|
db::file::get_files(folder_id, &pool),
|
||||||
db::folder::get_folders(folder_id, &pool)
|
db::folder::get_folders(folder_id, claims.user_id, &pool).try_collect()
|
||||||
)
|
)
|
||||||
.handle_internal()?;
|
.handle_internal()?;
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
pub mod create;
|
pub mod create;
|
||||||
pub mod delete;
|
pub mod delete;
|
||||||
|
pub mod get_structure;
|
||||||
pub mod list;
|
pub mod list;
|
||||||
|
@ -13,18 +13,12 @@ use tokio::net::TcpListener;
|
|||||||
|
|
||||||
type Pool = sqlx::postgres::PgPool;
|
type Pool = sqlx::postgres::PgPool;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, FromRef)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
pool: Pool,
|
pool: Pool,
|
||||||
storage: FileStorage,
|
storage: FileStorage,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRef<AppState> for Pool {
|
|
||||||
fn from_ref(input: &AppState) -> Self {
|
|
||||||
input.pool.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn create_test_users(pool: &Pool) -> anyhow::Result<()> {
|
async fn create_test_users(pool: &Pool) -> anyhow::Result<()> {
|
||||||
let count = sqlx::query!("SELECT count(user_id) FROM users")
|
let count = sqlx::query!("SELECT count(user_id) FROM users")
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
@ -104,6 +98,7 @@ fn app(state: AppState) -> Router {
|
|||||||
.post(folder::create::create)
|
.post(folder::create::create)
|
||||||
.delete(folder::delete::delete),
|
.delete(folder::delete::delete),
|
||||||
)
|
)
|
||||||
|
.route("/folders/structure", get(folder::get_structure::structure))
|
||||||
.route(
|
.route(
|
||||||
"/permissions",
|
"/permissions",
|
||||||
get(permissions::get::get)
|
get(permissions::get::get)
|
||||||
|
Reference in New Issue
Block a user