From 67ee99ab60377343e4db1e5f0fb899255b25a177 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Wed, 31 Aug 2022 14:37:03 +0000 Subject: [PATCH] pagination is added --- src/compositions/betting/composition.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/compositions/betting/composition.rs b/src/compositions/betting/composition.rs index a7f5702..791b9db 100644 --- a/src/compositions/betting/composition.rs +++ b/src/compositions/betting/composition.rs @@ -128,6 +128,25 @@ impl Composition { } } + fn get_pagination(&self, find_all: &models::FindAll) -> Result { + let mut query_pagination = String::new(); + + if let Some(pagination) = &find_all.pagination { + let page = pagination.page.unwrap_or(1); + if let Some(page_size) = pagination.page_size { + write!( + &mut query_pagination, + " OFFSET {} LIMIT {} ", + ((page - 1) * page_size) as i64, + page_size as i64 + ) + .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; + } + } + + Ok(query_pagination) + } + fn get_ordery_by(&self, find_all: &models::FindAll) -> Result { let mut query_ordery_by = String::new(); @@ -503,6 +522,11 @@ impl Composition { write!(&mut query, " ORDER BY {}", query_ordery_by) .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; } + let query_pagination = self.get_pagination(find_all)?; + if !query_pagination.is_empty() { + write!(&mut query, " {} ", query_pagination) + .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; + } println!("query: {}", query);