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);
|
return Some(format!("{} {}", order_by, order));
|
||||||
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() {
|
||||||
Self,
|
sqlx::query_as!(
|
||||||
"SELECT * FROM owners
|
Self,
|
||||||
ORDER BY $1
|
"SELECT * FROM owners
|
||||||
LIMIT $2
|
ORDER BY $1
|
||||||
OFFSET $3",
|
LIMIT $2
|
||||||
list_params.get_order_by(),
|
OFFSET $3",
|
||||||
list_params.limit.unwrap_or(10),
|
order_by,
|
||||||
list_params.offset.unwrap_or(0),
|
list_params.limit.unwrap_or(10),
|
||||||
)
|
list_params.offset.unwrap_or(0),
|
||||||
.fetch_all(db)
|
)
|
||||||
.await?;
|
.fetch_all(db)
|
||||||
|
.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() {
|
||||||
Self,
|
sqlx::query_as!(
|
||||||
"SELECT * FROM shops
|
Self,
|
||||||
ORDER BY $1
|
"SELECT * FROM shops
|
||||||
LIMIT $2
|
ORDER BY $1
|
||||||
OFFSET $3",
|
LIMIT $2
|
||||||
list_params.get_order_by(),
|
OFFSET $3",
|
||||||
list_params.limit.unwrap_or(10),
|
order_by,
|
||||||
list_params.offset.unwrap_or(0),
|
list_params.limit.unwrap_or(10),
|
||||||
)
|
list_params.offset.unwrap_or(0),
|
||||||
.fetch_all(db)
|
)
|
||||||
.await?;
|
.fetch_all(db)
|
||||||
|
.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