Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tikitko committed Dec 25, 2023
1 parent e38564a commit 7d9a5ba
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 48 deletions.
11 changes: 11 additions & 0 deletions blog-generic/src/author_slug_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub const SPLIT_SYMBOL: char = '^';

pub fn clean(slug: &String) -> String {
slug.rsplit_once(SPLIT_SYMBOL)
.map(|r| r.0.to_owned())
.unwrap_or(slug.clone())
}

pub fn extend(slug: &String, suffix: &String) -> String {
format!("{slug}{SPLIT_SYMBOL}{suffix}")
}
24 changes: 3 additions & 21 deletions blog-generic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
pub mod author_slug_utils;
pub mod entities;
pub mod events;
mod page_processor;

pub const AUTHOR_SLUG_SPLIT_SYMBOL: char = '^';

pub fn clean_author_slug(slug: &String) -> String {
slug.rsplit_once(AUTHOR_SLUG_SPLIT_SYMBOL)
.map(|r| r.0.to_owned())
.unwrap_or(slug.clone())
}

pub fn extend_author_slug(slug: &String, suffix: &String) -> String {
format!("{slug}{AUTHOR_SLUG_SPLIT_SYMBOL}{suffix}")
}

pub const ITEMS_PER_PAGE: u64 = 10;

pub fn offset_for_page<const LIMIT: u64>(page: &u64) -> u64 {
let Some(real_page) = page.checked_sub(1) else {
return 0;
};
let offset = real_page * LIMIT;
offset
}
pub use page_processor::*;
25 changes: 25 additions & 0 deletions blog-generic/src/page_processor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub trait PageProcessor {
fn create_for_page(page: &u64) -> Self;
fn limit(&self) -> u64;
fn offset(&self) -> u64;
}

pub struct DefaultPageProcessor<const LIMIT: u64 = 10> {
page: u64,
}

impl<const LIMIT: u64> PageProcessor for DefaultPageProcessor<LIMIT> {
fn create_for_page(page: &u64) -> Self {
Self { page: *page }
}
fn limit(&self) -> u64 {
LIMIT
}
fn offset(&self) -> u64 {
let Some(real_page) = self.page.checked_sub(1) else {
return 0;
};
let offset = real_page * LIMIT;
offset
}
}
42 changes: 21 additions & 21 deletions blog-server-api/src/endpoints/client_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn client_handler<
};

let status = status(&request).await;
let app_content = app_content(&request).await;
let app_content = app_content::<_, DefaultPageProcessor>(&request).await;

let rendered = server_renderer(
request.path.as_str().to_string(),
Expand Down Expand Up @@ -166,25 +166,23 @@ async fn status<Extensions>(
}

// TODO: to think, if it's not a cringe
async fn app_content<Extensions>(
async fn app_content<Extensions, PP>(
request: &router::RoutedRequest<Request<Extensions>>,
) -> Option<AppContent>
where
Extensions: Resolve<std::sync::Arc<Box<dyn AuthorService>>>
+ Resolve<std::sync::Arc<Box<dyn PostService>>>
+ Resolve<std::sync::Arc<Box<dyn EntityPostService>>>,
PP: PageProcessor,
{
let offset = || -> u64 {
offset_for_page::<ITEMS_PER_PAGE>(
&request
.query
.get("page")
.map(|v| v.parse().ok())
.flatten()
.unwrap_or(1),
)
};
let limit = || -> u64 { ITEMS_PER_PAGE };
let page_processor = PP::create_for_page(
&request
.query
.get("page")
.map(|v| v.parse().ok())
.flatten()
.unwrap_or(1),
);
match Route::recognize_path(request.path.as_str())? {
Route::Post { slug: _, id } | Route::EditPost { id } => post::direct_handler(
id.to_string(),
Expand All @@ -205,20 +203,22 @@ where
.flatten()
}
Route::Posts => posts::direct_handler(
offset(),
limit(),
page_processor.offset(),
page_processor.limit(),
request.origin.extensions.resolve(),
request.origin.extensions.resolve(),
)
.await
.map(|v| app_content_encode(&v))
.flatten(),
Route::Authors => {
authors::direct_handler(offset(), limit(), request.origin.extensions.resolve())
.await
.map(|v| app_content_encode(&v))
.flatten()
}
Route::Authors => authors::direct_handler(
page_processor.offset(),
page_processor.limit(),
request.origin.extensions.resolve(),
)
.await
.map(|v| app_content_encode(&v))
.flatten(),
_ => None,
}
}
6 changes: 3 additions & 3 deletions blog-server-api/src/endpoints/telegram_login/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blog_generic::entities::LoginTelegramQuestion;
use blog_generic::*;
use blog_server_services::traits::author_service::BaseMinimalAuthor;
use blog_server_services::traits::social_service::SocialId;
use blog_server_services::utils::time_utils;
Expand Down Expand Up @@ -31,7 +31,7 @@ pub async fn http_handler(
social_service,
},): (LoginTelegramRequestContent,),
) -> Result<LoginTelegramResponseContentSuccess, LoginTelegramResponseContentFailure> {
let LoginTelegramQuestion {
let entities::LoginTelegramQuestion {
id,
first_name,
last_name,
Expand Down Expand Up @@ -78,7 +78,7 @@ pub async fn http_handler(
}

let telegram_base_minimal_author = BaseMinimalAuthor {
slug: blog_generic::extend_author_slug(
slug: author_slug_utils::extend(
&username.unwrap_or(id.to_string()),
&"t".to_string(),
),
Expand Down
6 changes: 3 additions & 3 deletions blog-server-api/src/endpoints/yandex_login/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blog_generic::entities::LoginYandexQuestion;
use blog_generic::*;
use blog_server_services::traits::author_service::BaseMinimalAuthor;
use blog_server_services::traits::social_service::SocialId;
use blog_server_services::utils::time_utils;
Expand Down Expand Up @@ -37,7 +37,7 @@ pub async fn http_handler(
social_service,
},): (LoginYandexRequestContent,),
) -> Result<LoginYandexResponseContentSuccess, LoginYandexResponseContentFailure> {
let LoginYandexQuestion {
let entities::LoginYandexQuestion {
access_token,
token_type: _,
expires_in: _,
Expand Down Expand Up @@ -66,7 +66,7 @@ pub async fn http_handler(
};

let yandex_base_minimal_author = BaseMinimalAuthor {
slug: blog_generic::extend_author_slug(&yandex_login_response.login, &"y".to_string()),
slug: author_slug_utils::extend(&yandex_login_response.login, &"y".to_string()),
first_name: yandex_login_response.first_name,
last_name: yandex_login_response.last_name,
image_url: if !yandex_login_response.is_avatar_empty {
Expand Down

0 comments on commit 7d9a5ba

Please sign in to comment.