diff --git a/src-tauri/src/games/collections/commands.rs b/src-tauri/src/games/collections/commands.rs index 19d7fdf..dbef344 100644 --- a/src-tauri/src/games/collections/commands.rs +++ b/src-tauri/src/games/collections/commands.rs @@ -1,21 +1,81 @@ -use std::sync::Mutex; - use reqwest::blocking::Client; +use url::Url; -use crate::{error::remote_access_error::RemoteAccessError, games::{collections::collection::CollectionObject, library::Game}, remote::{auth::generate_authorization_header, requests::make_request}, AppState}; +use crate::{database::db::DatabaseImpls, error::remote_access_error::RemoteAccessError, remote::{auth::generate_authorization_header, requests::make_request}, DB}; use super::collection::{Collection, Collections}; #[tauri::command] pub fn fetch_collections() -> Result { - println!("Fetching collection"); let client = Client::new(); let response = make_request(&client, &["/api/v1/client/collection"], &[], |r| { r.header("Authorization", generate_authorization_header()) })? .send()?; - let res = response.json().unwrap(); + Ok(response.json()?) +} - return Ok(res) +#[tauri::command] +pub fn fetch_collection(id: String) -> Result { + let client = Client::new(); + let response = make_request(&client, &["/api/v1/client/collection/", &id], &[], |r| { + r.header("Authorization", generate_authorization_header()) + })? + .send()?; + + Ok(response.json()?) +} + +#[tauri::command] +pub fn create_collection(name: String) -> Result { + let client = Client::new(); + let base_url = DB.fetch_base_url(); + + let base_url = Url::parse(&format!("{}/api/v1/client/collection/", base_url))?; + + let response = client + .post(base_url) + .header("Authorization", generate_authorization_header()) + .json(&{name}) + .send()?; + Ok(response.json()?) +} + +#[tauri::command] +pub fn add_game_to_collection(name: String) -> Result<(), RemoteAccessError> { + let client = Client::new(); + let url = Url::parse(&format!("{}/api/v1/client/collection/{}/entry/", DB.fetch_base_url(), name))?; + + client + .post(url) + .header("Authorization", generate_authorization_header()) + .send()?; + Ok(()) +} + +#[tauri::command] +pub fn delete_collection(id: String) -> Result { + let client = Client::new(); + let base_url = Url::parse(&format!("{}/api/v1/client/collection/{}", DB.fetch_base_url(), id))?; + + let response = client + .delete(base_url) + .header("Authorization", generate_authorization_header()) + .send()?; + + Ok(response.json()?) +} +#[tauri::command] +pub fn delete_game_in_collection(id: String) -> Result<(), RemoteAccessError> { + let client = Client::new(); + let base_url = Url::parse(&format!("{}/api/v1/client/collection/{}/entry", DB.fetch_base_url(), id))?; + + client + .delete(base_url) + .header("Authorization", generate_authorization_header()) + .json(&{id}) + .send()?; + + Ok(()) } \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8e5edbf..5919ef6 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -25,7 +25,7 @@ use download_manager::commands::{ }; use download_manager::download_manager::DownloadManager; use download_manager::download_manager_builder::DownloadManagerBuilder; -use games::collections::commands::fetch_collections; +use games::collections::commands::{add_game_to_collection, create_collection, delete_collection, delete_game_in_collection, fetch_collection, fetch_collections}; use games::commands::{ fetch_game, fetch_game_status, fetch_game_verion_options, fetch_library, uninstall_game, }; @@ -251,6 +251,11 @@ pub fn run() { fetch_game_verion_options, // Collections fetch_collections, + fetch_collection, + create_collection, + add_game_to_collection, + delete_collection, + delete_game_in_collection, // Downloads download_game, move_download_in_queue,