add: shared structs, caching services, import task change
This commit is contained in:
41 files changed
+875
-770
No files matched your search
@@ -0,0 +1,116 @@
|
||||
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,
|
||||
models::{
|
||||
posts::Post,
|
||||
authors::{
|
||||
Author,
|
||||
AuthorGetOneParams,
|
||||
}
|
||||
},
|
||||
services::authors::*,
|
||||
state::AppState,
|
||||
utils::pagination::{Pagination, PaginationQuery},
|
||||
};
|
||||
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct AuthorPostsResponse {
|
||||
posts: Vec<Post>,
|
||||
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<AppState>,
|
||||
Query(query): Query<PaginationQuery>,
|
||||
) -> 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 = authors_cache::<Vec<Author>>(&mut state, String::from("all")).await;
|
||||
if let Some(authors) = cached {
|
||||
return Ok(Json(authors));
|
||||
}
|
||||
|
||||
match AuthorsDatasource::get_all(&state.database, pagination).await {
|
||||
Ok(authors) => {
|
||||
set_authors_cache(
|
||||
&app_state,
|
||||
String::from("all"),
|
||||
authors.clone(),
|
||||
Expiration::EX(5),
|
||||
);
|
||||
Ok(Json(authors))
|
||||
}
|
||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_one(
|
||||
State(app_state): State<AppState>,
|
||||
Path(params): Path<AuthorGetOneParams>,
|
||||
) -> impl IntoResponse {
|
||||
let mut state = app_state.lock().await;
|
||||
let cached = authors_cache::<Author>(&mut state, params.id.to_string()).await;
|
||||
|
||||
if let Some(author) = cached {
|
||||
return Ok(Json(author));
|
||||
}
|
||||
|
||||
match AuthorsDatasource::get_one(&state.database, params.id).await {
|
||||
Ok(author) => {
|
||||
set_authors_cache(
|
||||
&app_state,
|
||||
author.author_id.to_string(),
|
||||
author.clone(),
|
||||
Expiration::EX(5),
|
||||
);
|
||||
|
||||
Ok(Json(author))
|
||||
}
|
||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_authors_posts(
|
||||
State(app_state): State<AppState>,
|
||||
Path(params): Path<AuthorGetOneParams>,
|
||||
Query(pagination): Query<PaginationQuery>,
|
||||
) -> 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())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user