Allow no ORDER BY clause by default in list method
This commit is contained in:
parent
359096e0bf
commit
9ea3e35359
@ -37,10 +37,11 @@ pub struct ListParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ListParams {
|
impl ListParams {
|
||||||
pub fn get_order_by(&self) -> String {
|
pub fn get_order_by(&self) -> Option<String> {
|
||||||
let default_order_by = "updated_at".to_string();
|
if let Some(order_by) = self.order_by.as_ref() {
|
||||||
let order_by = self.order_by.as_ref().unwrap_or(&default_order_by);
|
|
||||||
let order = self.order.as_ref().unwrap_or(&Order::Desc);
|
let order = self.order.as_ref().unwrap_or(&Order::Desc);
|
||||||
format!("{} {}", order_by, order)
|
return Some(format!("{} {}", order_by, order));
|
||||||
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,18 +62,31 @@ impl Model for Owner {
|
|||||||
|
|
||||||
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
|
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
|
||||||
let timer = std::time::Instant::now();
|
let timer = std::time::Instant::now();
|
||||||
let result = sqlx::query_as!(
|
let result = if let Some(order_by) = list_params.get_order_by() {
|
||||||
|
sqlx::query_as!(
|
||||||
Self,
|
Self,
|
||||||
"SELECT * FROM owners
|
"SELECT * FROM owners
|
||||||
ORDER BY $1
|
ORDER BY $1
|
||||||
LIMIT $2
|
LIMIT $2
|
||||||
OFFSET $3",
|
OFFSET $3",
|
||||||
list_params.get_order_by(),
|
order_by,
|
||||||
list_params.limit.unwrap_or(10),
|
list_params.limit.unwrap_or(10),
|
||||||
list_params.offset.unwrap_or(0),
|
list_params.offset.unwrap_or(0),
|
||||||
)
|
)
|
||||||
.fetch_all(db)
|
.fetch_all(db)
|
||||||
.await?;
|
.await?
|
||||||
|
} else {
|
||||||
|
sqlx::query_as!(
|
||||||
|
Self,
|
||||||
|
"SELECT * FROM owners
|
||||||
|
LIMIT $1
|
||||||
|
OFFSET $2",
|
||||||
|
list_params.limit.unwrap_or(10),
|
||||||
|
list_params.offset.unwrap_or(0),
|
||||||
|
)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
let elapsed = timer.elapsed();
|
let elapsed = timer.elapsed();
|
||||||
debug!("SELECT * FROM owners ... {:.3?}", elapsed);
|
debug!("SELECT * FROM owners ... {:.3?}", elapsed);
|
||||||
Ok(result)
|
Ok(result)
|
||||||
|
@ -67,18 +67,31 @@ impl Model for Shop {
|
|||||||
|
|
||||||
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
|
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
|
||||||
let timer = std::time::Instant::now();
|
let timer = std::time::Instant::now();
|
||||||
let result = sqlx::query_as!(
|
let result = if let Some(order_by) = list_params.get_order_by() {
|
||||||
|
sqlx::query_as!(
|
||||||
Self,
|
Self,
|
||||||
"SELECT * FROM shops
|
"SELECT * FROM shops
|
||||||
ORDER BY $1
|
ORDER BY $1
|
||||||
LIMIT $2
|
LIMIT $2
|
||||||
OFFSET $3",
|
OFFSET $3",
|
||||||
list_params.get_order_by(),
|
order_by,
|
||||||
list_params.limit.unwrap_or(10),
|
list_params.limit.unwrap_or(10),
|
||||||
list_params.offset.unwrap_or(0),
|
list_params.offset.unwrap_or(0),
|
||||||
)
|
)
|
||||||
.fetch_all(db)
|
.fetch_all(db)
|
||||||
.await?;
|
.await?
|
||||||
|
} else {
|
||||||
|
sqlx::query_as!(
|
||||||
|
Self,
|
||||||
|
"SELECT * FROM shops
|
||||||
|
LIMIT $1
|
||||||
|
OFFSET $2",
|
||||||
|
list_params.limit.unwrap_or(10),
|
||||||
|
list_params.offset.unwrap_or(0),
|
||||||
|
)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
let elapsed = timer.elapsed();
|
let elapsed = timer.elapsed();
|
||||||
debug!("SELECT * FROM shops ... {:.3?}", elapsed);
|
debug!("SELECT * FROM shops ... {:.3?}", elapsed);
|
||||||
Ok(result)
|
Ok(result)
|
||||||
|
Loading…
Reference in New Issue
Block a user