#[derive(Clone, Debug)]
pub enum OrderType {
Asc,
Desc,
None,
}
impl OrderType {
pub fn sql(&self) -> String {
match self {
OrderType::Asc => "asc".to_string(),
OrderType::Desc => "desc".to_string(),
OrderType::None => "".to_string(),
}
}
}
#[derive(Clone, Debug)]
pub struct Order {
pub field: String,
pub order_type: OrderType,
}
impl Order {
pub fn asc(field: String) -> Self {
Self {
field,
order_type: OrderType::Asc,
}
}
pub fn desc(field: String) -> Self {
Self {
field,
order_type: OrderType::Desc,
}
}
pub fn new(field: String) -> Self {
Self {
field,
order_type: OrderType::None,
}
}
}
pub struct PageRequest {
page_size: usize,
page_no: usize,
pub total_page_info: bool,
}
impl PageRequest {
pub fn new(page_size: usize, page_no: usize) -> Self {
Self {
page_no,
page_size,
total_page_info: false,
}
}
pub fn with_total(page_size: usize, page_no: usize) -> Self {
Self {
page_no,
page_size,
total_page_info: true,
}
}
pub fn get_page_no(&self) -> usize {
if self.page_no <= 0 {
1
} else {
self.page_no
}
}
pub fn get_page_size(&self) -> usize {
if self.page_size <= 0 {
20
} else {
self.page_size
}
}
}
#[derive(Default)]
pub struct PageResult<O>
where
O: std::marker::Send,
O: Unpin,
{
pub page_size: usize,
pub page_no: usize,
pub total: usize,
pub page_count: usize,
pub records: Vec<O>,
}
impl<O> PageResult<O>
where
O: std::marker::Send,
O: Unpin,
{
pub fn set_total(&mut self, total: usize) {
self.total = total;
if total == 0 {
self.page_count = 0;
} else {
let page_count = total / self.page_size;
if total % self.page_size != 0 {
self.page_count = page_count + 1;
} else {
self.page_count = page_count
}
}
}
}