use crate::filter::Filter;
use crate::pagination::Pagination;
use crate::relations::Include;
use crate::traits::Model;
use crate::types::{OrderBy, Select};
pub trait WhereInput {
type Model: Model;
fn into_ir(self) -> Filter;
}
pub trait WhereUniqueInput {
type Model: Model;
fn into_ir(self) -> Filter;
}
pub trait IncludeInput {
type Model: Model;
fn into_ir(self) -> Include;
}
pub trait SelectInput {
type Model: Model;
fn into_ir(self) -> Select;
}
pub trait OrderByInput {
type Model: Model;
fn into_ir(self) -> OrderBy;
}
pub trait CreateInput {
type Model: Model;
type Data: Send + Sync;
fn into_ir(self) -> Self::Data;
}
pub trait UpdateInput {
type Model: Model;
type Data: Send + Sync;
fn into_ir(self) -> Self::Data;
}
pub trait CountSelect {
type Model: Model;
fn into_relation_names(self) -> Vec<String>;
}
pub trait AggregateInput {
type Model: Model;
}
pub trait GroupByInput {
type Model: Model;
}
#[derive(Debug, Clone, Default)]
pub struct PaginationInput {
pub skip: Option<u64>,
pub take: Option<u64>,
}
impl From<PaginationInput> for Pagination {
fn from(p: PaginationInput) -> Self {
let mut out = Pagination::new();
if let Some(n) = p.skip {
out = out.skip(n);
}
if let Some(n) = p.take {
out = out.take(n);
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestModel;
impl Model for TestModel {
const MODEL_NAME: &'static str = "TestModel";
const TABLE_NAME: &'static str = "test_models";
const PRIMARY_KEY: &'static [&'static str] = &["id"];
const COLUMNS: &'static [&'static str] = &["id"];
}
struct TestWhere;
impl WhereInput for TestWhere {
type Model = TestModel;
fn into_ir(self) -> Filter {
Filter::None
}
}
#[test]
fn where_input_lowers_to_filter_none() {
assert!(matches!(TestWhere.into_ir(), Filter::None));
}
#[test]
fn pagination_input_roundtrip() {
let p = PaginationInput {
skip: Some(5),
take: Some(10),
};
let raw: Pagination = p.into();
assert_eq!(raw.skip, Some(5));
assert_eq!(raw.take, Some(10));
}
}