Skip to content

Commit

Permalink
feat: add collection file endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AcrylicShrimp committed Mar 30, 2024
1 parent 31cf9bf commit 66030e8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
70 changes: 68 additions & 2 deletions src/routes/collection/controllers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::dto::{
AddingCollectionFile, CollectionList, CollectionSearchResult, CreatingCollection,
SearchingCollection, UpdatingCollection,
AddingCollectionFile, CollectionFileList, CollectionFileSearchResult, CollectionList,
CollectionSearchResult, CreatingCollection, SearchingCollection, SearchingCollectionFile,
UpdatingCollection,
};
use crate::{
db::models::{Collection, CollectionFilePair},
Expand Down Expand Up @@ -29,6 +30,8 @@ pub fn register_routes(rocket: Rocket<Build>) -> Rocket<Build> {
update_collection,
add_file_to_collection,
remove_file_from_collection,
search_files_in_collection,
get_files_in_collection,
],
)
}
Expand Down Expand Up @@ -247,3 +250,66 @@ async fn remove_file_from_collection(

Ok((Status::Ok, Json(pair)))
}

#[post("/<collection_id>/files/search", data = "<body>")]
async fn search_files_in_collection(
#[allow(unused_variables)] sess: AuthUserSession<'_>,
search_service: &State<Arc<SearchService>>,
collection_id: Uuid,
body: Json<SearchingCollectionFile<'_>>,
) -> JsonRes<CollectionFileSearchResult> {
let files = search_service
.search_collection_files(
collection_id,
body.query,
body.filter_mime,
body.filter_size,
body.filter_hash,
body.filter_uploaded_at,
)
.await;

let files = match files {
Ok(files) => files,
Err(err) => {
let body = body.into_inner();
log::error!(target: "routes::file::controllers", controller = "search_files_in_collection", service = "SearchService", body:serde, err:err; "Error returned from service.");
return Err(Status::InternalServerError.into());
}
};

Ok((Status::Ok, Json(CollectionFileSearchResult { files })))
}

#[get("/<collection_id>/files?<last_file_id>&<limit>")]
async fn get_files_in_collection(
#[allow(unused_variables)] sess: AuthUserSession<'_>,
collection_file_pair_service: &State<Arc<CollectionFilePairService>>,
collection_id: Uuid,
last_file_id: Option<Uuid>,
limit: Option<u32>,
) -> JsonRes<CollectionFileList> {
let limit = limit.unwrap_or(25);
let limit = u32::max(1, limit);
let limit = u32::min(limit, 100);
let files = collection_file_pair_service
.get_files_in_collection(collection_id, last_file_id, limit)
.await;

let files = match files {
Ok(files) => files,
Err(err) => {
log::error!(target: "routes::collection::controllers", controller = "get_files_in_collection", service = "CollectionFilePairService", collection_id:serde, last_file_id:serde, limit, err:err; "Error returned from service.");
return Err(Status::InternalServerError.into());
}
};

Ok((
Status::Ok,
Json(CollectionFileList {
files,
last_file_id,
limit,
}),
))
}
24 changes: 23 additions & 1 deletion src/routes/collection/dto.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::db::models::Collection;
use crate::db::models::{Collection, File};
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

Expand Down Expand Up @@ -35,3 +36,24 @@ pub struct CollectionList {
pub struct AddingCollectionFile {
pub file_id: Uuid,
}

#[derive(Serialize, Deserialize)]
pub struct SearchingCollectionFile<'a> {
pub query: &'a str,
pub filter_mime: Option<&'a str>,
pub filter_size: Option<(u32, u32)>,
pub filter_hash: Option<u32>,
pub filter_uploaded_at: Option<(NaiveDateTime, NaiveDateTime)>,
}

#[derive(Serialize, Deserialize)]
pub struct CollectionFileSearchResult {
pub files: Vec<File>,
}

#[derive(Serialize, Deserialize)]
pub struct CollectionFileList {
pub files: Vec<File>,
pub last_file_id: Option<Uuid>,
pub limit: u32,
}

0 comments on commit 66030e8

Please sign in to comment.