2023-12-22 23:37:23 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-12-23 04:06:36 +01:00
|
|
|
use axum::{Json, Form, Router, routing::{put, post, get}, extract::{State, Query}, response::Redirect};
|
2023-12-22 23:37:23 +01:00
|
|
|
|
2024-01-03 03:32:03 +01:00
|
|
|
use crate::{notifications::NotificationProcessor, model::{Page, PageOptions, PageInsertion, PageView}, storage::StorageProvider};
|
2023-12-22 23:37:23 +01:00
|
|
|
|
|
|
|
pub fn create_router_with_app_routes(state: Context) -> Router {
|
|
|
|
Router::new()
|
2023-12-23 01:26:31 +01:00
|
|
|
.route("/api", get(get_suggestion))
|
|
|
|
.route("/api", post(send_suggestion_form))
|
|
|
|
.route("/api", put(send_suggestion_json))
|
2024-01-02 03:12:29 +01:00
|
|
|
.with_state(Arc::new(state))
|
2023-12-22 23:37:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Context {
|
2023-12-28 18:19:16 +01:00
|
|
|
providers: Vec<Box<dyn NotificationProcessor<Page>>>,
|
2024-01-03 03:32:03 +01:00
|
|
|
storage: StorageProvider,
|
2023-12-22 23:37:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
2024-01-03 03:32:03 +01:00
|
|
|
pub fn new(storage: StorageProvider) -> Self {
|
|
|
|
Context { providers: Vec::new(), storage }
|
2023-12-22 23:37:23 +01:00
|
|
|
}
|
|
|
|
|
2024-01-02 03:12:29 +01:00
|
|
|
pub fn register(&mut self, notifier: Box<dyn NotificationProcessor<Page>>) {
|
2023-12-22 23:37:23 +01:00
|
|
|
self.providers.push(notifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 03:12:29 +01:00
|
|
|
async fn send_suggestion(payload: PageInsertion, state: Arc<Context>) -> Result<Redirect, String> {
|
2024-01-03 03:32:03 +01:00
|
|
|
tracing::debug!("processing insertion {:?}", payload);
|
|
|
|
match state.storage.archive(payload).await {
|
2023-12-23 04:06:36 +01:00
|
|
|
Err(e) => Err(e.to_string()),
|
2024-01-03 03:32:03 +01:00
|
|
|
Ok(page) => {
|
|
|
|
for p in state.providers.iter() {
|
|
|
|
p.process(&page).await;
|
|
|
|
}
|
|
|
|
Ok(Redirect::to("/"))
|
|
|
|
},
|
2023-12-22 23:37:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 03:12:29 +01:00
|
|
|
async fn send_suggestion_json(State(state): State<Arc<Context>>, Json(payload): Json<PageInsertion>) -> Result<Redirect, String> { send_suggestion(payload, state).await }
|
|
|
|
async fn send_suggestion_form(State(state): State<Arc<Context>>, Form(payload): Form<PageInsertion>) -> Result<Redirect, String> { send_suggestion(payload, state).await }
|
2023-12-22 23:37:23 +01:00
|
|
|
|
|
|
|
|
2024-01-03 03:32:03 +01:00
|
|
|
async fn get_suggestion(State(state): State<Arc<Context>>, Query(page): Query<PageOptions>) -> Result<Json<Vec<PageView>>, String> {
|
2023-12-22 23:37:23 +01:00
|
|
|
let offset = page.offset.unwrap_or(0);
|
|
|
|
let limit = std::cmp::min(page.limit.unwrap_or(20), 20);
|
2024-01-03 03:32:03 +01:00
|
|
|
tracing::debug!("serving suggestions (offset {} limit {}", offset, limit);
|
2023-12-22 23:37:23 +01:00
|
|
|
|
2024-01-03 03:47:30 +01:00
|
|
|
match state.storage.extract(offset, limit, true).await {
|
2023-12-22 23:37:23 +01:00
|
|
|
Ok(x) => Ok(Json(x)),
|
|
|
|
Err(e) => Err(e.to_string()),
|
|
|
|
}
|
|
|
|
}
|