mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 07:24:07 +01:00
Add folder model
This commit is contained in:
@@ -492,6 +492,69 @@ async fn delete_request(
|
||||
emit_and_return(&window, "deleted_model", req)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn list_folders(
|
||||
workspace_id: &str,
|
||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||
) -> Result<Vec<models::Folder>, String> {
|
||||
let pool = &*db_instance.lock().await;
|
||||
models::find_folders(workspace_id, pool)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn create_folder(
|
||||
workspace_id: &str,
|
||||
name: &str,
|
||||
sort_priority: f64,
|
||||
folder_id: Option<&str>,
|
||||
window: Window<Wry>,
|
||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||
) -> Result<models::Folder, String> {
|
||||
let pool = &*db_instance.lock().await;
|
||||
let created_request = models::upsert_folder(
|
||||
pool,
|
||||
models::Folder {
|
||||
workspace_id: workspace_id.to_string(),
|
||||
name: name.to_string(),
|
||||
folder_id: folder_id.map(|s| s.to_string()),
|
||||
sort_priority,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect("Failed to create folder");
|
||||
|
||||
emit_and_return(&window, "created_model", created_request)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn update_folder(
|
||||
folder: models::Folder,
|
||||
window: Window<Wry>,
|
||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||
) -> Result<models::Folder, String> {
|
||||
let pool = &*db_instance.lock().await;
|
||||
let updated_folder = models::upsert_folder(pool, folder)
|
||||
.await
|
||||
.expect("Failed to update request");
|
||||
emit_and_return(&window, "updated_model", updated_folder)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn delete_folder(
|
||||
window: Window<Wry>,
|
||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||
folder_id: &str,
|
||||
) -> Result<models::Folder, String> {
|
||||
let pool = &*db_instance.lock().await;
|
||||
let req = models::delete_folder(folder_id, pool)
|
||||
.await
|
||||
.expect("Failed to delete folder");
|
||||
emit_and_return(&window, "deleted_model", req)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn delete_environment(
|
||||
window: Window<Wry>,
|
||||
@@ -529,6 +592,17 @@ async fn list_environments(
|
||||
Ok(environments)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn get_folder(
|
||||
id: &str,
|
||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||
) -> Result<models::Folder, String> {
|
||||
let pool = &*db_instance.lock().await;
|
||||
models::get_folder(id, pool)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn get_request(
|
||||
id: &str,
|
||||
@@ -705,20 +779,24 @@ fn main() {
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
create_environment,
|
||||
create_folder,
|
||||
create_request,
|
||||
create_workspace,
|
||||
delete_all_responses,
|
||||
delete_environment,
|
||||
delete_folder,
|
||||
delete_request,
|
||||
delete_response,
|
||||
delete_workspace,
|
||||
duplicate_request,
|
||||
get_key_value,
|
||||
get_environment,
|
||||
get_folder,
|
||||
get_request,
|
||||
get_workspace,
|
||||
import_data,
|
||||
list_environments,
|
||||
list_folders,
|
||||
list_requests,
|
||||
list_responses,
|
||||
list_workspaces,
|
||||
@@ -727,6 +805,7 @@ fn main() {
|
||||
send_request,
|
||||
set_key_value,
|
||||
update_environment,
|
||||
update_folder,
|
||||
update_request,
|
||||
update_workspace,
|
||||
])
|
||||
|
||||
@@ -64,6 +64,7 @@ pub struct HttpRequest {
|
||||
pub updated_at: NaiveDateTime,
|
||||
pub id: String,
|
||||
pub workspace_id: String,
|
||||
pub folder_id: Option<String>,
|
||||
pub model: String,
|
||||
pub sort_priority: f64,
|
||||
pub name: String,
|
||||
@@ -77,6 +78,19 @@ pub struct HttpRequest {
|
||||
pub headers: Json<Vec<HttpRequestHeader>>,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct Folder {
|
||||
pub created_at: NaiveDateTime,
|
||||
pub updated_at: NaiveDateTime,
|
||||
pub id: String,
|
||||
pub workspace_id: String,
|
||||
pub folder_id: Option<String>,
|
||||
pub model: String,
|
||||
pub name: String,
|
||||
pub sort_priority: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct HttpResponseHeader {
|
||||
@@ -292,6 +306,103 @@ pub async fn get_environment(id: &str, pool: &Pool<Sqlite>) -> Result<Environmen
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_folder(id: &str, pool: &Pool<Sqlite>) -> Result<Folder, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Folder,
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
model,
|
||||
workspace_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
folder_id,
|
||||
name,
|
||||
sort_priority
|
||||
FROM folders
|
||||
WHERE id = ?
|
||||
"#,
|
||||
id,
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_folders(
|
||||
workspace_id: &str,
|
||||
pool: &Pool<Sqlite>,
|
||||
) -> Result<Vec<Folder>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Folder,
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
model,
|
||||
workspace_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
folder_id,
|
||||
name,
|
||||
sort_priority
|
||||
FROM folders
|
||||
WHERE workspace_id = ?
|
||||
"#,
|
||||
workspace_id,
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_folder(id: &str, pool: &Pool<Sqlite>) -> Result<Folder, sqlx::Error> {
|
||||
let env = get_folder(id, pool).await?;
|
||||
let _ = sqlx::query!(
|
||||
r#"
|
||||
DELETE FROM folders
|
||||
WHERE id = ?
|
||||
"#,
|
||||
id,
|
||||
)
|
||||
.execute(pool)
|
||||
.await;
|
||||
|
||||
Ok(env)
|
||||
}
|
||||
|
||||
pub async fn upsert_folder(pool: &Pool<Sqlite>, r: Folder) -> Result<Folder, sqlx::Error> {
|
||||
let id = match r.id.as_str() {
|
||||
"" => generate_id(Some("fl")),
|
||||
_ => r.id.to_string(),
|
||||
};
|
||||
let trimmed_name = r.name.trim();
|
||||
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO folders (
|
||||
id,
|
||||
workspace_id,
|
||||
folder_id,
|
||||
name,
|
||||
sort_priority
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
updated_at = CURRENT_TIMESTAMP,
|
||||
name = excluded.name,
|
||||
folder_id = excluded.folder_id,
|
||||
sort_priority = excluded.sort_priority
|
||||
"#,
|
||||
id,
|
||||
r.workspace_id,
|
||||
r.folder_id,
|
||||
trimmed_name,
|
||||
r.sort_priority,
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
get_folder(&id, pool).await
|
||||
}
|
||||
|
||||
pub async fn duplicate_request(id: &str, pool: &Pool<Sqlite>) -> Result<HttpRequest, sqlx::Error> {
|
||||
let mut request = get_request(id, pool).await?.clone();
|
||||
request.id = "".to_string();
|
||||
@@ -315,6 +426,7 @@ pub async fn upsert_request(
|
||||
INSERT INTO http_requests (
|
||||
id,
|
||||
workspace_id,
|
||||
folder_id,
|
||||
name,
|
||||
url,
|
||||
method,
|
||||
@@ -325,10 +437,11 @@ pub async fn upsert_request(
|
||||
headers,
|
||||
sort_priority
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
updated_at = CURRENT_TIMESTAMP,
|
||||
name = excluded.name,
|
||||
folder_id = excluded.folder_id,
|
||||
method = excluded.method,
|
||||
headers = excluded.headers,
|
||||
body = excluded.body,
|
||||
@@ -340,6 +453,7 @@ pub async fn upsert_request(
|
||||
"#,
|
||||
id,
|
||||
r.workspace_id,
|
||||
r.folder_id,
|
||||
trimmed_name,
|
||||
r.url,
|
||||
r.method,
|
||||
@@ -367,6 +481,7 @@ pub async fn find_requests(
|
||||
id,
|
||||
model,
|
||||
workspace_id,
|
||||
folder_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
name,
|
||||
@@ -395,6 +510,7 @@ pub async fn get_request(id: &str, pool: &Pool<Sqlite>) -> Result<HttpRequest, s
|
||||
id,
|
||||
model,
|
||||
workspace_id,
|
||||
folder_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
name,
|
||||
|
||||
Reference in New Issue
Block a user