feat: implement media listing with sorting and filtering options
This commit is contained in:
1
libertas_api/src/extractors/mod.rs
Normal file
1
libertas_api/src/extractors/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod query_options;
|
||||
42
libertas_api/src/extractors/query_options.rs
Normal file
42
libertas_api/src/extractors/query_options.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use axum::{extract::{FromRequestParts, Query}, http::request::Parts};
|
||||
use libertas_core::{error::CoreError, schema::{FilterParams, ListMediaOptions, SortOrder, SortParams}};
|
||||
|
||||
use crate::{error::ApiError, schema::ListMediaParams, state::AppState};
|
||||
|
||||
pub struct ApiListMediaOptions(pub ListMediaOptions);
|
||||
|
||||
impl From<ListMediaParams> for ListMediaOptions {
|
||||
fn from(params: ListMediaParams) -> Self {
|
||||
let sort = params.sort_by.map(|field| {
|
||||
let order = match params.order.as_deref() {
|
||||
Some("asc") => SortOrder::Asc,
|
||||
_ => SortOrder::Desc,
|
||||
};
|
||||
SortParams {
|
||||
sort_by: field,
|
||||
sort_order: order,
|
||||
}
|
||||
});
|
||||
|
||||
let filter = Some(FilterParams {
|
||||
// e.g., mime_type: params.mime_type
|
||||
});
|
||||
|
||||
ListMediaOptions { sort, filter }
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequestParts<AppState> for ApiListMediaOptions {
|
||||
type Rejection = ApiError;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut Parts,
|
||||
state: &AppState,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
let Query(params) = Query::<ListMediaParams>::from_request_parts(parts, state)
|
||||
.await
|
||||
.map_err(|e| ApiError::from(CoreError::Validation(e.to_string())))?;
|
||||
|
||||
Ok(ApiListMediaOptions(params.into()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user