88 lines
2.2 KiB
Rust
88 lines
2.2 KiB
Rust
use axum::{
|
|
extract::{Path, Query},
|
|
response::IntoResponse,
|
|
routing::{get, post},
|
|
Json, Router,
|
|
};
|
|
use chrono::NaiveDateTime;
|
|
use serde::Deserialize;
|
|
use std::sync::Arc;
|
|
use uuid::Uuid;
|
|
|
|
use crate::{
|
|
content::{collection::CollectionRepository, post::PostRepository, site::SiteRepository},
|
|
database::Database,
|
|
http::error::ApiError,
|
|
state::AppState,
|
|
};
|
|
|
|
const DEFAULT_PAGE_SIZE: i64 = 10;
|
|
|
|
async fn create_collection<Repo: CollectionRepository>(repository: Repo) {
|
|
todo!()
|
|
}
|
|
|
|
async fn get_collection<Repo: CollectionRepository>(repository: Repo) {
|
|
todo!()
|
|
}
|
|
|
|
async fn list_collections_for_site<Repo: SiteRepository + CollectionRepository>(
|
|
repository: Repo,
|
|
Path(site): Path<String>,
|
|
) -> Result<impl IntoResponse, ApiError<'static>> {
|
|
let (site_id, _) = repository.get_site_id_and_owner(&site).await?;
|
|
|
|
Ok(Json(repository.list_collections(&site_id).await?))
|
|
}
|
|
|
|
async fn update_collection<Repo: CollectionRepository>(repository: Repo) {
|
|
todo!()
|
|
}
|
|
|
|
async fn delete_collection<Repo: CollectionRepository>(repository: Repo) {
|
|
todo!()
|
|
}
|
|
|
|
#[derive(Deserialize, Default)]
|
|
struct PaginationQuery {
|
|
before: Option<NaiveDateTime>,
|
|
limit: Option<i64>,
|
|
}
|
|
|
|
async fn list_posts_in_collection<Repo: SiteRepository + PostRepository>(
|
|
repository: Repo,
|
|
Path((site, id)): Path<(String, Uuid)>,
|
|
query: Query<PaginationQuery>,
|
|
) -> Result<impl IntoResponse, ApiError<'static>> {
|
|
// Resolve site
|
|
let (site_id, _) = repository.get_site_id_and_owner(&site).await?;
|
|
|
|
let results = repository
|
|
.list_posts_in_collection(
|
|
&site_id,
|
|
&id,
|
|
query.before,
|
|
query.limit.unwrap_or(DEFAULT_PAGE_SIZE),
|
|
)
|
|
.await?;
|
|
Ok(Json(results))
|
|
}
|
|
|
|
pub fn router() -> Router<Arc<AppState>> {
|
|
Router::new()
|
|
.route(
|
|
"/:site",
|
|
get(list_collections_for_site::<Database>).post(create_collection::<Database>),
|
|
)
|
|
.route(
|
|
"/:site/:id",
|
|
get(get_collection::<Database>)
|
|
.put(update_collection::<Database>)
|
|
.delete(delete_collection::<Database>),
|
|
)
|
|
.route(
|
|
"/:site/:id/posts",
|
|
get(list_posts_in_collection::<Database>),
|
|
)
|
|
}
|