Add sorting to file search

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 2ad64f61da
commit 4d4f143ec5

@ -2,7 +2,7 @@ use crate::client_api::error::ApiResult;
use crate::client_api::IPCApi; use crate::client_api::IPCApi;
use crate::types::files::{ use crate::types::files::{
FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest, FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest,
TagQuery, ThumbnailMetadataResponse, SortKey, TagQuery, ThumbnailMetadataResponse,
}; };
use crate::types::identifier::FileIdentifier; use crate::types::identifier::FileIdentifier;
use async_trait::async_trait; use async_trait::async_trait;
@ -39,9 +39,19 @@ impl FileApi {
/// Searches for a file by a list of tags /// Searches for a file by a list of tags
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn find_files(&self, tags: Vec<TagQuery>) -> ApiResult<Vec<FileMetadataResponse>> { pub async fn find_files(
self.emit_and_get("find_files", FindFilesByTagsRequest { tags }) &self,
.await tags: Vec<TagQuery>,
sort_expression: Vec<SortKey>,
) -> ApiResult<Vec<FileMetadataResponse>> {
self.emit_and_get(
"find_files",
FindFilesByTagsRequest {
tags,
sort_expression,
},
)
.await
} }
/// Reads the file and returns its contents as bytes /// Reads the file and returns its contents as bytes

@ -1,6 +1,6 @@
use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess}; use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess};
use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::error::PluginResult;
use crate::types::files::{FileMetadataResponse, TagQuery, ThumbnailMetadataResponse}; use crate::types::files::{FileMetadataResponse, SortKey, TagQuery, ThumbnailMetadataResponse};
#[tauri::command] #[tauri::command]
pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult<Vec<FileMetadataResponse>> { pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult<Vec<FileMetadataResponse>> {
@ -13,10 +13,11 @@ pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult<Vec<FileMet
#[tauri::command] #[tauri::command]
pub async fn find_files( pub async fn find_files(
tags: Vec<TagQuery>, tags: Vec<TagQuery>,
sort_by: Vec<SortKey>,
api_state: ApiAccess<'_>, api_state: ApiAccess<'_>,
) -> PluginResult<Vec<FileMetadataResponse>> { ) -> PluginResult<Vec<FileMetadataResponse>> {
let api = api_state.api().await?; let api = api_state.api().await?;
let files = api.file.find_files(tags).await?; let files = api.file.find_files(tags, sort_by).await?;
Ok(files) Ok(files)
} }

@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize};
use chrono::NaiveDateTime;
use crate::types::identifier::FileIdentifier; use crate::types::identifier::FileIdentifier;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AddFileRequest { pub struct AddFileRequest {
@ -25,6 +25,7 @@ pub struct GetFileTagsRequest {
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FindFilesByTagsRequest { pub struct FindFilesByTagsRequest {
pub tags: Vec<TagQuery>, pub tags: Vec<TagQuery>,
pub sort_expression: Vec<SortKey>,
} }
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
@ -33,6 +34,32 @@ pub struct TagQuery {
pub name: String, pub name: String,
} }
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SortKey {
Namespace(SortNamespace),
FileName(SortDirection),
FileSize(SortDirection),
FileImportedTime(SortDirection),
FileCreatedTime(SortDirection),
FileChangeTime(SortDirection),
FileType(SortDirection),
NumTags(SortDirection),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SortNamespace {
pub tag: String,
pub direction: SortDirection,
}
#[derive(Clone, Debug, Serialize, Deserialize, Ord, PartialOrd, PartialEq)]
pub enum SortDirection {
Ascending,
Descending,
}
impl Eq for SortDirection {}
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FileMetadataResponse { pub struct FileMetadataResponse {
pub id: i64, pub id: i64,

Loading…
Cancel
Save