Skip to content

Commit

Permalink
wip: sorts in game
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Dec 16, 2024
1 parent bb74a5b commit 9777a67
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/db/transfer/game.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sea_orm::{entity::prelude::*, QuerySelect};
use std::str::FromStr;
use sea_orm::{entity::prelude::*, Order, QueryOrder, QuerySelect};
use serde::{Deserialize, Serialize};

use crate::db::{entity, get_db};
Expand Down Expand Up @@ -67,7 +68,7 @@ impl From<Game> for entity::game::Model {
}

pub async fn find(
id: Option<i64>, title: Option<String>, is_enabled: Option<bool>, page: Option<u64>,
id: Option<i64>, title: Option<String>, is_enabled: Option<bool>, sorts: Option<String>, page: Option<u64>,
size: Option<u64>,
) -> Result<(Vec<entity::game::Model>, u64), DbErr> {
let mut sql = entity::game::Entity::find();
Expand All @@ -84,6 +85,23 @@ pub async fn find(
sql = sql.filter(entity::game::Column::IsEnabled.eq(is_enabled));
}

if let Some(sorts) = sorts {
let sorts = sorts.split(",").collect::<Vec<&str>>();
for sort in sorts {
let col = match crate::db::entity::game::Column::from_str(
sort.replace("-", "").as_str(),
) {
Ok(col) => col,
Err(_) => continue,
};
if sort.starts_with("-") {
sql = sql.order_by(col, Order::Desc);
} else {
sql = sql.order_by(col, Order::Asc);
}
}
}

let total = sql.clone().count(get_db()).await?;

if let Some(page) = page {
Expand Down
2 changes: 1 addition & 1 deletion src/db/transfer/game_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub async fn find(
sort.replace("-", "").as_str(),
) {
Ok(col) => col,
Err(_) => return Err(DbErr::Custom("invalid sort column".to_string())),
Err(_) => continue,
};
if sort.starts_with("-") {
sql = sql.order_by(col, Order::Desc);
Expand Down
2 changes: 2 additions & 0 deletions src/web/router/api/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct GetRequest {
pub is_enabled: Option<bool>,
pub page: Option<u64>,
pub size: Option<u64>,
pub sorts: Option<String>
}

pub async fn get(
Expand All @@ -98,6 +99,7 @@ pub async fn get(
params.id,
params.title,
params.is_enabled,
params.sorts,
params.page,
params.size,
)
Expand Down

0 comments on commit 9777a67

Please sign in to comment.