feat(certificates-documents-repositories): Refactor Certificates and Documents repositories to utilize DataConnectService and simplify dependency management
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_data_connect/firebase_data_connect.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
import 'package:krow_domain/krow_domain.dart' as domain;
|
||||
import 'package:krow_core/core.dart';
|
||||
|
||||
import '../../domain/repositories/certificates_repository.dart';
|
||||
|
||||
@@ -11,37 +9,22 @@ import '../../domain/repositories/certificates_repository.dart';
|
||||
/// This class handles the communication with the backend via [ExampleConnector].
|
||||
/// It maps raw generated data types to clean [domain.StaffDocument] entities.
|
||||
class CertificatesRepositoryImpl
|
||||
with DataErrorHandler
|
||||
implements CertificatesRepository {
|
||||
/// The generated Data Connect SDK client.
|
||||
final ExampleConnector _dataConnect;
|
||||
|
||||
/// The Firebase Authentication instance.
|
||||
final FirebaseAuth _firebaseAuth;
|
||||
/// The Data Connect service instance.
|
||||
final DataConnectService _service;
|
||||
|
||||
/// Creates a [CertificatesRepositoryImpl].
|
||||
///
|
||||
/// Requires [ExampleConnector] for data access and [FirebaseAuth] for user context.
|
||||
CertificatesRepositoryImpl({
|
||||
required ExampleConnector dataConnect,
|
||||
required FirebaseAuth firebaseAuth,
|
||||
}) : _dataConnect = dataConnect,
|
||||
_firebaseAuth = firebaseAuth;
|
||||
CertificatesRepositoryImpl() : _service = DataConnectService.instance;
|
||||
|
||||
@override
|
||||
Future<List<domain.StaffDocument>> getCertificates() async {
|
||||
return executeProtected(() async {
|
||||
final User? currentUser = _firebaseAuth.currentUser;
|
||||
if (currentUser == null) {
|
||||
throw domain.NotAuthenticatedException(
|
||||
technicalMessage: 'User not authenticated');
|
||||
}
|
||||
return _service.run(() async {
|
||||
final String staffId = await _service.getStaffId();
|
||||
|
||||
// Execute the query via DataConnect generated SDK
|
||||
final QueryResult<ListStaffDocumentsByStaffIdData,
|
||||
ListStaffDocumentsByStaffIdVariables> result =
|
||||
await _dataConnect
|
||||
.listStaffDocumentsByStaffId(staffId: currentUser.uid)
|
||||
final result =
|
||||
await _service.connector
|
||||
.listStaffDocumentsByStaffId(staffId: staffId)
|
||||
.execute();
|
||||
|
||||
// Map the generated SDK types to pure Domain entities
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter_modular/flutter_modular.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
|
||||
import 'data/repositories_impl/certificates_repository_impl.dart';
|
||||
import 'domain/repositories/certificates_repository.dart';
|
||||
@@ -12,12 +10,7 @@ import 'presentation/pages/certificates_page.dart';
|
||||
class StaffCertificatesModule extends Module {
|
||||
@override
|
||||
void binds(Injector i) {
|
||||
i.addLazySingleton<CertificatesRepository>(
|
||||
() => CertificatesRepositoryImpl(
|
||||
dataConnect: i.get<ExampleConnector>(), // Assuming ExampleConnector is provided by parent module
|
||||
firebaseAuth: FirebaseAuth.instance,
|
||||
),
|
||||
);
|
||||
i.addLazySingleton<CertificatesRepository>(CertificatesRepositoryImpl.new);
|
||||
i.addLazySingleton(GetCertificatesUseCase.new);
|
||||
i.addLazySingleton(CertificatesCubit.new);
|
||||
}
|
||||
|
||||
@@ -1,39 +1,27 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_data_connect/firebase_data_connect.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
import 'package:krow_domain/krow_domain.dart' as domain;
|
||||
import 'package:krow_core/core.dart';
|
||||
|
||||
import '../../domain/repositories/documents_repository.dart';
|
||||
|
||||
/// Implementation of [DocumentsRepository] using Data Connect.
|
||||
class DocumentsRepositoryImpl
|
||||
with DataErrorHandler
|
||||
implements DocumentsRepository {
|
||||
final ExampleConnector _dataConnect;
|
||||
final FirebaseAuth _firebaseAuth;
|
||||
final DataConnectService _service;
|
||||
|
||||
DocumentsRepositoryImpl({
|
||||
required ExampleConnector dataConnect,
|
||||
required FirebaseAuth firebaseAuth,
|
||||
}) : _dataConnect = dataConnect,
|
||||
_firebaseAuth = firebaseAuth;
|
||||
DocumentsRepositoryImpl() : _service = DataConnectService.instance;
|
||||
|
||||
@override
|
||||
Future<List<domain.StaffDocument>> getDocuments() async {
|
||||
return executeProtected(() async {
|
||||
final User? currentUser = _firebaseAuth.currentUser;
|
||||
if (currentUser == null) {
|
||||
throw domain.NotAuthenticatedException(
|
||||
technicalMessage: 'User not authenticated');
|
||||
}
|
||||
return _service.run(() async {
|
||||
final String? staffId = await _service.getStaffId();
|
||||
|
||||
/// MOCK IMPLEMENTATION
|
||||
/// To be replaced with real data connect query when available
|
||||
return [
|
||||
domain.StaffDocument(
|
||||
id: 'doc1',
|
||||
staffId: currentUser.uid,
|
||||
staffId: staffId!,
|
||||
documentId: 'd1',
|
||||
name: 'Work Permit',
|
||||
description: 'Valid work permit document',
|
||||
@@ -43,7 +31,7 @@ class DocumentsRepositoryImpl
|
||||
),
|
||||
domain.StaffDocument(
|
||||
id: 'doc2',
|
||||
staffId: currentUser.uid,
|
||||
staffId: staffId!,
|
||||
documentId: 'd2',
|
||||
name: 'Health and Safety Training',
|
||||
description: 'Certificate of completion for health and safety training',
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter_modular/flutter_modular.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
import 'data/repositories_impl/documents_repository_impl.dart';
|
||||
import 'domain/repositories/documents_repository.dart';
|
||||
import 'domain/usecases/get_documents_usecase.dart';
|
||||
@@ -11,12 +9,7 @@ import 'presentation/pages/documents_page.dart';
|
||||
class StaffDocumentsModule extends Module {
|
||||
@override
|
||||
void binds(Injector i) {
|
||||
i.addLazySingleton<DocumentsRepository>(
|
||||
() => DocumentsRepositoryImpl(
|
||||
dataConnect: ExampleConnector.instance,
|
||||
firebaseAuth: FirebaseAuth.instance,
|
||||
),
|
||||
);
|
||||
i.addLazySingleton<DocumentsRepository>(DocumentsRepositoryImpl.new);
|
||||
i.addLazySingleton(GetDocumentsUseCase.new);
|
||||
i.addLazySingleton(DocumentsCubit.new);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user