Tons of Stuff
This commit is contained in:
88
electron-app/src/main/database.ts
Normal file
88
electron-app/src/main/database.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { app } from 'electron';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
export interface PriceAlert {
|
||||
id: string;
|
||||
auecAmount: number;
|
||||
maxPrice: number;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
let db: Database.Database | null = null;
|
||||
|
||||
export function initDatabase() {
|
||||
const userDataPath = app.getPath('userData');
|
||||
const dbPath = path.join(userDataPath, 'alerts.db');
|
||||
|
||||
// Ensure directory exists
|
||||
if (!fs.existsSync(userDataPath)) {
|
||||
fs.mkdirSync(userDataPath, { recursive: true });
|
||||
}
|
||||
|
||||
db = new Database(dbPath);
|
||||
|
||||
// Create alerts table
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS alerts (
|
||||
id TEXT PRIMARY KEY,
|
||||
auecAmount REAL NOT NULL,
|
||||
maxPrice REAL NOT NULL,
|
||||
enabled INTEGER NOT NULL DEFAULT 1
|
||||
)
|
||||
`);
|
||||
|
||||
console.log('Database initialized at:', dbPath);
|
||||
}
|
||||
|
||||
export function getAllAlerts(): PriceAlert[] {
|
||||
if (!db) throw new Error('Database not initialized');
|
||||
|
||||
const stmt = db.prepare('SELECT * FROM alerts');
|
||||
const rows = stmt.all() as any[];
|
||||
|
||||
return rows.map(row => ({
|
||||
id: row.id,
|
||||
auecAmount: row.auecAmount,
|
||||
maxPrice: row.maxPrice,
|
||||
enabled: row.enabled === 1,
|
||||
}));
|
||||
}
|
||||
|
||||
export function addAlert(alert: PriceAlert): void {
|
||||
if (!db) throw new Error('Database not initialized');
|
||||
|
||||
const stmt = db.prepare(`
|
||||
INSERT INTO alerts (id, auecAmount, maxPrice, enabled)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
stmt.run(alert.id, alert.auecAmount, alert.maxPrice, alert.enabled ? 1 : 0);
|
||||
}
|
||||
|
||||
export function updateAlert(alert: PriceAlert): void {
|
||||
if (!db) throw new Error('Database not initialized');
|
||||
|
||||
const stmt = db.prepare(`
|
||||
UPDATE alerts
|
||||
SET auecAmount = ?, maxPrice = ?, enabled = ?
|
||||
WHERE id = ?
|
||||
`);
|
||||
|
||||
stmt.run(alert.auecAmount, alert.maxPrice, alert.enabled ? 1 : 0, alert.id);
|
||||
}
|
||||
|
||||
export function deleteAlert(id: string): void {
|
||||
if (!db) throw new Error('Database not initialized');
|
||||
|
||||
const stmt = db.prepare('DELETE FROM alerts WHERE id = ?');
|
||||
stmt.run(id);
|
||||
}
|
||||
|
||||
export function closeDatabase(): void {
|
||||
if (db) {
|
||||
db.close();
|
||||
db = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user