Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ilike dsl #573

Closed
wants to merge 13 commits into from
13 changes: 10 additions & 3 deletions examples/demo/src/controllers/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
#![allow(clippy::unnecessary_struct_initialization)]
#![allow(clippy::unused_async)]
use axum::extract::Query;
use loco_rs::{controller::bad_request, model::ModelError, prelude::*};
use loco_rs::{
controller::bad_request,
model::ModelError,
prelude::{
query::condition::{postgres::Postgres, ConditionBuilderTrait},
*,
},
};
use sea_orm::Condition;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -49,7 +56,7 @@ pub async fn list(
let paginated_notes = query::paginate(
&ctx.db,
Entity::find(),
Some(query::with(params.into_query()).build()),
Some(Postgres::with(params.into_query()).build()),
&pagination_query,
)
.await?;
Expand Down Expand Up @@ -138,7 +145,7 @@ pub async fn get_one(
impl ListQueryParams {
#[must_use]
pub fn into_query(&self) -> Condition {
let mut condition = query::condition();
let mut condition = Postgres::condition();

if let Some(title) = &self.title {
condition = condition.like(Column::Title, title);
Expand Down
27 changes: 19 additions & 8 deletions examples/demo/src/models/users.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use async_trait::async_trait;
use chrono::offset::Local;
use loco_rs::{auth::jwt, hash, prelude::*};
use loco_rs::{
auth::jwt,
hash,
prelude::{
query::condition::{postgres::Postgres, ConditionBuilderTrait},
*,
},
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use uuid::Uuid;
Expand Down Expand Up @@ -61,7 +68,7 @@ impl Authenticable for super::_entities::users::Model {
async fn find_by_api_key(db: &DatabaseConnection, api_key: &str) -> ModelResult<Self> {
let user = users::Entity::find()
.filter(
query::condition()
Postgres::condition()
.eq(users::Column::ApiKey, api_key)
.build(),
)
Expand All @@ -83,7 +90,11 @@ impl super::_entities::users::Model {
/// When could not find user by the given token or DB query error
pub async fn find_by_email(db: &DatabaseConnection, email: &str) -> ModelResult<Self> {
let user = users::Entity::find()
.filter(query::condition().eq(users::Column::Email, email).build())
.filter(
Postgres::condition()
.eq(users::Column::Email, email)
.build(),
)
.one(db)
.await?;
user.ok_or_else(|| ModelError::EntityNotFound)
Expand All @@ -100,7 +111,7 @@ impl super::_entities::users::Model {
) -> ModelResult<Self> {
let user = users::Entity::find()
.filter(
query::condition()
Postgres::condition()
.eq(users::Column::EmailVerificationToken, token)
.build(),
)
Expand All @@ -117,7 +128,7 @@ impl super::_entities::users::Model {
pub async fn find_by_reset_token(db: &DatabaseConnection, token: &str) -> ModelResult<Self> {
let user = users::Entity::find()
.filter(
query::condition()
Postgres::condition()
.eq(users::Column::ResetToken, token)
.build(),
)
Expand All @@ -135,7 +146,7 @@ impl super::_entities::users::Model {
let parse_uuid = Uuid::parse_str(pid).map_err(|e| ModelError::Any(e.into()))?;
let user = users::Entity::find()
.filter(
query::condition()
Postgres::condition()
.eq(users::Column::Pid, parse_uuid)
.build(),
)
Expand All @@ -152,7 +163,7 @@ impl super::_entities::users::Model {
pub async fn find_by_api_key(db: &DatabaseConnection, api_key: &str) -> ModelResult<Self> {
let user = users::Entity::find()
.filter(
query::condition()
Postgres::condition()
.eq(users::Column::ApiKey, api_key)
.build(),
)
Expand Down Expand Up @@ -181,7 +192,7 @@ impl super::_entities::users::Model {

if users::Entity::find()
.filter(
query::condition()
Postgres::condition()
.eq(users::Column::Email, &params.email)
.build(),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/tests/models/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ expression: res
---
Err(
Custom(
"{\"email\":[{\"code\":\"invalid email\",\"message\":null}],\"name\":[{\"code\":\"length\",\"message\":\"Name must be at least 2 characters long.\"}]}",
"{\"name\":[{\"code\":\"length\",\"message\":\"Name must be at least 2 characters long.\"}],\"email\":[{\"code\":\"invalid email\",\"message\":null}]}",
),
)
1 change: 0 additions & 1 deletion examples/demo/tests/requests/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ macro_rules! configure_insta {
#[serial]
async fn can_get_notes(#[case] test_name: &str, #[case] params: serde_json::Value) {
configure_insta!();

testing::request::<App, _, _>(|request, ctx| async move {
testing::seed::<App>(&ctx.db).await.unwrap();

Expand Down
Loading
Loading