1// 1. DOMAIN LAYER (Pure Dart - No Flutter imports, No Dio)
2// entities/user_entity.dart
3class UserEntity {
4 final String id;
5 final String email;
6 final bool isVerified;
7
8 const UserEntity({
9 required this.id,
10 required this.email,
11 required this.isVerified,
12 });
13}
14
15// repositories/auth_repository.dart (Contract)
16abstract class AuthRepository {
17 Future<Result<UserEntity, Failure>> getProfile(String userId);
18}
19
20// 2. DATA LAYER (Implements Domain interface, handles JSON)
21// models/user_dto.dart
22class UserDto {
23 final String userId;
24 final String emailAddress;
25 final int verificationStatus;
26
27 UserDto.fromJson(Map<String, dynamic> json)
28 : userId = json['user_id'] as String,
29 emailAddress = json['email_address'] as String,
30 verificationStatus = json['verification_status'] as int;
31
32 // Crucial: Mapper converts fragile DTO into stable Domain Entity
33 UserEntity toEntity() => UserEntity(
34 id: userId,
35 email: emailAddress,
36 isVerified: verificationStatus == 1,
37 );
38}
39
40// repositories/auth_repository_impl.dart
41class AuthRepositoryImpl implements AuthRepository {
42 final AuthRemoteDataSource remoteSource;
43 final AuthLocalDataSource localCache;
44
45 AuthRepositoryImpl({required this.remoteSource, required this.localCache});
46
47 @override
48 Future<Result<UserEntity, Failure>> getProfile(String userId) async {
49 try {
50 final UserDto dto = await remoteSource.fetchUser(userId);
51 await localCache.cacheUser(dto);
52 return Success(dto.toEntity());
53 } on NetworkException catch (e) {
54 // Fallback to local cache if network is unavailable
55 final cachedDto = await localCache.getCachedUser(userId);
56 if (cachedDto != null) return Success(cachedDto.toEntity());
57 return Error(NetworkFailure(e.message));
58 }
59 }
60}