fix(collections): Ensured that all internal collection commands use and send the correct data

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2025-03-11 20:35:43 +11:00
parent 19c8fc24aa
commit 1ce6be80db

View File

@ -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<Collections, RemoteAccessError> {
}
#[tauri::command]
pub fn fetch_collection(id: String) -> Result<Collection, RemoteAccessError> {
pub fn fetch_collection(collection_id: String) -> Result<Collection, RemoteAccessError> {
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<Collection, RemoteAccessError> {
}
#[tauri::command]
pub fn create_collection(name: String) -> Result<(), RemoteAccessError> {
pub fn create_collection(name: String) -> Result<Collection, RemoteAccessError> {
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});
.json(&json!({"name": name}))
.send()?;
println!("{:?}", response);
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<bool, RemoteAccessError> {
pub fn delete_collection(collection_id: String) -> Result<bool, RemoteAccessError> {
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<bool, RemoteAccessError> {
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(())