uto_manager/lib/infrastructure/database/daos/position_slot_dao.dart

73 lines
3.1 KiB
Dart

import 'package:drift/drift.dart';
import 'package:uto_manager/infrastructure/database/app_database.dart';
part 'position_slot_dao.g.dart';
/// Data Access Object for [PositionSlots] table operations.
@DriftAccessor(tables: [PositionSlots])
class PositionSlotDao extends DatabaseAccessor<AppDatabase> with _$PositionSlotDaoMixin {
PositionSlotDao(super.db);
/// Returns all position slot rows.
Future<List<PositionSlot>> getAllSlots() => select(positionSlots).get();
/// Returns the slot with [slotId], or null if not found.
Future<PositionSlot?> getSlotById(String slotId) =>
(select(positionSlots)..where((t) => t.slotId.equals(slotId))).getSingleOrNull();
/// Returns all slots belonging to [unitId].
Future<List<PositionSlot>> getSlotsByUnit(String unitId) =>
(select(positionSlots)..where((t) => t.unitId.equals(unitId))).get();
/// Returns all vacant slots (no assigned personnel).
Future<List<PositionSlot>> getVacantSlots() =>
(select(positionSlots)..where((t) => t.assignedPersonnelId.isNull())).get();
/// Returns all key leader slots.
Future<List<PositionSlot>> getKeyLeaderSlots() =>
(select(positionSlots)..where((t) => t.isKeyLeader.equals(true))).get();
/// Inserts a new position slot row.
Future<void> insertSlot(PositionSlotsCompanion entry) =>
into(positionSlots).insert(entry, mode: InsertMode.insertOrFail);
/// Inserts multiple position slot rows in a single transaction.
Future<void> insertAllSlots(List<PositionSlotsCompanion> entries) =>
batch((b) => b.insertAll(positionSlots, entries, mode: InsertMode.insertOrFail));
/// Updates an existing position slot row.
Future<bool> updateSlot(PositionSlotsCompanion entry) => update(positionSlots).replace(entry);
/// Assigns [personnelId] to the slot with [slotId].
Future<void> assignPersonnel(String slotId, String personnelId) async {
await (update(positionSlots)..where((t) => t.slotId.equals(slotId))).write(
PositionSlotsCompanion(assignedPersonnelId: Value(personnelId)),
);
}
/// Clears the personnel assignment for the slot with [slotId].
Future<void> clearAssignment(String slotId) async {
await (update(positionSlots)..where((t) => t.slotId.equals(slotId))).write(
const PositionSlotsCompanion(assignedPersonnelId: Value(null)),
);
}
/// Deletes the slot with [slotId].
Future<int> deleteSlot(String slotId) =>
(delete(positionSlots)..where((t) => t.slotId.equals(slotId))).go();
/// Deletes all slots belonging to [unitId].
Future<int> deleteSlotsByUnit(String unitId) =>
(delete(positionSlots)..where((t) => t.unitId.equals(unitId))).go();
/// Deletes all position slots.
Future<int> deleteAllSlots() => delete(positionSlots).go();
/// Returns a stream that emits all slots whenever the table changes.
Stream<List<PositionSlot>> watchAllSlots() => select(positionSlots).watch();
/// Returns a stream that emits slots for [unitId] on changes.
Stream<List<PositionSlot>> watchSlotsByUnit(String unitId) =>
(select(positionSlots)..where((t) => t.unitId.equals(unitId))).watch();
}