37 lines
1000 B
Dart
37 lines
1000 B
Dart
|
|
import '../../core/network/i_api_client.dart';
|
||
|
|
import 'models/user_summary.dart';
|
||
|
|
|
||
|
|
abstract class UserRepository {
|
||
|
|
Future<UserSummary> getById(String userId);
|
||
|
|
Future<UserSummary> getMe();
|
||
|
|
}
|
||
|
|
|
||
|
|
class UserRepositoryImpl implements UserRepository {
|
||
|
|
final IApiClient _apiClient;
|
||
|
|
static const _prefix = '/api/v1/users';
|
||
|
|
|
||
|
|
UserRepositoryImpl(this._apiClient);
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<UserSummary> getById(String userId) async {
|
||
|
|
final response = await _apiClient.get<Map<String, dynamic>>(
|
||
|
|
'$_prefix/$userId',
|
||
|
|
);
|
||
|
|
final user = response.data;
|
||
|
|
if (user == null) {
|
||
|
|
throw StateError('Invalid getById response: empty payload');
|
||
|
|
}
|
||
|
|
return UserSummary.fromJson(user);
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<UserSummary> getMe() async {
|
||
|
|
final response = await _apiClient.get<Map<String, dynamic>>('$_prefix/me');
|
||
|
|
final user = response.data;
|
||
|
|
if (user == null) {
|
||
|
|
throw StateError('Invalid getMe response: empty payload');
|
||
|
|
}
|
||
|
|
return UserSummary.fromJson(user);
|
||
|
|
}
|
||
|
|
}
|