use axum::{ extract::{Path, Query, State}, http::StatusCode, response::IntoResponse, routing::get, Json, }; use cache::Expiration; use serde::{Deserialize, Serialize}; use crate::{ datasources::authors::AuthorsDatasource, routes::posts::Post, state::AppState, utils::pagination::{Pagination, PaginationQuery}, }; #[derive(Deserialize, Serialize, Clone)] pub struct Author { pub author_id: i32, pub first_name: String, pub last_name: String, pub bio: Option, pub image: Option, } #[derive(Deserialize, Serialize, Clone)] pub struct AuthorGetOneParams { pub id: i32, } #[derive(Deserialize, Serialize)] pub struct AuthorPostsResponse { posts: Vec, total_posts: i64, } pub struct AuthorsRoute; impl AuthorsRoute { pub fn routes(app_state: &AppState) -> axum::Router { axum::Router::new() .route("/authors", get(AuthorsRoute::get_all)) .route("/authors/{id}", get(AuthorsRoute::get_one)) .route("/authors/{id}/posts", get(AuthorsRoute::get_authors_posts)) .with_state(app_state.clone()) } async fn get_all( State(app_state): State, Query(query): Query, ) -> impl IntoResponse { let pagination = Pagination { page: query.page.unwrap_or(1), limit: query.limit.unwrap_or(12), }; let mut state = app_state.lock().await; let cached: Option> = state .cache .get(String::from("authors:all")) .await .unwrap_or(None); if let Some(authors) = cached { tracing::info!("grabbing all authors from cache"); return Ok(Json(authors)); } match AuthorsDatasource::get_all(&state.database, pagination).await { Ok(authors) => { tracing::info!("grabbing all authors from the database"); if let a = &authors { let author_cloned = a.clone(); let state = app_state.clone(); tracing::info!("storing database data in cache"); tokio::spawn(async move { let mut s = state.lock().await; let _ = s .cache .set( String::from("authors:all"), &author_cloned, Some(Expiration::EX(5)), None, false, ) .await; }); } Ok(Json(authors)) } Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), } } async fn get_one( State(app_state): State, Path(params): Path, ) -> impl IntoResponse { let mut state = app_state.lock().await; let cached: Option = state .cache .get(format!("authors:{}", params.id)) .await .unwrap_or(None); if let Some(author) = cached { tracing::info!("grabbing one author from cache"); return Ok(Json(author)); } match AuthorsDatasource::get_one(&state.database, params.id).await { Ok(author) => { tracing::info!("grabbing all authors from the database"); if let a = &author { let author_cloned = a.clone(); let state = app_state.clone(); tracing::info!("storing database data in cache"); tokio::spawn(async move { let mut s = state.lock().await; let _ = s .cache .set( format!("authors:{}", author_cloned.author_id), &author_cloned, Some(Expiration::EX(5)), None, false, ) .await; }); } Ok(Json(author)) } Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), } } async fn get_authors_posts( State(app_state): State, Path(params): Path, Query(pagination): Query, ) -> impl IntoResponse { let pagination = Pagination { page: pagination.page.unwrap_or(1), limit: pagination.limit.unwrap_or(12), }; let state = app_state.lock().await; match AuthorsDatasource::get_authors_posts(&state.database, params.id, pagination).await { Ok((posts, total_posts)) => Ok(Json(AuthorPostsResponse { posts, total_posts })), Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), } } }