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