From 66030e8cca49c61705e3f64bdf4170c94824b292 Mon Sep 17 00:00:00 2001 From: AcrylicShrimp Date: Sun, 31 Mar 2024 03:21:22 +0900 Subject: [PATCH] feat: add collection file endpoints --- src/routes/collection/controllers.rs | 70 +++++++++++++++++++++++++++- src/routes/collection/dto.rs | 24 +++++++++- 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/src/routes/collection/controllers.rs b/src/routes/collection/controllers.rs index d738a8c..1741360 100644 --- a/src/routes/collection/controllers.rs +++ b/src/routes/collection/controllers.rs @@ -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}, @@ -29,6 +30,8 @@ pub fn register_routes(rocket: Rocket) -> Rocket { update_collection, add_file_to_collection, remove_file_from_collection, + search_files_in_collection, + get_files_in_collection, ], ) } @@ -247,3 +250,66 @@ async fn remove_file_from_collection( Ok((Status::Ok, Json(pair))) } + +#[post("//files/search", data = "")] +async fn search_files_in_collection( + #[allow(unused_variables)] sess: AuthUserSession<'_>, + search_service: &State>, + collection_id: Uuid, + body: Json>, +) -> JsonRes { + 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("//files?&")] +async fn get_files_in_collection( + #[allow(unused_variables)] sess: AuthUserSession<'_>, + collection_file_pair_service: &State>, + collection_id: Uuid, + last_file_id: Option, + limit: Option, +) -> JsonRes { + 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, + }), + )) +} diff --git a/src/routes/collection/dto.rs b/src/routes/collection/dto.rs index 0c361cc..a2c5ed4 100644 --- a/src/routes/collection/dto.rs +++ b/src/routes/collection/dto.rs @@ -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; @@ -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, + pub filter_uploaded_at: Option<(NaiveDateTime, NaiveDateTime)>, +} + +#[derive(Serialize, Deserialize)] +pub struct CollectionFileSearchResult { + pub files: Vec, +} + +#[derive(Serialize, Deserialize)] +pub struct CollectionFileList { + pub files: Vec, + pub last_file_id: Option, + pub limit: u32, +}