feat: add tournoment feature
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import '../../../../../core/locator/locator.dart';
|
||||
import '../../../../../core/network/api_provider_imp.dart';
|
||||
import '../../../domain/entities/tournament.dart';
|
||||
|
||||
/// دادهٔ تورنومنتها از backend (فهرست، ردهبندی، ثبتنام).
|
||||
class TournamentApiProvider {
|
||||
ApiProviderImp get _api => locator<ApiProviderImp>();
|
||||
|
||||
Future<List<Tournament>> getTournaments() async {
|
||||
final res = await _api.get('/tournaments');
|
||||
if (res.statusCode != 200) {
|
||||
throw Exception('tournaments: ${res.statusCode}');
|
||||
}
|
||||
final list = (res.data['tournaments'] as List?) ?? const [];
|
||||
return list
|
||||
.map((e) => Tournament.fromJson(Map<String, dynamic>.from(e as Map)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<List<TournamentStanding>> getStandings(int id) async {
|
||||
final res = await _api.get('/tournaments/standings', query: {'id': '$id'});
|
||||
if (res.statusCode != 200) {
|
||||
throw Exception('standings: ${res.statusCode}');
|
||||
}
|
||||
final list = (res.data['standings'] as List?) ?? const [];
|
||||
return list
|
||||
.map((e) =>
|
||||
TournamentStanding.fromJson(Map<String, dynamic>.from(e as Map)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// ثبتنام؛ null یعنی موفق، وگرنه پیامِ خطای فارسی.
|
||||
Future<String?> join(int id) async {
|
||||
final res = await _api.post('/tournaments/join', body: {'id': id});
|
||||
if (res.statusCode == 200) return null;
|
||||
final msg = (res.data is Map) ? res.data['message']?.toString() : null;
|
||||
switch (res.statusCode) {
|
||||
case 402:
|
||||
return 'سکهٔ کافی برای ثبتنام ندارید';
|
||||
case 409:
|
||||
return msg ?? 'امکان ثبتنام نیست';
|
||||
case 404:
|
||||
return 'تورنومنت یافت نشد';
|
||||
default:
|
||||
return msg ?? 'ثبتنام ناموفق بود';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user