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/cache/cache_policy.dart';
|
||||
import '../../../../data/cache/cached_repository.dart';
|
||||
import '../../../../core/logging/logger.dart';
|
||||
import '../models/friend_request.dart';
|
||||
|
||||
abstract class FriendRepository {
|
||||
@@ -16,6 +17,7 @@ abstract class FriendRepository {
|
||||
class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
||||
implements FriendRepository {
|
||||
final IApiClient _apiClient;
|
||||
final Logger _logger = getLogger('features.contacts.friend_repository');
|
||||
static const _prefix = '/api/v1/friends';
|
||||
|
||||
FriendRepositoryImpl({required IApiClient apiClient, required super.store})
|
||||
@@ -36,17 +38,27 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
||||
}
|
||||
|
||||
Future<List<FriendUser>> _loadFriendsFromRemote() async {
|
||||
final response = await _apiClient.get<List<dynamic>>(_prefix);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw StateError('Invalid getFriends response: empty payload');
|
||||
try {
|
||||
final response = await _apiClient.get<List<dynamic>>(_prefix);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw StateError('Invalid getFriends response: empty payload');
|
||||
}
|
||||
return data
|
||||
.map((item) => item as Map<String, dynamic>)
|
||||
.map(
|
||||
(item) =>
|
||||
FriendUser.fromJson(item['friend'] as Map<String, dynamic>),
|
||||
)
|
||||
.toList(growable: false);
|
||||
} catch (e, stackTrace) {
|
||||
_logger.error(
|
||||
message: 'Failed to load friends from remote',
|
||||
error: e,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
return data
|
||||
.map((item) => item as Map<String, dynamic>)
|
||||
.map(
|
||||
(item) => FriendUser.fromJson(item['friend'] as Map<String, dynamic>),
|
||||
)
|
||||
.toList(growable: false);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -55,14 +67,24 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
||||
}
|
||||
|
||||
Future<FriendRequest> _loadRequestById(String friendshipId) async {
|
||||
final response = await _apiClient.get<Map<String, dynamic>>(
|
||||
'$_prefix/requests/$friendshipId',
|
||||
);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw StateError('Invalid getRequestById response: empty payload');
|
||||
try {
|
||||
final response = await _apiClient.get<Map<String, dynamic>>(
|
||||
'$_prefix/requests/$friendshipId',
|
||||
);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw StateError('Invalid getRequestById response: empty payload');
|
||||
}
|
||||
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;
|
||||
}
|
||||
return FriendRequest.fromJson(data);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -85,30 +107,58 @@ class FriendRepositoryImpl extends CachedRepository<List<FriendUser>>
|
||||
|
||||
@override
|
||||
Future<FriendRequest> acceptRequest(String friendshipId) async {
|
||||
final response = await _apiClient.post<Map<String, dynamic>>(
|
||||
'$_prefix/requests/$friendshipId/accept',
|
||||
);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw StateError('Invalid acceptRequest response: empty payload');
|
||||
try {
|
||||
final response = await _apiClient.post<Map<String, dynamic>>(
|
||||
'$_prefix/requests/$friendshipId/accept',
|
||||
);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw StateError('Invalid acceptRequest response: empty payload');
|
||||
}
|
||||
final request = FriendRequest.fromJson(data);
|
||||
await _invalidateFriendCaches(friendshipId);
|
||||
_logger.info(
|
||||
message: 'Friend request accepted',
|
||||
extra: {'friendship_id': friendshipId},
|
||||
);
|
||||
return request;
|
||||
} catch (e, stackTrace) {
|
||||
_logger.error(
|
||||
message: 'Failed to accept friend request',
|
||||
error: e,
|
||||
stackTrace: stackTrace,
|
||||
extra: {'friendship_id': friendshipId},
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
final request = FriendRequest.fromJson(data);
|
||||
await _invalidateFriendCaches(friendshipId);
|
||||
return request;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FriendRequest> declineRequest(String friendshipId) async {
|
||||
final response = await _apiClient.post<Map<String, dynamic>>(
|
||||
'$_prefix/requests/$friendshipId/decline',
|
||||
);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw StateError('Invalid declineRequest response: empty payload');
|
||||
try {
|
||||
final response = await _apiClient.post<Map<String, dynamic>>(
|
||||
'$_prefix/requests/$friendshipId/decline',
|
||||
);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
throw StateError('Invalid declineRequest response: empty payload');
|
||||
}
|
||||
final request = FriendRequest.fromJson(data);
|
||||
await _invalidateFriendCaches(friendshipId);
|
||||
_logger.info(
|
||||
message: 'Friend request declined',
|
||||
extra: {'friendship_id': friendshipId},
|
||||
);
|
||||
return request;
|
||||
} catch (e, stackTrace) {
|
||||
_logger.error(
|
||||
message: 'Failed to decline friend request',
|
||||
error: e,
|
||||
stackTrace: stackTrace,
|
||||
extra: {'friendship_id': friendshipId},
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
final request = FriendRequest.fromJson(data);
|
||||
await _invalidateFriendCaches(friendshipId);
|
||||
return request;
|
||||
}
|
||||
|
||||
Future<void> _invalidateFriendCaches(String friendshipId) {
|
||||
|
||||
Reference in New Issue
Block a user