use chrono::{DateTime, NaiveDate, Utc};
use uuid::Uuid;
use crate::column::{Filter, col, col_as};
use crate::error::{OrmError, Result};
use crate::model::Model;
use crate::pagination::{CursorRequest, PageRequest};
use crate::query::Builder;
use crate::value::Value;
pub use navree_http::list::*;
impl<M: Model> Builder<M> {
pub fn list(mut self, list: &ListQuery, spec: &ListSpec) -> Result<Self> {
for condition in &list.filter {
let field = spec.field(&condition.field).ok_or_else(|| {
OrmError::InvalidQuery(format!(
"`{}` is not a filter of this list",
condition.field
))
})?;
if !field.by_app {
self = self.where_(filter_of(field, condition)?);
}
}
if let (Some(text), Search::Columns(columns)) = (&list.search, spec.search) {
let matches: Vec<Filter> = columns
.iter()
.map(|column| col_as::<String>(*column).contains_ignore_case(text.clone()))
.collect();
if !matches.is_empty() {
self = self.where_(Filter::any(matches));
}
}
let sorts = spec.sort_of(list);
if !sorts.is_empty() {
self = self.reorder();
let mut last_descending = false;
for key in &sorts {
let name = sort_column(spec, &key.field)?;
self = self.order(if key.descending {
col(name).desc()
} else {
col(name).asc()
});
last_descending = key.descending;
}
let primary = M::meta().key_column();
if !sorts.iter().any(|key| key.field == primary) {
self = self.order(if last_descending {
col(primary).desc()
} else {
col(primary).asc()
});
}
}
Ok(self)
}
}
fn sort_column(spec: &ListSpec, name: &str) -> Result<&'static str> {
if let Some(sort) = spec.sorts.iter().copied().find(|sort| *sort == name) {
return Ok(sort);
}
spec.default_sort
.iter()
.map(|term| term.strip_prefix('-').unwrap_or(term))
.find(|sort| *sort == name)
.ok_or_else(|| OrmError::InvalidQuery(format!("`{name}` is not a sort of this list")))
}
pub trait ListQueryExt {
fn page_request(&self, spec: &ListSpec) -> PageRequest;
fn cursor_request(&self, spec: &ListSpec) -> CursorRequest;
}
impl ListQueryExt for ListQuery {
fn page_request(&self, spec: &ListSpec) -> PageRequest {
match spec.paging(self) {
Paging::Pages { page, per_page } => PageRequest {
page,
per_page: Some(per_page),
},
Paging::Cursor { limit, .. } => PageRequest {
page: 1,
per_page: Some(limit),
},
}
}
fn cursor_request(&self, spec: &ListSpec) -> CursorRequest {
match spec.paging(self) {
Paging::Cursor { cursor, limit, .. } => CursorRequest {
cursor,
limit: Some(limit),
},
Paging::Pages { per_page, .. } => CursorRequest {
cursor: None,
limit: Some(per_page),
},
}
}
}
fn invalid(field: &Field, text: &str, what: &str) -> OrmError {
OrmError::InvalidQuery(format!("`{text}` is not {what} (filter `{}`)", field.name))
}
fn bind(field: &Field, text: &str) -> Result<Value> {
Ok(match field.kind {
Kind::Text | Kind::OneOf(_) => Value::String(text.to_owned()),
Kind::Int => Value::I64(
text.parse()
.map_err(|_| invalid(field, text, "a whole number"))?,
),
Kind::Decimal => Value::Decimal(
text.parse()
.map_err(|_| invalid(field, text, "a decimal number"))?,
),
Kind::Bool => match text {
"true" => Value::Bool(true),
"false" => Value::Bool(false),
_ => return Err(invalid(field, text, "true or false")),
},
Kind::Date => Value::Date(date(field, text)?),
Kind::DateTime => match DateTime::parse_from_rfc3339(text) {
Ok(at) => Value::Timestamp(at.with_timezone(&Utc)),
Err(_) => Value::Timestamp(start_of(date(field, text)?)),
},
Kind::Uuid => {
Value::Uuid(Uuid::parse_str(text).map_err(|_| invalid(field, text, "a UUID"))?)
}
_ => {
return Err(OrmError::InvalidQuery(format!(
"the filter `{}` holds a kind of value this version cannot bind",
field.name
)));
}
})
}
fn date(field: &Field, text: &str) -> Result<NaiveDate> {
NaiveDate::parse_from_str(text, "%Y-%m-%d").map_err(|_| invalid(field, text, "a date"))
}
fn start_of(day: NaiveDate) -> DateTime<Utc> {
day.and_time(chrono::NaiveTime::MIN).and_utc()
}
fn is_whole_day(field: &Field, text: &str) -> bool {
field.kind == Kind::DateTime && DateTime::parse_from_rfc3339(text).is_err()
}
fn filter_of(field: &Field, condition: &Condition) -> Result<Filter> {
let source = col(field.source());
let text = condition.value();
let compare = |op: Op| -> Result<Filter> {
if is_whole_day(field, text) {
let start = start_of(date(field, text)?);
let next = start + chrono::Duration::days(1);
let (start, next) = (Value::Timestamp(start), Value::Timestamp(next));
return Ok(match op {
Op::Eq => Filter::all([source.gte(start), source.lt(next)]),
Op::Ne => Filter::any([source.lt(start), source.gte(next)]),
Op::Gt => source.gte(next),
Op::Gte => source.gte(start),
Op::Lt => source.lt(start),
_ => source.lt(next),
});
}
let value = bind(field, text)?;
Ok(match op {
Op::Eq => source.eq(value),
Op::Ne => source.ne(value),
Op::Gt => source.gt(value),
Op::Gte => source.gte(value),
Op::Lt => source.lt(value),
_ => source.lte(value),
})
};
Ok(match condition.op {
Op::Eq | Op::Ne | Op::Gt | Op::Gte | Op::Lt | Op::Lte => compare(condition.op)?,
Op::In | Op::NotIn => {
let values = condition
.values
.iter()
.map(|text| bind(field, text))
.collect::<Result<Vec<Value>>>()?;
if condition.op == Op::In {
source.is_in(values)
} else {
source.not_in(values)
}
}
Op::Contains => col_as::<String>(field.source()).contains_ignore_case(text.to_owned()),
Op::StartsWith => {
if text.contains(['%', '_', '\\']) {
return Err(OrmError::InvalidQuery(format!(
"a `starts_with` value for `{}` cannot contain %, _ or \\",
field.name
)));
}
col_as::<String>(field.source()).like(format!("{text}%"))
}
Op::Null => {
if condition.null_flag() == Some(true) {
source.is_null()
} else {
source.is_not_null()
}
}
op => {
return Err(OrmError::InvalidQuery(format!(
"the filter `{}` cannot apply `{op}` here",
field.name
)));
}
})
}