use axum::{
extract::{Path, State},
http::StatusCode,
Json,
};
use serde::Deserialize;
use super::sessions::ErrorResponse;
use crate::web::state::WebState;
pub async fn list_starred(
State(state): State<WebState>,
) -> Result<Json<serde_json::Value>, (StatusCode, Json<ErrorResponse>)> {
let ids = state.db.list_starred().await.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
error: e.to_string(),
}),
)
})?;
Ok(Json(serde_json::json!({ "session_ids": ids })))
}
pub async fn star_session(
State(state): State<WebState>,
Path(id): Path<String>,
) -> Result<StatusCode, (StatusCode, Json<ErrorResponse>)> {
let session = state.db.get_session(&id).await.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
error: e.to_string(),
}),
)
})?;
if session.is_none() {
return Err((
StatusCode::NOT_FOUND,
Json(ErrorResponse {
error: "session not found".to_string(),
}),
));
}
state.db.star_session(&id).await.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
error: e.to_string(),
}),
)
})?;
Ok(StatusCode::NO_CONTENT)
}
pub async fn unstar_session(
State(state): State<WebState>,
Path(id): Path<String>,
) -> Result<StatusCode, (StatusCode, Json<ErrorResponse>)> {
state.db.unstar_session(&id).await.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
error: e.to_string(),
}),
)
})?;
Ok(StatusCode::NO_CONTENT)
}
#[derive(Deserialize)]
pub struct BulkStarRequest {
pub session_ids: Vec<String>,
}
pub async fn bulk_star(
State(state): State<WebState>,
Json(body): Json<BulkStarRequest>,
) -> Result<StatusCode, (StatusCode, Json<ErrorResponse>)> {
state.db.bulk_star(&body.session_ids).await.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
error: e.to_string(),
}),
)
})?;
Ok(StatusCode::NO_CONTENT)
}