Files
kawai-focus-v2/client/src/db/crud/timerCrud.ts
2026-06-05 14:38:51 +03:00

68 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { QueryResult } from "@tauri-apps/plugin-sql";
import { getDb } from "@/db/initDb";
import { SELECT_TIMERS, SELECT_TIMER, INSERT_TIMER, UPDATE_TIMER, DELETE_TIMER } from "@/db/dml/timerDML";
import { TimersRow, TimerRow } from "@/types/timerType";
/** Получает список таймеров */
export async function getTimers(): Promise<TimersRow[]> {
const db = await getDb();
return await db.select<TimersRow[]>(SELECT_TIMERS);
}
/** Получает таймер по его id */
export async function getTimer(TimerId: number): Promise<TimerRow> {
const db = await getDb();
const rows = await db.select<TimerRow[]>(SELECT_TIMER, [TimerId]);
if (!Array.isArray(rows) || rows.length === 0) {
throw new Error('Таймер не найден');
}
return rows[0];
}
/** Создаёт новый таймер */
export async function createTimer(
title: string,
pomodoroTime: number,
breakTime: number,
breakLongTime: number,
countPomodoro: number
): Promise<QueryResult> {
const db = await getDb();
return await db.execute(INSERT_TIMER, [
title,
pomodoroTime,
breakTime,
breakLongTime,
countPomodoro
]);
}
/** Обновляет таймер */
export async function updateTimer(
TimerId: number,
title: string,
pomodoroTime: number,
breakTime: number,
breakLongTime: number,
countPomodoro: number
): Promise<QueryResult> {
const db = await getDb();
return await db.execute(UPDATE_TIMER, [
title,
pomodoroTime,
breakTime,
breakLongTime,
countPomodoro,
TimerId
]);
}
/** Удаляет таймер */
export async function deleteTimer(TimerId: number): Promise<QueryResult> {
const db = await getDb();
return await db.execute(DELETE_TIMER, [TimerId]);
}