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

73 lines
3.0 KiB
Dart

import 'package:drift/drift.dart';
import 'package:uto_manager/infrastructure/database/app_database.dart';
part 'personnel_dao.g.dart';
/// Data Access Object for [Personnel] table operations.
@DriftAccessor(tables: [Personnel])
class PersonnelDao extends DatabaseAccessor<AppDatabase> with _$PersonnelDaoMixin {
PersonnelDao(super.db);
/// Returns all personnel rows.
Future<List<PersonnelData>> getAllPersonnel() => select(personnel).get();
/// Returns the personnel record with [soldierId], or null if not found.
Future<PersonnelData?> getPersonnelById(String soldierId) =>
(select(personnel)..where((t) => t.soldierId.equals(soldierId))).getSingleOrNull();
/// Returns all soldiers assigned to [slotId].
Future<List<PersonnelData>> getBySlot(String slotId) =>
(select(personnel)..where((t) => t.assignedSlotId.equals(slotId))).get();
/// Returns all unassigned soldiers.
Future<List<PersonnelData>> getUnassigned() =>
(select(personnel)..where((t) => t.assignedSlotId.isNull())).get();
/// Returns all soldiers with [primaryMos].
Future<List<PersonnelData>> getByMos(String primaryMos) =>
(select(personnel)..where((t) => t.primaryMos.equals(primaryMos))).get();
/// Returns all soldiers at [payGrade].
Future<List<PersonnelData>> getByPayGrade(String payGrade) =>
(select(personnel)..where((t) => t.payGrade.equals(payGrade))).get();
/// Inserts a new personnel row.
Future<void> insertPersonnel(PersonnelCompanion entry) =>
into(personnel).insert(entry, mode: InsertMode.insertOrFail);
/// Inserts multiple personnel rows in a single transaction.
Future<void> insertAllPersonnel(List<PersonnelCompanion> entries) =>
batch((b) => b.insertAll(personnel, entries, mode: InsertMode.insertOrFail));
/// Updates an existing personnel row.
Future<bool> updatePersonnel(PersonnelCompanion entry) => update(personnel).replace(entry);
/// Deletes the personnel record with [soldierId].
Future<int> deletePersonnel(String soldierId) =>
(delete(personnel)..where((t) => t.soldierId.equals(soldierId))).go();
/// Assigns [soldierId] to [slotId].
Future<void> assignToSlot(String soldierId, String slotId) async {
await (update(personnel)..where((t) => t.soldierId.equals(soldierId))).write(
PersonnelCompanion(assignedSlotId: Value(slotId)),
);
}
/// Clears the slot assignment for [soldierId].
Future<void> unassignFromSlot(String soldierId) async {
await (update(personnel)..where((t) => t.soldierId.equals(soldierId))).write(
const PersonnelCompanion(assignedSlotId: Value(null)),
);
}
/// Deletes all personnel records.
Future<int> deleteAllPersonnel() => delete(personnel).go();
/// Returns a stream that emits all personnel whenever the table changes.
Stream<List<PersonnelData>> watchAllPersonnel() => select(personnel).watch();
/// Returns a stream that emits unassigned soldiers on changes.
Stream<List<PersonnelData>> watchUnassigned() =>
(select(personnel)..where((t) => t.assignedSlotId.isNull())).watch();
}