feat: refactor code
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../../core/locator/locator.dart';
|
||||
import '../../../../../core/network/api_provider_imp.dart';
|
||||
|
||||
class ProfileApiProvider {
|
||||
ApiProviderImp get _api => locator<ApiProviderImp>();
|
||||
|
||||
Future<Response> getMe() => _api.get('/me');
|
||||
Future<Response> getWallet() => _api.get('/wallet');
|
||||
Future<Response> getStats() => _api.get('/stats');
|
||||
|
||||
Future<Response> updateProfile(String firstName, String avatar) =>
|
||||
_api.post('/profile', body: {'first_name': firstName, 'avatar': avatar});
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import '../../domain/entities/profile_entity.dart';
|
||||
|
||||
/// نگاشتِ پاسخهای /me، /wallet و /stats به ProfileEntity.
|
||||
class ProfileModel {
|
||||
static ProfileEntity fromJson(
|
||||
Map<String, dynamic> user,
|
||||
Map<String, dynamic> wallet,
|
||||
Map<String, dynamic> stats,
|
||||
) {
|
||||
final name = (user['first_name'] as String?)?.trim();
|
||||
final avatar = (user['avatar'] as String?)?.trim();
|
||||
final s = stats['stats'] as Map?;
|
||||
return ProfileEntity(
|
||||
name: (name == null || name.isEmpty) ? 'بازیکن' : name,
|
||||
avatar: (avatar == null || avatar.isEmpty) ? 'hakem-1' : avatar,
|
||||
mobile: (user['mobile'] as String?) ?? '',
|
||||
level: (wallet['level'] ?? 1) as int,
|
||||
trophies: (wallet['trophies'] ?? 0) as int,
|
||||
xpInto: (wallet['xp_into_level'] ?? 0) as int,
|
||||
xpNext: (wallet['xp_for_next'] ?? 1) as int,
|
||||
vip: (stats['vip'] ?? false) as bool,
|
||||
stats: s == null
|
||||
? null
|
||||
: ProfileStats(
|
||||
games: (s['games'] ?? 0) as int,
|
||||
wins: (s['wins'] ?? 0) as int,
|
||||
losses: (s['losses'] ?? 0) as int,
|
||||
kotMade: (s['kot_made'] ?? 0) as int,
|
||||
kotReceived: (s['kot_received'] ?? 0) as int,
|
||||
cuts: (s['cuts'] ?? 0) as int,
|
||||
hakemCount: (s['hakem_count'] ?? 0) as int,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../core/error/custom_error.dart';
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../../domain/entities/profile_entity.dart';
|
||||
import '../../domain/repository/profile_repository.dart';
|
||||
import '../data_source/remote/profile_api_provider.dart';
|
||||
import '../model/profile_model.dart';
|
||||
|
||||
class ProfileRepositoryImpl extends ProfileRepository {
|
||||
final ProfileApiProvider api;
|
||||
ProfileRepositoryImpl(this.api);
|
||||
|
||||
@override
|
||||
Future<DataState<ProfileEntity>> getProfile() async {
|
||||
final results =
|
||||
await Future.wait([api.getMe(), api.getWallet(), api.getStats()]);
|
||||
final Response me = results[0];
|
||||
final Response wallet = results[1];
|
||||
final Response stats = results[2];
|
||||
if (me.statusCode == 200 &&
|
||||
wallet.statusCode == 200 &&
|
||||
stats.statusCode == 200) {
|
||||
return DataSuccess(ProfileModel.fromJson(
|
||||
Map<String, dynamic>.from((me.data['user'] ?? {}) as Map),
|
||||
Map<String, dynamic>.from(wallet.data as Map),
|
||||
Map<String, dynamic>.from(stats.data as Map),
|
||||
));
|
||||
}
|
||||
return DataError(errorConvertor(me.statusCode, null));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<String>> updateProfile(ProfileParams params) async {
|
||||
final Response res = await api.updateProfile(params.firstName, params.avatar);
|
||||
if (res.statusCode == 200) return const DataSuccess('ok');
|
||||
final d = res.data;
|
||||
final msg = (d is Map && d['message'] != null) ? d['message'].toString() : null;
|
||||
return DataError(errorConvertor(res.statusCode, msg));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user