feat: add timer CRUD and DML helpers
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
|
import { QueryResult } from "@tauri-apps/plugin-sql";
|
||||||
import { getDb } from "@/db/initDb";
|
import { getDb } from "@/db/initDb";
|
||||||
import { SELECT_TIMERS, SELECT_TIMER } from "@/db/dml/timerDML";
|
import { SELECT_TIMERS, SELECT_TIMER, INSERT_TIMER, UPDATE_TIMER, DELETE_TIMER } from "@/db/dml/timerDML";
|
||||||
import { TimersRow, TimerRow } from "@/types/timerType";
|
import { TimersRow, TimerRow } from "@/types/timerType";
|
||||||
|
|
||||||
|
|
||||||
@@ -9,8 +10,58 @@ export async function getTimers(): Promise<TimersRow[]> {
|
|||||||
return await db.select<TimersRow[]>(SELECT_TIMERS);
|
return await db.select<TimersRow[]>(SELECT_TIMERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Получает таймер по его id **/
|
/** Получает таймер по его id */
|
||||||
export async function getTimer(TimerId: number): Promise<TimerRow> {
|
export async function getTimer(TimerId: number): Promise<TimerRow> {
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
return await db.select<TimerRow>(SELECT_TIMER, [TimerId]);
|
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]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,3 +5,6 @@ export const INSERT_SEED_DB = `
|
|||||||
('Timer mini example', 10, 3, 15, 2), ('Timer max example', 90, 10, 40, 8)
|
('Timer mini example', 10, 3, 15, 2), ('Timer max example', 90, 10, 40, 8)
|
||||||
`
|
`
|
||||||
export const SELECT_TIMER = 'SELECT * FROM timer WHERE id = ?'
|
export const SELECT_TIMER = 'SELECT * FROM timer WHERE id = ?'
|
||||||
|
export const INSERT_TIMER = 'INSERT INTO timer (title, pomodoro_time, break_time, break_long_time, count_pomodoro) VALUES (?, ?, ?, ?, ?)'
|
||||||
|
export const UPDATE_TIMER = 'UPDATE timer SET title = ?, pomodoro_time = ?, break_time = ?, break_long_time = ?, count_pomodoro = ? WHERE id = ?'
|
||||||
|
export const DELETE_TIMER = 'DELETE FROM timer WHERE id = ?'
|
||||||
|
|||||||
Reference in New Issue
Block a user