Skip to content

Commit

Permalink
feat: add debug_handler, improve pagination, and apply tokio mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
husni-zuhdi committed Sep 2, 2024
1 parent b9dd825 commit bc4e16d
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions internal/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::model::blog::BlogId;
use crate::model::blog::{BlogEndPage, BlogId, BlogPagination, BlogStartPage};
use crate::model::{axum::AppState, templates::*};
use crate::utils::read_version_manifest;
use askama::Template;
use axum::extract::{Path, State};
use axum::debug_handler;
use axum::extract::{Path, Query, State};
use axum::response::Html;
use log::{debug, error, info};

Expand All @@ -29,11 +30,33 @@ pub async fn get_profile() -> Html<String> {
/// get_blogs
/// Serve get_blogs HTML file
/// List our blogs title and id
pub async fn get_blogs(State(app_state): State<AppState>) -> Html<String> {
#[debug_handler]
pub async fn get_blogs(
State(app_state): State<AppState>,
pagination: Query<BlogPagination>,
) -> Html<String> {
// Locking Mutex
let data = app_state.blog_usecase.lock().expect("Mutex was poisoned");
let data = app_state.blog_usecase.lock().await;

// Setup Pagination
debug!("Pagination {:?}", &pagination);
let start = match pagination.0.start {
Some(val) => val,
None => {
info!("Set default start to 0");
BlogStartPage(0)
}
};
let end = match pagination.0.end {
Some(val) => val,
None => {
info!("Set default end to 10");
BlogEndPage(10)
}
};

// Copy data to Template struct
let blogs_data = data.blog_repo.find_all();
let blogs_data = data.blog_repo.find_blogs(start, end).await;
let blogs: Vec<BlogTemplate> = blogs_data
.iter()
.map(|blog| BlogTemplate {
Expand Down Expand Up @@ -61,11 +84,13 @@ pub async fn get_blogs(State(app_state): State<AppState>) -> Html<String> {
/// get_blog
/// Serve get_blog HTML file
/// Render our blog
#[debug_handler]
pub async fn get_blog(Path(path): Path<String>, State(app_state): State<AppState>) -> Html<String> {
// Locking Mutex
let data = app_state.blog_usecase.lock().expect("Mutex was poisoned");
let blog_data = data.blog_repo.find(BlogId(path.clone()));
let data = app_state.blog_usecase.lock().await;

// Copy data to Template struct
let blog_data = data.blog_repo.find(BlogId(path.clone())).await;
let blog = BlogTemplate {
id: path.clone().as_str(),
name: &blog_data.name.as_str(),
Expand Down

0 comments on commit bc4e16d

Please sign in to comment.