Files
drop-app/src-tauri/src/collections.rs
quexeky 59f040bc8b chore: Major refactoring
Still needs a massive go-over because there shouldn't be anything referencing tauri in any of the workspaces except the original one. Process manager has been refactored as an example

Signed-off-by: quexeky <git@quexeky.dev>
2025-10-11 09:28:41 +11:00

111 lines
2.9 KiB
Rust

use serde_json::json;
use crate::{
error::remote_access_error::RemoteAccessError,
remote::{
auth::generate_authorization_header,
cache::{cache_object, get_cached_object},
requests::{generate_url, make_authenticated_get},
utils::DROP_CLIENT_ASYNC,
},
};
use super::collection::{Collection, Collections};
#[tauri::command]
pub async fn fetch_collections(
hard_refresh: Option<bool>,
) -> Result<Collections, RemoteAccessError> {
let do_hard_refresh = hard_refresh.unwrap_or(false);
if !do_hard_refresh && let Ok(cached_response) = get_cached_object::<Collections>("collections")
{
return Ok(cached_response);
}
let response =
make_authenticated_get(generate_url(&["/api/v1/client/collection"], &[])?).await?;
let collections: Collections = response.json().await?;
cache_object("collections", &collections)?;
Ok(collections)
}
#[tauri::command]
pub async fn fetch_collection(collection_id: String) -> Result<Collection, RemoteAccessError> {
let response = make_authenticated_get(generate_url(
&["/api/v1/client/collection/", &collection_id],
&[],
)?)
.await?;
Ok(response.json().await?)
}
#[tauri::command]
pub async fn create_collection(name: String) -> Result<Collection, RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(&["/api/v1/client/collection"], &[])?;
let response = client
.post(url)
.header("Authorization", generate_authorization_header())
.json(&json!({"name": name}))
.send()
.await?;
Ok(response.json().await?)
}
#[tauri::command]
pub async fn add_game_to_collection(
collection_id: String,
game_id: String,
) -> Result<(), RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(&["/api/v1/client/collection", &collection_id, "entry"], &[])?;
client
.post(url)
.header("Authorization", generate_authorization_header())
.json(&json!({"id": game_id}))
.send()
.await?;
Ok(())
}
#[tauri::command]
pub async fn delete_collection(collection_id: String) -> Result<bool, RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(&["/api/v1/client/collection", &collection_id], &[])?;
let response = client
.delete(url)
.header("Authorization", generate_authorization_header())
.send()
.await?;
Ok(response.json().await?)
}
#[tauri::command]
pub async fn delete_game_in_collection(
collection_id: String,
game_id: String,
) -> Result<(), RemoteAccessError> {
let client = DROP_CLIENT_ASYNC.clone();
let url = generate_url(&["/api/v1/client/collection", &collection_id, "entry"], &[])?;
client
.delete(url)
.header("Authorization", generate_authorization_header())
.json(&json!({"id": game_id}))
.send()
.await?;
Ok(())
}