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

153 lines
4.7 KiB
Rust
Raw Normal View History

2025-04-02 05:08:30 +02:00
use std::sync::Arc;
use axum::{
extract::{Path, State, Query},
http::StatusCode,
response::IntoResponse,
Json,
};
2025-04-04 21:31:41 +02:00
use serde::Deserialize;
2025-04-02 05:08:30 +02:00
use tracing::{error, info};
use crate::application::ports::recent_ports::RecentItemsUseCase;
2026-02-07 04:02:38 +01:00
use crate::interfaces::middleware::auth::AuthUser;
2025-04-02 05:08:30 +02:00
/// Query parameters for getting recent items
2025-04-02 05:08:30 +02:00
#[derive(Deserialize)]
pub struct GetRecentParams {
#[serde(default)]
limit: Option<i32>,
}
/// Get user's recent items
2025-04-02 05:08:30 +02:00
pub async fn get_recent_items(
State(recent_service): State<Arc<dyn RecentItemsUseCase>>,
2026-02-07 04:02:38 +01:00
auth_user: AuthUser,
2025-04-02 05:08:30 +02:00
Query(params): Query<GetRecentParams>,
) -> impl IntoResponse {
2026-02-07 04:02:38 +01:00
let user_id = &auth_user.id;
2025-04-02 05:08:30 +02:00
match recent_service.get_recent_items(user_id, params.limit).await {
Ok(items) => {
info!("Retrieved {} recent items for user", items.len());
2025-04-02 05:08:30 +02:00
(StatusCode::OK, Json(items)).into_response()
},
Err(err) => {
error!("Error retrieving recent items: {}", err);
2025-04-02 05:08:30 +02:00
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({
"error": format!("Failed to retrieve recent items: {}", err)
2025-04-02 05:08:30 +02:00
}))
).into_response()
}
}
}
/// Record access to an item
2025-04-02 05:08:30 +02:00
pub async fn record_item_access(
State(recent_service): State<Arc<dyn RecentItemsUseCase>>,
2026-02-07 04:02:38 +01:00
auth_user: AuthUser,
2025-04-02 05:08:30 +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;
2025-04-02 05:08:30 +02:00
// Validate item type
2025-04-02 05:08:30 +02:00
if item_type != "file" && item_type != "folder" {
return (
StatusCode::BAD_REQUEST,
Json(serde_json::json!({
"error": "Item type must be 'file' or 'folder'"
2025-04-02 05:08:30 +02:00
}))
).into_response();
}
match recent_service.record_item_access(user_id, &item_id, &item_type).await {
Ok(_) => {
info!("Recorded access to {} '{}' in recents", item_type, item_id);
2025-04-02 05:08:30 +02:00
(
StatusCode::OK,
Json(serde_json::json!({
"message": "Access recorded successfully"
2025-04-02 05:08:30 +02:00
}))
).into_response()
},
Err(err) => {
error!("Error recording access in recents: {}", err);
2025-04-02 05:08:30 +02:00
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({
"error": format!("Failed to record access: {}", err)
2025-04-02 05:08:30 +02:00
}))
).into_response()
}
}
}
/// Remove an item from recents
2025-04-02 05:08:30 +02:00
pub async fn remove_from_recent(
State(recent_service): State<Arc<dyn RecentItemsUseCase>>,
2026-02-07 04:02:38 +01:00
auth_user: AuthUser,
2025-04-02 05:08:30 +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;
2025-04-02 05:08:30 +02:00
match recent_service.remove_from_recent(user_id, &item_id, &item_type).await {
Ok(removed) => {
if removed {
info!("Removed {} '{}' from recents", item_type, item_id);
2025-04-02 05:08:30 +02:00
(
StatusCode::OK,
Json(serde_json::json!({
"message": "Item removed from recents"
2025-04-02 05:08:30 +02:00
}))
).into_response()
} else {
info!("Item {} '{}' was not in recents", item_type, item_id);
2025-04-02 05:08:30 +02:00
(
StatusCode::NOT_FOUND,
Json(serde_json::json!({
"message": "Item was not in recents"
2025-04-02 05:08:30 +02:00
}))
).into_response()
}
},
Err(err) => {
error!("Error removing from recents: {}", err);
2025-04-02 05:08:30 +02:00
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({
"error": format!("Failed to remove from recents: {}", err)
2025-04-02 05:08:30 +02:00
}))
).into_response()
}
}
}
/// Clear all recent items
2025-04-02 05:08:30 +02:00
pub async fn clear_recent_items(
State(recent_service): State<Arc<dyn RecentItemsUseCase>>,
2026-02-07 04:02:38 +01:00
auth_user: AuthUser,
2025-04-02 05:08:30 +02:00
) -> impl IntoResponse {
2026-02-07 04:02:38 +01:00
let user_id = &auth_user.id;
2025-04-02 05:08:30 +02:00
match recent_service.clear_recent_items(user_id).await {
Ok(_) => {
info!("Cleared all recent items for user");
2025-04-02 05:08:30 +02:00
(
StatusCode::OK,
Json(serde_json::json!({
"message": "Recent items cleared successfully"
2025-04-02 05:08:30 +02:00
}))
).into_response()
},
Err(err) => {
error!("Error clearing recent items: {}", err);
2025-04-02 05:08:30 +02:00
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({
"error": format!("Failed to clear recent items: {}", err)
2025-04-02 05:08:30 +02:00
}))
).into_response()
}
}
}