feat(logging): add logging to contacts friend_repository
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import '../../../../data/network/i_api_client.dart';
|
import '../../../../data/network/i_api_client.dart';
|
||||||
import '../../../../data/cache/cache_policy.dart';
|
import '../../../../data/cache/cache_policy.dart';
|
||||||
import '../../../../data/cache/cached_repository.dart';
|
import '../../../../data/cache/cached_repository.dart';
|
||||||
|
import '../../../../core/logging/logger.dart';
|
||||||
import '../models/friend_request.dart';
|
import '../models/friend_request.dart';
|
||||||
|
|
||||||
abstract class FriendRepository {
|
abstract class FriendRepository {
|
||||||
@@ -16,6 +17,7 @@ abstract class FriendRepository {
|
|||||||
class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
||||||
implements FriendRepository {
|
implements FriendRepository {
|
||||||
final IApiClient _apiClient;
|
final IApiClient _apiClient;
|
||||||
|
final Logger _logger = getLogger('features.contacts.friend_repository');
|
||||||
static const _prefix = '/api/v1/friends';
|
static const _prefix = '/api/v1/friends';
|
||||||
|
|
||||||
FriendRepositoryImpl({required IApiClient apiClient, required super.store})
|
FriendRepositoryImpl({required IApiClient apiClient, required super.store})
|
||||||
@@ -36,6 +38,7 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<List<FriendUser>> _loadFriendsFromRemote() async {
|
Future<List<FriendUser>> _loadFriendsFromRemote() async {
|
||||||
|
try {
|
||||||
final response = await _apiClient.get<List<dynamic>>(_prefix);
|
final response = await _apiClient.get<List<dynamic>>(_prefix);
|
||||||
final data = response.data;
|
final data = response.data;
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
@@ -44,9 +47,18 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
|||||||
return data
|
return data
|
||||||
.map((item) => item as Map<String, dynamic>)
|
.map((item) => item as Map<String, dynamic>)
|
||||||
.map(
|
.map(
|
||||||
(item) => FriendUser.fromJson(item['friend'] as Map<String, dynamic>),
|
(item) =>
|
||||||
|
FriendUser.fromJson(item['friend'] as Map<String, dynamic>),
|
||||||
)
|
)
|
||||||
.toList(growable: false);
|
.toList(growable: false);
|
||||||
|
} catch (e, stackTrace) {
|
||||||
|
_logger.error(
|
||||||
|
message: 'Failed to load friends from remote',
|
||||||
|
error: e,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -55,6 +67,7 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<FriendRequest> _loadRequestById(String friendshipId) async {
|
Future<FriendRequest> _loadRequestById(String friendshipId) async {
|
||||||
|
try {
|
||||||
final response = await _apiClient.get<Map<String, dynamic>>(
|
final response = await _apiClient.get<Map<String, dynamic>>(
|
||||||
'$_prefix/requests/$friendshipId',
|
'$_prefix/requests/$friendshipId',
|
||||||
);
|
);
|
||||||
@@ -63,6 +76,15 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
|||||||
throw StateError('Invalid getRequestById response: empty payload');
|
throw StateError('Invalid getRequestById response: empty payload');
|
||||||
}
|
}
|
||||||
return FriendRequest.fromJson(data);
|
return FriendRequest.fromJson(data);
|
||||||
|
} catch (e, stackTrace) {
|
||||||
|
_logger.error(
|
||||||
|
message: 'Failed to load friend request by id',
|
||||||
|
error: e,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
extra: {'friendship_id': friendshipId},
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -85,6 +107,7 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<FriendRequest> acceptRequest(String friendshipId) async {
|
Future<FriendRequest> acceptRequest(String friendshipId) async {
|
||||||
|
try {
|
||||||
final response = await _apiClient.post<Map<String, dynamic>>(
|
final response = await _apiClient.post<Map<String, dynamic>>(
|
||||||
'$_prefix/requests/$friendshipId/accept',
|
'$_prefix/requests/$friendshipId/accept',
|
||||||
);
|
);
|
||||||
@@ -94,11 +117,25 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
|||||||
}
|
}
|
||||||
final request = FriendRequest.fromJson(data);
|
final request = FriendRequest.fromJson(data);
|
||||||
await _invalidateFriendCaches(friendshipId);
|
await _invalidateFriendCaches(friendshipId);
|
||||||
|
_logger.info(
|
||||||
|
message: 'Friend request accepted',
|
||||||
|
extra: {'friendship_id': friendshipId},
|
||||||
|
);
|
||||||
return request;
|
return request;
|
||||||
|
} catch (e, stackTrace) {
|
||||||
|
_logger.error(
|
||||||
|
message: 'Failed to accept friend request',
|
||||||
|
error: e,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
extra: {'friendship_id': friendshipId},
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<FriendRequest> declineRequest(String friendshipId) async {
|
Future<FriendRequest> declineRequest(String friendshipId) async {
|
||||||
|
try {
|
||||||
final response = await _apiClient.post<Map<String, dynamic>>(
|
final response = await _apiClient.post<Map<String, dynamic>>(
|
||||||
'$_prefix/requests/$friendshipId/decline',
|
'$_prefix/requests/$friendshipId/decline',
|
||||||
);
|
);
|
||||||
@@ -108,7 +145,20 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
|||||||
}
|
}
|
||||||
final request = FriendRequest.fromJson(data);
|
final request = FriendRequest.fromJson(data);
|
||||||
await _invalidateFriendCaches(friendshipId);
|
await _invalidateFriendCaches(friendshipId);
|
||||||
|
_logger.info(
|
||||||
|
message: 'Friend request declined',
|
||||||
|
extra: {'friendship_id': friendshipId},
|
||||||
|
);
|
||||||
return request;
|
return request;
|
||||||
|
} catch (e, stackTrace) {
|
||||||
|
_logger.error(
|
||||||
|
message: 'Failed to decline friend request',
|
||||||
|
error: e,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
extra: {'friendship_id': friendshipId},
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _invalidateFriendCaches(String friendshipId) {
|
Future<void> _invalidateFriendCaches(String friendshipId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user