Skip to content

Commit

Permalink
add filter by date
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Mar 14, 2024
1 parent b8fde83 commit a74b34d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src-tauri/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ pub struct GameQuery {
pub player1: Option<i32>,
pub player2: Option<i32>,
pub tournament_id: Option<i32>,
pub start_date: Option<String>,
pub end_date: Option<String>,
pub range1: Option<(i32, i32)>,
pub range2: Option<(i32, i32)>,
pub sides: Option<Sides>,
Expand Down Expand Up @@ -720,6 +722,16 @@ pub async fn get_games(
count_query = count_query.filter(games::result.eq(outcome));
}

if let Some(start_date) = query.start_date {
sql_query = sql_query.filter(games::date.ge(start_date.clone()));
count_query = count_query.filter(games::date.ge(start_date));
}

if let Some(end_date) = query.end_date {
sql_query = sql_query.filter(games::date.le(end_date.clone()));
count_query = count_query.filter(games::date.le(end_date));
}

if let Some(tournament_id) = query.tournament_id {
sql_query = sql_query.filter(games::event_id.eq(tournament_id));
count_query = count_query.filter(games::event_id.eq(tournament_id));
Expand Down
12 changes: 12 additions & 0 deletions src-tauri/src/db/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ pub async fn search_position(
games::id,
games::white_id,
games::black_id,
games::date,
games::result,
games::moves,
games::fen,
Expand All @@ -265,6 +266,7 @@ pub async fn search_position(
id,
white_id,
black_id,
date,
result,
game,
fen,
Expand Down Expand Up @@ -294,6 +296,14 @@ pub async fn search_position(
.unwrap();
}

if let Some(start_date) = &query.start_date {
if let Some(date) = date {
if date < start_date {
return;
}
}
}

if let Some(white) = query.player1 {
if white != *white_id {
return;
Expand Down Expand Up @@ -396,6 +406,7 @@ pub async fn is_position_in_db(
games::id,
games::white_id,
games::black_id,
games::date,
games::result,
games::moves,
games::fen,
Expand All @@ -413,6 +424,7 @@ pub async fn is_position_in_db(
_id,
_white_id,
_black_id,
_date,
_result,
game,
fen,
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub type GameData = (
i32,
i32,
Option<String>,
Option<String>,
Vec<u8>,
Option<String>,
i32,
Expand Down
27 changes: 26 additions & 1 deletion src/components/databases/GameTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
type Outcome,
query_games,
} from "@/utils/db";
import { invoke } from "@/utils/invoke";
import { createTab } from "@/utils/tabs";
import {
ActionIcon,
Expand All @@ -22,8 +21,10 @@ import {
Text,
useMantineTheme,
} from "@mantine/core";
import { DateInput } from "@mantine/dates";
import { useHotkeys, useToggle } from "@mantine/hooks";
import { IconDotsVertical, IconEye, IconTrash } from "@tabler/icons-react";
import dayjs from "dayjs";
import { useAtom, useSetAtom } from "jotai";
import { DataTable } from "mantine-datatable";
import { useState } from "react";
Expand Down Expand Up @@ -182,6 +183,30 @@ function GameTable({ database }: { database: DatabaseInfo }) {
{ label: "Draw", value: "1/2-1/2" },
]}
/>
<Group>
<DateInput
label="From"
value={query.start_date ? dayjs(query.start_date, "YYYY.MM.DD").toDate() : null}
onChange={(value) =>
setQuery({
...query, start_date: dayjs(value).format(
"YYYY.MM.DD"
)
})
}
/>
<DateInput
label="To"
value={query.end_date ? dayjs(query.end_date, "YYYY.MM.DD").toDate() : null}
onChange={(value) =>
setQuery({
...query, end_date: dayjs(value).format(
"YYYY.MM.DD"
)
})
}
/>
</Group>
</Stack>
</Collapse>
</Box>
Expand Down
4 changes: 4 additions & 0 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export interface GameQuery extends Query {
rangePlayer2?: [number, number];
speed?: Speed;
outcome?: Outcome;
start_date?: string;
end_date?: string;
}

export interface Game {
Expand Down Expand Up @@ -110,6 +112,8 @@ export async function query_games(
sides: query.sides,
speed: query.speed,
outcome: query.outcome,
start_date: query.start_date,
end_date: query.end_date,
},
});
}
Expand Down

0 comments on commit a74b34d

Please sign in to comment.