Files
seguridad_electronica/lib/db/dao/alarmas_dao.dart
T
PablinuxandClaude Sonnet 4.6 1184ab5fe2 feat: add SQLite data layer, local auth and security settings
- Add offline-first SQLite architecture with DatabaseHelper singleton,
  12 CREATE TABLE statements and full seed data on first launch
- Add 10 models (fromMap/toMap) and 12 DAOs with CRUD + getPendientes()
- Add ApiAuthService (JWT via SharedPreferences) and Sincronizador skeleton
- Move login auth from SQLite to SharedPreferences with SHA-256 PIN hash;
  default PIN 123456, platform-independent (no native plugin required)
- Add seguridad_page.dart: change PIN form (current → new → confirm)
- Add Cerrar Sesión with confirmation dialog and full stack clear
- Redesign settings_page.dart with AppColors dark theme; add Sistema
  section with "Inicializar base de datos" and "Inicializar sincronización"
- Fix Linux desktop: sqflite_common_ffi with platform detection in DatabaseHelper
- Fix ScrollController conflict on Linux: primary: false on all IndexedStack pages
- Update plan_proyecto.md with session 2–3 progress and auth roadmap

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 01:56:13 -05:00

95 lines
2.4 KiB
Dart

import '../database_helper.dart';
import '../../models/alarma.dart';
class AlarmasDao {
final _db = DatabaseHelper.instance;
Future<List<Alarma>> getAll() async {
final db = await _db.db;
final rows = await db.query('alarmas', orderBy: 'timestamp DESC');
return rows.map(Alarma.fromMap).toList();
}
Future<List<Alarma>> getActivas() async {
final db = await _db.db;
final rows = await db.query(
'alarmas',
where: 'estado = ?',
whereArgs: ['activa'],
orderBy: 'severidad DESC, timestamp DESC',
);
return rows.map(Alarma.fromMap).toList();
}
Future<List<Alarma>> getByZona(int zonaId) async {
final db = await _db.db;
final rows = await db.query(
'alarmas',
where: 'zona_id = ?',
whereArgs: [zonaId],
orderBy: 'timestamp DESC',
);
return rows.map(Alarma.fromMap).toList();
}
Future<Alarma?> getById(int id) async {
final db = await _db.db;
final rows = await db.query('alarmas', where: 'id = ?', whereArgs: [id]);
return rows.isEmpty ? null : Alarma.fromMap(rows.first);
}
Future<int> insert(Alarma a) async {
final db = await _db.db;
return db.insert('alarmas', a.toMap());
}
Future<int> update(Alarma a) async {
final db = await _db.db;
return db.update('alarmas', a.toMap(), where: 'id = ?', whereArgs: [a.id]);
}
Future<int> resolver(int id, int usuarioId, {String? notas}) async {
final db = await _db.db;
return db.update(
'alarmas',
{
'estado': 'resuelta',
'resuelto_por': usuarioId,
'notas': notas,
'sincronizado': 0,
},
where: 'id = ?',
whereArgs: [id],
);
}
Future<int> silenciar(int id) async {
final db = await _db.db;
return db.update(
'alarmas',
{'estado': 'silenciada', 'sincronizado': 0},
where: 'id = ?',
whereArgs: [id],
);
}
Future<int> delete(int id) async {
final db = await _db.db;
return db.delete('alarmas', where: 'id = ?', whereArgs: [id]);
}
Future<List<Alarma>> getPendientes() async {
final db = await _db.db;
final rows = await db.query('alarmas', where: 'sincronizado = 0');
return rows.map(Alarma.fromMap).toList();
}
Future<int> contarActivas() async {
final db = await _db.db;
final result = await db.rawQuery(
"SELECT COUNT(*) as c FROM alarmas WHERE estado = 'activa'",
);
return result.first['c'] as int;
}
}