Skip to content

Commit

Permalink
change XxxQueries -> XxxQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
hjklchan committed Jun 26, 2024
1 parent 62fc5bb commit a8e87df
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/http/articles/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
response::{OffsetPagination, Pagination, Response},
OhMyResult,
},
utils::pagination::{PaginationQueries, PaginationUtil},
utils::pagination::{PaginationQuery, PaginationUtil},
};
use axum::extract::{Query, State};
use serde::Serialize;
Expand All @@ -25,7 +25,7 @@ struct TotalResult {

pub async fn list_handler(
State(AppState { ref pool }): State<AppState>,
Query(queries): Query<PaginationQueries>,
Query(queries): Query<PaginationQuery>,
) -> OhMyResult<Response<ListItem>> {
let mut pagination: PaginationUtil = queries.into();

Expand Down
15 changes: 5 additions & 10 deletions src/utils/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@ pub enum PaginationError {
LimitExceeded,
}

// TODO: PaginationQueries 应该实现 FromRequest 或者 FromRequestParts
// TODO: PaginationQueries should implements FromRequest or FromRequestParts
// ! 应该通过 PaginationQueries 转为 PaginationUtil 等等...
// !!
// ! 为 PaginationUtil 实现 From<PaginationQueries> 特征
#[derive(Debug, Deserialize, Clone, Copy)]
pub struct PaginationQueries {
pub struct PaginationQuery {
#[serde(default)]
page: i64,
#[serde(default)]
size: i64,
}

impl PaginationQueries {
impl PaginationQuery {
pub fn to_sql(&self) -> Result<String, PaginationError> {
if self.size > MAX_LIMIT {
return Err(PaginationError::LimitExceeded);
Expand All @@ -37,7 +32,7 @@ impl PaginationQueries {
}
}

impl PaginationQueries {
impl PaginationQuery {
pub fn page(self) -> i64 {
if self.page <= 0 {
1
Expand Down Expand Up @@ -79,8 +74,8 @@ pub struct PaginationUtil {
total_data_size: u64,
}

impl From<PaginationQueries> for PaginationUtil {
fn from(value: PaginationQueries) -> Self {
impl From<PaginationQuery> for PaginationUtil {
fn from(value: PaginationQuery) -> Self {
Self {
page: value.page as u64,
size: value.size as u64,
Expand Down

0 comments on commit a8e87df

Please sign in to comment.