Improved card with new reservations.
This commit is contained in:
@@ -17,6 +17,95 @@ pub enum ApiResponse<T> {
|
||||
Error(String)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default)]
|
||||
pub struct Page {
|
||||
page_size: i32,
|
||||
page_num: i32
|
||||
}
|
||||
|
||||
impl Page {
|
||||
pub fn new(page_size: i32) -> Self {
|
||||
Self {
|
||||
page_size,
|
||||
page_num: 1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_page(self, page_num: i32) -> Self {
|
||||
Self {
|
||||
page_num,
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn limit(&self) -> i32 {
|
||||
if self.page_size == 0 {
|
||||
i32::MAX
|
||||
} else {
|
||||
self.page_size
|
||||
}
|
||||
}
|
||||
|
||||
pub fn offset(&self) -> i32 {
|
||||
(self.page_num - 1) * self.page_size
|
||||
}
|
||||
|
||||
pub fn page_num(&self) -> i32 {
|
||||
self.page_num
|
||||
}
|
||||
|
||||
pub fn first(&mut self) {
|
||||
self.page_num = 1;
|
||||
}
|
||||
|
||||
pub fn prev(&mut self) {
|
||||
if self.page_num > 1 {
|
||||
self.page_num = self.page_num - 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) {
|
||||
self.page_num = self.page_num + 1;
|
||||
}
|
||||
|
||||
pub fn go_to(&mut self, page: i32) {
|
||||
self.page_num = page;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct PagedResponse<T> {
|
||||
page: Page,
|
||||
record_count: i64,
|
||||
data: Vec<T>
|
||||
}
|
||||
|
||||
impl<T: Clone> PagedResponse<T> {
|
||||
pub fn new(data: Vec<T>, page: Page, record_count: i64) -> Self {
|
||||
Self {
|
||||
page,
|
||||
record_count,
|
||||
data
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_first(&self) -> bool {
|
||||
self.page.page_num() == 1
|
||||
}
|
||||
|
||||
pub fn is_last(&self) -> bool {
|
||||
self.page.offset() as i64 + self.page.limit() as i64 >= self.record_count
|
||||
}
|
||||
|
||||
pub fn total_pages(&self) -> i32 {
|
||||
(self.record_count as f64 / self.page.limit() as f64).ceil() as i32
|
||||
}
|
||||
|
||||
pub fn data(&self) -> Vec<T> {
|
||||
self.data.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Validate, Default)]
|
||||
#[cfg_attr(feature = "ssr", derive(sqlx::FromRow))]
|
||||
pub struct Company {
|
||||
|
||||
Reference in New Issue
Block a user