From 15fd9ae27576736e9733764018f5d35c41c5528b Mon Sep 17 00:00:00 2001 From: Arduinum Date: Thu, 4 Jun 2026 22:12:01 +0300 Subject: [PATCH] feat: add timer CRUD and DML helpers --- client/src/db/crud/timerCrud.ts | 57 +++++++++++++++++++++++++++++++-- client/src/db/dml/timerDML.ts | 3 ++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/client/src/db/crud/timerCrud.ts b/client/src/db/crud/timerCrud.ts index a0170e8..7c2f46d 100644 --- a/client/src/db/crud/timerCrud.ts +++ b/client/src/db/crud/timerCrud.ts @@ -1,5 +1,6 @@ +import { QueryResult } from "@tauri-apps/plugin-sql"; 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"; @@ -9,8 +10,58 @@ export async function getTimers(): Promise { return await db.select(SELECT_TIMERS); } -/* Получает таймер по его id **/ +/** Получает таймер по его id */ export async function getTimer(TimerId: number): Promise { const db = await getDb(); - return await db.select(SELECT_TIMER, [TimerId]); + const rows = await db.select(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 { + 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 { + const db = await getDb(); + return await db.execute(UPDATE_TIMER, [ + title, + pomodoroTime, + breakTime, + breakLongTime, + countPomodoro, + TimerId + ]); +} + +/** Удаляет таймер */ +export async function deleteTimer(TimerId: number): Promise { + const db = await getDb(); + return await db.execute(DELETE_TIMER, [TimerId]); } diff --git a/client/src/db/dml/timerDML.ts b/client/src/db/dml/timerDML.ts index 95f4014..28337f8 100644 --- a/client/src/db/dml/timerDML.ts +++ b/client/src/db/dml/timerDML.ts @@ -5,3 +5,6 @@ export const INSERT_SEED_DB = ` ('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 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 = ?'