diff --git a/src-tauri/src/games/collections/commands.rs b/src-tauri/src/games/collections/commands.rs index 0a72ad9..9c202c9 100644 --- a/src-tauri/src/games/collections/commands.rs +++ b/src-tauri/src/games/collections/commands.rs @@ -1,4 +1,5 @@ use reqwest::blocking::Client; +use serde_json::json; use url::Url; use crate::{database::db::DatabaseImpls, error::remote_access_error::RemoteAccessError, remote::{auth::generate_authorization_header, requests::make_request}, DB}; @@ -17,9 +18,9 @@ pub fn fetch_collections() -> Result { } #[tauri::command] -pub fn fetch_collection(id: String) -> Result { +pub fn fetch_collection(collection_id: String) -> Result { let client = Client::new(); - let response = make_request(&client, &["/api/v1/client/collection/", &id], &[], |r| { + let response = make_request(&client, &["/api/v1/client/collection/", &collection_id], &[], |r| { r.header("Authorization", generate_authorization_header()) })? .send()?; @@ -28,42 +29,39 @@ pub fn fetch_collection(id: String) -> Result { } #[tauri::command] -pub fn create_collection(name: String) -> Result<(), RemoteAccessError> { +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}); - - println!("{:?}", response); + .json(&json!({"name": name})) + .send()?; - - println!("{}", response.send()?.text().unwrap()); - - - Ok(()) + Ok(response.json()?) } #[tauri::command] -pub fn add_game_to_collection(name: String) -> Result<(), RemoteAccessError> { +pub fn add_game_to_collection(collection_id: String, game_id: String) -> Result<(), RemoteAccessError> { let client = Client::new(); - let url = Url::parse(&format!("{}api/v1/client/collection/{}/entry/", DB.fetch_base_url(), name))?; + let url = Url::parse(&format!("{}api/v1/client/collection/{}/entry/", DB.fetch_base_url(), collection_id))?; - let response = client + client .post(url) .header("Authorization", generate_authorization_header()) + .json(&json!({"id": game_id})) .send()?; Ok(()) } #[tauri::command] -pub fn delete_collection(id: String) -> Result { +pub fn delete_collection(collection_id: String) -> Result { let client = Client::new(); - let base_url = Url::parse(&format!("{}api/v1/client/collection/{}", DB.fetch_base_url(), id))?; + let base_url = Url::parse(&format!("{}api/v1/client/collection/{}", DB.fetch_base_url(), collection_id))?; let response = client .delete(base_url) @@ -73,14 +71,14 @@ pub fn delete_collection(id: String) -> Result { Ok(response.json()?) } #[tauri::command] -pub fn delete_game_in_collection(id: String) -> Result<(), RemoteAccessError> { +pub fn delete_game_in_collection(collection_id: String, game_id: String) -> Result<(), RemoteAccessError> { let client = Client::new(); - let base_url = Url::parse(&format!("{}api/v1/client/collection/{}/entry", DB.fetch_base_url(), id))?; + let base_url = Url::parse(&format!("{}api/v1/client/collection/{}/entry", DB.fetch_base_url(), collection_id))?; client .delete(base_url) .header("Authorization", generate_authorization_header()) - .json(&{id}) + .json(&json!({"id": game_id})) .send()?; Ok(())