2024-09-28 01:48:58 -04:00
|
|
|
use axum::{
|
|
|
|
extract::{Path, State},
|
|
|
|
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-03-16 02:56:54 -04:00
|
|
|
use crate::{datasources::authors::AuthorsDatasource, state::AppState};
|
2024-09-28 01:48:58 -04:00
|
|
|
|
|
|
|
use super::comments::Pagination;
|
|
|
|
|
2025-03-16 02:56:54 -04:00
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
2024-09-28 01:48:58 -04:00
|
|
|
pub struct Author {
|
|
|
|
pub author_id: i32,
|
|
|
|
pub first_name: String,
|
|
|
|
pub last_name: String,
|
|
|
|
pub bio: Option<String>,
|
2024-12-02 18:29:23 -05:00
|
|
|
pub image: Option<String>,
|
2024-09-28 01:48:58 -04:00
|
|
|
}
|
|
|
|
|
2025-03-16 02:56:54 -04:00
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
2024-09-28 01:48:58 -04:00
|
|
|
pub struct AuthorGetOneParams {
|
|
|
|
pub id: i32,
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
.route("/", get(AuthorsRoute::get_all))
|
|
|
|
.route("/:id", get(AuthorsRoute::get_one))
|
2024-09-28 02:26:17 -04:00
|
|
|
.route("/: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>,
|
2024-09-28 01:48:58 -04:00
|
|
|
Json(pagination): Json<Pagination>,
|
|
|
|
) -> impl IntoResponse {
|
2025-03-16 02:56:54 -04:00
|
|
|
let mut state = app_state.lock().await;
|
|
|
|
let cached: Option<Vec<Author>> = 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))
|
|
|
|
}
|
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;
|
|
|
|
let cached: Option<Author> = 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))
|
|
|
|
}
|
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>,
|
|
|
|
) -> impl IntoResponse {
|
2025-03-16 02:56:54 -04:00
|
|
|
let state = app_state.lock().await;
|
|
|
|
|
|
|
|
match AuthorsDatasource::get_authors_posts(&state.database, params.id).await {
|
2024-09-28 02:26:17 -04:00
|
|
|
Ok(p) => Ok(Json(p)),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 01:48:58 -04:00
|
|
|
}
|