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::types::files::{
FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest,
TagQuery, ThumbnailMetadataResponse,
SortKey, TagQuery, ThumbnailMetadataResponse,
};
use crate::types::identifier::FileIdentifier;
use async_trait::async_trait;
@ -39,9 +39,19 @@ impl FileApi {
/// Searches for a file by a list of tags
#[tracing::instrument(level = "debug", skip(self))]
pub async fn find_files(&self, tags: Vec<TagQuery>) -> ApiResult<Vec<FileMetadataResponse>> {
self.emit_and_get("find_files", FindFilesByTagsRequest { tags })
.await
pub async fn find_files(
&self,
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

@ -1,6 +1,6 @@
use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess};
use crate::tauri_plugin::error::PluginResult;
use crate::types::files::{FileMetadataResponse, TagQuery, ThumbnailMetadataResponse};
use crate::types::files::{FileMetadataResponse, SortKey, TagQuery, ThumbnailMetadataResponse};
#[tauri::command]
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]
pub async fn find_files(
tags: Vec<TagQuery>,
sort_by: Vec<SortKey>,
api_state: ApiAccess<'_>,
) -> PluginResult<Vec<FileMetadataResponse>> {
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)
}

@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize};
use chrono::NaiveDateTime;
use crate::types::identifier::FileIdentifier;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AddFileRequest {
@ -25,6 +25,7 @@ pub struct GetFileTagsRequest {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FindFilesByTagsRequest {
pub tags: Vec<TagQuery>,
pub sort_expression: Vec<SortKey>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
@ -33,6 +34,32 @@ pub struct TagQuery {
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)]
pub struct FileMetadataResponse {
pub id: i64,
@ -53,4 +80,4 @@ pub struct ThumbnailMetadataResponse {
pub height: i32,
pub width: i32,
pub mime_type: Option<String>,
}
}

Loading…
Cancel
Save