38 lines
1.3 KiB
Dart
38 lines
1.3 KiB
Dart
import 'package:drift/drift.dart';
|
|
|
|
import 'package:timetrack/core/database/app_database.dart';
|
|
import 'package:timetrack/core/database/tables/projects_table.dart';
|
|
|
|
part 'projects_dao.g.dart';
|
|
|
|
@DriftAccessor(tables: [Projects])
|
|
class ProjectsDao extends DatabaseAccessor<AppDatabase>
|
|
with _$ProjectsDaoMixin {
|
|
ProjectsDao(super.db);
|
|
|
|
Stream<List<Project>> watchAll() => select(projects).watch();
|
|
|
|
Stream<List<Project>> watchActive() => (select(projects)
|
|
..where((t) => t.archivedAt.isNull()))
|
|
.watch();
|
|
|
|
Future<Project?> getById(int id) =>
|
|
(select(projects)..where((t) => t.id.equals(id))).getSingleOrNull();
|
|
|
|
Future<int> insertProject(ProjectsCompanion companion) =>
|
|
into(projects).insert(companion);
|
|
|
|
Future<bool> updateProject(ProjectsCompanion companion) =>
|
|
update(projects).replace(companion);
|
|
|
|
Future<int> deleteProject(int id) =>
|
|
(delete(projects)..where((t) => t.id.equals(id))).go();
|
|
|
|
Future<void> archiveProject(int id) => (update(projects)
|
|
..where((t) => t.id.equals(id)))
|
|
.write(ProjectsCompanion(archivedAt: Value(DateTime.now())));
|
|
|
|
Future<void> unarchiveProject(int id) => (update(projects)
|
|
..where((t) => t.id.equals(id)))
|
|
.write(const ProjectsCompanion(archivedAt: Value(null)));
|
|
}
|