From a282edc100e573433efff4911acffa45b3627984 Mon Sep 17 00:00:00 2001 From: zhaojisen <1301338853@qq.com> Date: Thu, 16 Jul 2026 13:27:46 +0800 Subject: [PATCH] fix: restore fac --- src-tauri/src/api/endpoint.rs | 4 -- src-tauri/src/service/asset.rs | 99 +++------------------------------- 2 files changed, 6 insertions(+), 97 deletions(-) diff --git a/src-tauri/src/api/endpoint.rs b/src-tauri/src/api/endpoint.rs index 9de543ea..0f6413e3 100644 --- a/src-tauri/src/api/endpoint.rs +++ b/src-tauri/src/api/endpoint.rs @@ -4,10 +4,6 @@ pub(crate) mod assets { pub(crate) const FAVORITE_ASSETS: &str = "/api/v1/assets/favorite-assets/"; pub(crate) const FAVORITE_NODE_ASSETS: &str = "/api/v1/perms/users/self/nodes/favorite/assets/"; - pub(crate) fn favorite_detail(favorite_id: &str) -> String { - format!("/api/v1/assets/favorite-assets/{}/", favorite_id) - } - pub(crate) fn detail(asset_id: &str) -> String { format!("/api/v1/perms/users/self/assets/{}", asset_id) } diff --git a/src-tauri/src/service/asset.rs b/src-tauri/src/service/asset.rs index 9337bfc7..74f7548d 100644 --- a/src-tauri/src/service/asset.rs +++ b/src-tauri/src/service/asset.rs @@ -1,10 +1,11 @@ use crate::api::endpoint; use crate::api::request::{ApiRequestClient, ApiResponse}; -use log::{info, warn}; +use log::info; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use std::cmp::Ordering; use std::collections::HashSet; +use url::Url; #[derive(Debug, Serialize, Deserialize, Clone, Copy, Default)] #[serde(rename_all = "lowercase")] @@ -99,44 +100,6 @@ struct FavoriteAssetBody { asset: String, } -#[derive(Serialize)] -struct FavoriteAssetQuery<'a> { - asset: &'a str, -} - -#[derive(Deserialize)] -struct FavoriteAssetRecord { - id: String, - asset: String, -} - -#[derive(Deserialize)] -#[serde(untagged)] -enum FavoriteAssetListResponse { - List(Vec), - Paginated { results: Vec }, -} - -impl FavoriteAssetListResponse { - fn find_id(self, asset_id: &str) -> Option { - let records = match self { - Self::List(records) => records, - Self::Paginated { results } => results, - }; - - records - .into_iter() - .find(|record| record.asset == asset_id) - .map(|record| record.id) - } -} - -fn favorite_record_id(data: &str, asset_id: &str) -> Option { - serde_json::from_str::(data) - .ok() - .and_then(|response| response.find_id(asset_id)) -} - pub struct AssetService { api: ApiRequestClient, } @@ -217,24 +180,13 @@ impl AssetService { /// 从收藏列表中移除指定资产 pub async fn unfavorite(&self, asset_id: &str) -> ApiResponse { - let list_url = self.api.endpoint(endpoint::assets::FAVORITE_ASSETS); - let query = FavoriteAssetQuery { asset: asset_id }; - let lookup = self.api.get_with_query_response(&list_url, &query).await; - - if !lookup.success { - return lookup; - } + let mut url = self.api.endpoint(endpoint::assets::FAVORITE_ASSETS); - let favorite_id = match favorite_record_id(&lookup.data, asset_id) { - Some(favorite_id) => favorite_id, - None => { - warn!("收藏列表响应中未找到资产 {} 对应的收藏记录 ID", asset_id); - return ApiResponse::failed(format!("未找到资产 {} 对应的收藏记录 ID", asset_id)); - } + if let Ok(mut parsed) = Url::parse(&url) { + parsed.query_pairs_mut().append_pair("asset", asset_id); + url = parsed.to_string(); }; - let path = endpoint::assets::favorite_detail(&favorite_id); - let url = self.api.endpoint(&path); self.api.delete_with_response(&url).await } @@ -434,42 +386,3 @@ impl AssetService { .to_lowercase() } } - -#[cfg(test)] -mod tests { - use super::favorite_record_id; - - #[test] - fn favorite_record_id_should_be_found_in_list_response() { - let data = r#"[{"id":"favorite-1","asset":"asset-1"}]"#; - - assert_eq!( - favorite_record_id(data, "asset-1"), - Some("favorite-1".to_string()) - ); - } - - #[test] - fn favorite_record_id_should_be_found_in_paginated_response() { - let data = r#"{"results":[{"id":"favorite-1","asset":"asset-1"}]}"#; - - assert_eq!( - favorite_record_id(data, "asset-1"), - Some("favorite-1".to_string()) - ); - } - - #[test] - fn favorite_record_id_should_not_use_a_different_asset() { - let data = r#"[{"id":"favorite-2","asset":"asset-2"}]"#; - - assert_eq!(favorite_record_id(data, "asset-1"), None); - } - - #[test] - fn favorite_record_id_should_be_absent_when_response_has_no_id() { - let data = r#"[{"asset":"asset-1"}]"#; - - assert_eq!(favorite_record_id(data, "asset-1"), None); - } -}