Files
Oxicloud/src/interfaces/api/handlers/favorites_handler.rs
T

123 lines
3.6 KiB
Rust
Raw Normal View History

2025-04-02 03:43:44 +02:00
use axum::{
2026-02-14 01:29:34 +01:00
Json,
2025-04-02 03:43:44 +02:00
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
};
2026-02-14 01:29:34 +01:00
use std::sync::Arc;
2025-04-02 03:43:44 +02:00
use tracing::{error, info};
use crate::application::ports::favorites_ports::FavoritesUseCase;
2026-02-07 04:02:38 +01:00
use crate::interfaces::middleware::auth::AuthUser;
2025-04-02 03:43:44 +02:00
/// Handler for favorite-related API endpoints
pub async fn get_favorites(
State(favorites_service): State<Arc<dyn FavoritesUseCase>>,
2026-02-07 04:02:38 +01:00
auth_user: AuthUser,
2025-04-02 03:43:44 +02:00
) -> impl IntoResponse {
2026-02-07 04:02:38 +01:00
let user_id = &auth_user.id;
2026-02-14 01:29:34 +01:00
2025-04-02 03:43:44 +02:00
match favorites_service.get_favorites(user_id).await {
Ok(favorites) => {
info!("Retrieved {} favorites for user", favorites.len());
(StatusCode::OK, Json(serde_json::json!(favorites))).into_response()
2026-02-14 01:29:34 +01:00
}
2025-04-02 03:43:44 +02:00
Err(err) => {
error!("Error retrieving favorites: {}", err);
(
2026-02-14 01:29:34 +01:00
StatusCode::INTERNAL_SERVER_ERROR,
2025-04-02 03:43:44 +02:00
Json(serde_json::json!({
"error": format!("Failed to retrieve favorites: {}", err)
2026-02-14 01:29:34 +01:00
})),
)
.into_response()
2025-04-02 03:43:44 +02:00
}
}
}
/// Add an item to user's favorites
pub async fn add_favorite(
State(favorites_service): State<Arc<dyn FavoritesUseCase>>,
2026-02-07 04:02:38 +01:00
auth_user: AuthUser,
2025-04-02 03:43:44 +02:00
Path((item_type, item_id)): Path<(String, String)>,
) -> impl IntoResponse {
2026-02-07 04:02:38 +01:00
let user_id = &auth_user.id;
2026-02-14 01:29:34 +01:00
2025-04-02 03:43:44 +02:00
// Validate item_type
if item_type != "file" && item_type != "folder" {
return (
2026-02-14 01:29:34 +01:00
StatusCode::BAD_REQUEST,
2025-04-02 03:43:44 +02:00
Json(serde_json::json!({
"error": "Item type must be 'file' or 'folder'"
2026-02-14 01:29:34 +01:00
})),
2025-04-02 03:43:44 +02:00
);
}
2026-02-14 01:29:34 +01:00
match favorites_service
.add_to_favorites(user_id, &item_id, &item_type)
.await
{
2025-04-02 03:43:44 +02:00
Ok(_) => {
info!("Added {} '{}' to favorites", item_type, item_id);
(
2026-02-14 01:29:34 +01:00
StatusCode::CREATED,
2025-04-02 03:43:44 +02:00
Json(serde_json::json!({
"message": "Item added to favorites"
2026-02-14 01:29:34 +01:00
})),
2025-04-02 03:43:44 +02:00
)
2026-02-14 01:29:34 +01:00
}
2025-04-02 03:43:44 +02:00
Err(err) => {
error!("Error adding to favorites: {}", err);
(
2026-02-14 01:29:34 +01:00
StatusCode::INTERNAL_SERVER_ERROR,
2025-04-02 03:43:44 +02:00
Json(serde_json::json!({
"error": format!("Failed to add to favorites: {}", err)
2026-02-14 01:29:34 +01:00
})),
2025-04-02 03:43:44 +02:00
)
}
}
}
/// Remove an item from user's favorites
pub async fn remove_favorite(
State(favorites_service): State<Arc<dyn FavoritesUseCase>>,
2026-02-07 04:02:38 +01:00
auth_user: AuthUser,
2025-04-02 03:43:44 +02:00
Path((item_type, item_id)): Path<(String, String)>,
) -> impl IntoResponse {
2026-02-07 04:02:38 +01:00
let user_id = &auth_user.id;
2026-02-14 01:29:34 +01:00
match favorites_service
.remove_from_favorites(user_id, &item_id, &item_type)
.await
{
2025-04-02 03:43:44 +02:00
Ok(removed) => {
if removed {
info!("Removed {} '{}' from favorites", item_type, item_id);
(
2026-02-14 01:29:34 +01:00
StatusCode::OK,
2025-04-02 03:43:44 +02:00
Json(serde_json::json!({
"message": "Item removed from favorites"
2026-02-14 01:29:34 +01:00
})),
2025-04-02 03:43:44 +02:00
)
} else {
info!("Item {} '{}' was not in favorites", item_type, item_id);
(
2026-02-14 01:29:34 +01:00
StatusCode::NOT_FOUND,
2025-04-02 03:43:44 +02:00
Json(serde_json::json!({
"message": "Item was not in favorites"
2026-02-14 01:29:34 +01:00
})),
2025-04-02 03:43:44 +02:00
)
}
2026-02-14 01:29:34 +01:00
}
2025-04-02 03:43:44 +02:00
Err(err) => {
error!("Error removing from favorites: {}", err);
(
2026-02-14 01:29:34 +01:00
StatusCode::INTERNAL_SERVER_ERROR,
2025-04-02 03:43:44 +02:00
Json(serde_json::json!({
"error": format!("Failed to remove from favorites: {}", err)
2026-02-14 01:29:34 +01:00
})),
2025-04-02 03:43:44 +02:00
)
}
}
2026-02-14 01:29:34 +01:00
}