2024-09-28 01:48:58 -04:00
|
|
|
use axum::{
|
2025-07-07 21:05:27 -04:00
|
|
|
extract::{Path, Query, State},
|
2024-09-28 01:48:58 -04:00
|
|
|
http::StatusCode,
|
|
|
|
|
response::IntoResponse,
|
|
|
|
|
routing::get,
|
|
|
|
|
Json,
|
|
|
|
|
};
|
2025-07-14 20:26:17 -04:00
|
|
|
use cache::Expiration;
|
2024-09-28 01:48:58 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2025-07-07 21:05:27 -04:00
|
|
|
use crate::{
|
|
|
|
|
datasources::authors::AuthorsDatasource,
|
2026-11-09 00:04:22 -05:00
|
|
|
models::{
|
|
|
|
|
posts::Post,
|
|
|
|
|
authors::{
|
|
|
|
|
Author,
|
|
|
|
|
AuthorGetOneParams,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
services::authors::*,
|
2025-07-07 21:05:27 -04:00
|
|
|
state::AppState,
|
|
|
|
|
utils::pagination::{Pagination, PaginationQuery},
|
|
|
|
|
};
|
2024-09-28 01:48:58 -04:00
|
|
|
|
|
|
|
|
|
2025-07-07 21:05:27 -04:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
|
pub struct AuthorPostsResponse {
|
|
|
|
|
posts: Vec<Post>,
|
|
|
|
|
total_posts: i64,
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 22:38:35 -04:00
|
|
|
pub struct AuthorsRoute;
|
2024-09-28 01:48:58 -04:00
|
|
|
impl AuthorsRoute {
|
|
|
|
|
pub fn routes(app_state: &AppState) -> axum::Router {
|
|
|
|
|
axum::Router::new()
|
2025-07-07 21:05:27 -04:00
|
|
|
.route("/authors", get(AuthorsRoute::get_all))
|
|
|
|
|
.route("/authors/{id}", get(AuthorsRoute::get_one))
|
|
|
|
|
.route("/authors/{id}/posts", get(AuthorsRoute::get_authors_posts))
|
2025-03-16 02:56:54 -04:00
|
|
|
.with_state(app_state.clone())
|
2024-09-28 01:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_all(
|
2025-03-16 02:56:54 -04:00
|
|
|
State(app_state): State<AppState>,
|
2025-07-07 21:05:27 -04:00
|
|
|
Query(query): Query<PaginationQuery>,
|
2024-09-28 01:48:58 -04:00
|
|
|
) -> impl IntoResponse {
|
2025-07-07 21:05:27 -04:00
|
|
|
let pagination = Pagination {
|
|
|
|
|
page: query.page.unwrap_or(1),
|
|
|
|
|
limit: query.limit.unwrap_or(12),
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-16 02:56:54 -04:00
|
|
|
let mut state = app_state.lock().await;
|
2026-11-09 00:04:22 -05:00
|
|
|
let cached = authors_cache::<Vec<Author>>(&mut state, String::from("all")).await;
|
2025-03-16 02:56:54 -04:00
|
|
|
if let Some(authors) = cached {
|
|
|
|
|
return Ok(Json(authors));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match AuthorsDatasource::get_all(&state.database, pagination).await {
|
|
|
|
|
Ok(authors) => {
|
2026-11-09 00:04:22 -05:00
|
|
|
set_authors_cache(
|
|
|
|
|
&app_state,
|
|
|
|
|
String::from("all"),
|
|
|
|
|
authors.clone(),
|
|
|
|
|
Expiration::EX(5),
|
|
|
|
|
);
|
2025-03-16 02:56:54 -04:00
|
|
|
Ok(Json(authors))
|
|
|
|
|
}
|
2024-09-28 01:48:58 -04:00
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_one(
|
2025-03-16 02:56:54 -04:00
|
|
|
State(app_state): State<AppState>,
|
2024-09-28 01:48:58 -04:00
|
|
|
Path(params): Path<AuthorGetOneParams>,
|
|
|
|
|
) -> impl IntoResponse {
|
2025-03-16 02:56:54 -04:00
|
|
|
let mut state = app_state.lock().await;
|
2026-11-09 00:04:22 -05:00
|
|
|
let cached = authors_cache::<Author>(&mut state, params.id.to_string()).await;
|
2025-03-16 02:56:54 -04:00
|
|
|
|
|
|
|
|
if let Some(author) = cached {
|
|
|
|
|
return Ok(Json(author));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match AuthorsDatasource::get_one(&state.database, params.id).await {
|
|
|
|
|
Ok(author) => {
|
2026-11-09 00:04:22 -05:00
|
|
|
set_authors_cache(
|
|
|
|
|
&app_state,
|
|
|
|
|
author.author_id.to_string(),
|
|
|
|
|
author.clone(),
|
|
|
|
|
Expiration::EX(5),
|
|
|
|
|
);
|
2025-03-16 02:56:54 -04:00
|
|
|
|
|
|
|
|
Ok(Json(author))
|
|
|
|
|
}
|
2024-09-28 01:48:58 -04:00
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-28 02:26:17 -04:00
|
|
|
|
|
|
|
|
async fn get_authors_posts(
|
2025-03-16 02:56:54 -04:00
|
|
|
State(app_state): State<AppState>,
|
2024-09-28 02:26:17 -04:00
|
|
|
Path(params): Path<AuthorGetOneParams>,
|
2025-07-07 21:05:27 -04:00
|
|
|
Query(pagination): Query<PaginationQuery>,
|
2024-09-28 02:26:17 -04:00
|
|
|
) -> impl IntoResponse {
|
2025-07-07 21:05:27 -04:00
|
|
|
let pagination = Pagination {
|
|
|
|
|
page: pagination.page.unwrap_or(1),
|
|
|
|
|
limit: pagination.limit.unwrap_or(12),
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-16 02:56:54 -04:00
|
|
|
let state = app_state.lock().await;
|
|
|
|
|
|
2025-07-07 21:05:27 -04:00
|
|
|
match AuthorsDatasource::get_authors_posts(&state.database, params.id, pagination).await {
|
|
|
|
|
Ok((posts, total_posts)) => Ok(Json(AuthorPostsResponse { posts, total_posts })),
|
2024-09-28 02:26:17 -04:00
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-28 01:48:58 -04:00
|
|
|
}
|
2025-07-07 21:05:27 -04:00
|
|
|
|
|
|
|
|
|