| 1 |
|
| 2 |
|
| 3 |
use axum::extract::{Path, State}; |
| 4 |
use axum::response::IntoResponse; |
| 5 |
use axum::Json; |
| 6 |
|
| 7 |
use crate::{ |
| 8 |
auth::AuthUser, |
| 9 |
db::{self, ItemId}, |
| 10 |
error::{AppError, Result}, |
| 11 |
AppState, |
| 12 |
}; |
| 13 |
|
| 14 |
|
| 15 |
#[tracing::instrument(skip_all, name = "wishlists::toggle")] |
| 16 |
pub(super) async fn toggle_wishlist( |
| 17 |
State(state): State<AppState>, |
| 18 |
AuthUser(user): AuthUser, |
| 19 |
Path(item_id): Path<ItemId>, |
| 20 |
) -> Result<impl IntoResponse> { |
| 21 |
|
| 22 |
let item = db::items::get_item_by_id(&state.db, item_id) |
| 23 |
.await? |
| 24 |
.ok_or(AppError::NotFound)?; |
| 25 |
if !item.is_public { |
| 26 |
return Err(AppError::NotFound); |
| 27 |
} |
| 28 |
|
| 29 |
let currently_wishlisted = db::wishlists::is_wishlisted(&state.db, user.id, item_id).await?; |
| 30 |
|
| 31 |
if currently_wishlisted { |
| 32 |
db::wishlists::remove_from_wishlist(&state.db, user.id, item_id).await?; |
| 33 |
} else { |
| 34 |
db::wishlists::add_to_wishlist(&state.db, user.id, item_id).await?; |
| 35 |
} |
| 36 |
|
| 37 |
Ok(Json(serde_json::json!({ "wishlisted": !currently_wishlisted }))) |
| 38 |
} |
| 39 |
|