mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-15 09:11:28 +10:00
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>
This commit is contained in:
110
src-tauri/src/collections.rs
Normal file
110
src-tauri/src/collections.rs
Normal file
@ -0,0 +1,110 @@
|
||||
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(())
|
||||
}
|
||||
Reference in New Issue
Block a user