//! Wishlist API: toggle items in the user's wishlist. use axum::extract::{Path, State}; use axum::response::IntoResponse; use axum::Json; use crate::{ auth::AuthUser, db::{self, ItemId}, error::{AppError, Result}, AppState, }; /// Toggle an item's wishlist status. Returns the new state. #[tracing::instrument(skip_all, name = "wishlists::toggle")] pub(super) async fn toggle_wishlist( State(state): State, AuthUser(user): AuthUser, Path(item_id): Path, ) -> Result { // Verify item exists and is public let item = db::items::get_item_by_id(&state.db, item_id) .await? .ok_or(AppError::NotFound)?; if !item.is_public { return Err(AppError::NotFound); } let currently_wishlisted = db::wishlists::is_wishlisted(&state.db, user.id, item_id).await?; if currently_wishlisted { db::wishlists::remove_from_wishlist(&state.db, user.id, item_id).await?; } else { db::wishlists::add_to_wishlist(&state.db, user.id, item_id).await?; } Ok(Json(serde_json::json!({ "wishlisted": !currently_wishlisted }))) }