diff --git a/wrestling_scoreboard_common/lib/src/services/api.dart b/wrestling_scoreboard_common/lib/src/services/api.dart index 916e7c88..39ff99fe 100644 --- a/wrestling_scoreboard_common/lib/src/services/api.dart +++ b/wrestling_scoreboard_common/lib/src/services/api.dart @@ -1,4 +1,6 @@ -export 'reports/germany_nrw.dart'; +import '../../common.dart'; + +export 'apis/germany_nrw.dart'; enum WrestlingApiProvider { deNwRingenApi; @@ -6,10 +8,12 @@ enum WrestlingApiProvider { WrestlingApi get api { switch (this) { case WrestlingApiProvider.deNwRingenApi: - throw UnimplementedError(); + return NrwGermanyWrestlingApi(); } } } /// Abstraction for providing an api interface. -abstract class WrestlingApi {} +abstract class WrestlingApi { + Future> importLeagues({int? season}); +} diff --git a/wrestling_scoreboard_common/lib/src/services/apis/germany_nrw.dart b/wrestling_scoreboard_common/lib/src/services/apis/germany_nrw.dart new file mode 100644 index 00000000..e5bf75a7 --- /dev/null +++ b/wrestling_scoreboard_common/lib/src/services/apis/germany_nrw.dart @@ -0,0 +1,93 @@ +import 'dart:convert'; + +import 'package:http/http.dart' as http; + +import '../../../common.dart'; + +extension NrwGermanyLeague on League { + static Future fromJson(Map e, GetSingleOfTypeCallback getSingle) async { + final boutConfig = await getSingle(e['boutSchemeId'] as int); + return League( + id: e['id'] as int?, + name: e['name'] as String, + startDate: e['start_date'] as DateTime, + seasonPartitions: e['season_partitions'] as int, + boutConfig: boutConfig, + ); + } +} + +class NrwGermanyWrestlingApi extends WrestlingApi { + final _apiUrl = 'https://www.brv-ringen.de/Api/v1/cs/'; + + @override + Future> importLeagues({int? season}) async { + return await _getLeagueList(seasonId: season?.toString()); + } + + static Future getSingleFromDataType(int id) { + // return getControllerFromDataType(T).getSingle(id) as Future; + throw UnimplementedError(); + } + + /// Get all seasons + Future> _getSeasonList() async { + final uri = Uri.parse(_apiUrl).replace(queryParameters: { + 'op': 'listSaison', + }); + final response = await http.get(uri); + if (response.statusCode == 200) { + final Map json = jsonDecode(response.body); + final Map competitionList = json['ligaList']; + return await Future.wait(competitionList.entries.map((entry) async => entry.value.toString())); + } else { + throw Exception('Failed to get the saison list: ${response.reasonPhrase ?? response.statusCode.toString()}'); + } + } + + /// Get leagues of a season + Future> _getLeagueList({ + String? seasonId, + }) async { + seasonId ??= DateTime.now().year.toString(); + final uri = Uri.parse(_apiUrl).replace(queryParameters: { + 'op': 'listLiga', + 'sid': seasonId, + }); + final response = await http.get(uri); + if (response.statusCode == 200) { + final Map json = jsonDecode(response.body); + final Map ligaList = json['ligaList']; + return await Future.wait( + ligaList.entries.map((entry) => NrwGermanyLeague.fromJson(entry.value, getSingleFromDataType))); + } else { + throw Exception( + 'Failed to get the liga list (seasonId: $seasonId): ${response.reasonPhrase ?? response.statusCode.toString()}'); + } + } + + /// Get team matches of a league + Future> _getCompetitionList({ + String? seasonId, + String ligaId = 'Gruppenliga', + String regionId = 'Süd', + }) async { + seasonId ??= DateTime.now().year.toString(); + final uri = Uri.parse(_apiUrl).replace(queryParameters: { + 'op': 'listCompetition', + 'sid': seasonId, + 'ligaId': ligaId, + 'rid': regionId, + }); + final response = await http.get(uri); + if (response.statusCode == 200) { + final Map json = jsonDecode(response.body); + final Map competitionList = json['competitionList']; + return await Future.wait( + competitionList.entries.map((entry) => NrwGermanyLeague.fromJson(entry.value, getSingleFromDataType))); + } else { + throw Exception( + 'Failed to get the competition list (seasonId: $seasonId, ligaId: $ligaId, rid: $regionId): ${response.reasonPhrase ?? response.statusCode.toString()}'); + } + } +} diff --git a/wrestling_scoreboard_common/pubspec.yaml b/wrestling_scoreboard_common/pubspec.yaml index dd318820..0af78be3 100644 --- a/wrestling_scoreboard_common/pubspec.yaml +++ b/wrestling_scoreboard_common/pubspec.yaml @@ -10,6 +10,7 @@ dependencies: json_annotation: ^4.8.1 freezed_annotation: ^2.4.1 country: ^4.1.3 + http: ^1.2.1 dev_dependencies: build_runner: ^2.3.0 diff --git a/wrestling_scoreboard_common/test/services/apis_test.dart b/wrestling_scoreboard_common/test/services/apis_test.dart new file mode 100644 index 00000000..d9f71086 --- /dev/null +++ b/wrestling_scoreboard_common/test/services/apis_test.dart @@ -0,0 +1,15 @@ +import 'package:test/test.dart'; +import 'package:wrestling_scoreboard_common/common.dart'; + +void main() { + group('APIs', () { + test('Germany, NRW', () async { + final wrestlingApi = WrestlingApiProvider.deNwRingenApi.api; + final leagues = await wrestlingApi.importLeagues(season: 2023); + expect( + leagues, + '', + ); + }); + }); +} diff --git a/wrestling_scoreboard_common/test/services/reports_test.dart b/wrestling_scoreboard_common/test/services/reports_test.dart index 1039794e..1a8e5448 100644 --- a/wrestling_scoreboard_common/test/services/reports_test.dart +++ b/wrestling_scoreboard_common/test/services/reports_test.dart @@ -80,7 +80,7 @@ void main() { group('Reports', () { test('Germany, NRW', () { - final wrestlingReport = NrwGermanyWrestlingReporter(); + final wrestlingReport = WrestlingReportProvider.deNwRdb274.reporter; final report = wrestlingReport.exportTeamMatchReport( teamMatch, { diff --git a/wrestling_scoreboard_server/lib/controllers/service_controller.dart b/wrestling_scoreboard_server/lib/controllers/service_controller.dart new file mode 100644 index 00000000..f680ec19 --- /dev/null +++ b/wrestling_scoreboard_server/lib/controllers/service_controller.dart @@ -0,0 +1,19 @@ +import 'package:shelf/shelf.dart'; +import 'package:wrestling_scoreboard_common/common.dart'; + +import 'entity_controller.dart'; + +class ServiceController { + Future import(Request request, String provider) async { + final apiProvider = WrestlingApiProvider.values.byName(provider); + try { + final leagues = await apiProvider.api.importLeagues(season: 2023); + // Iterable entityControllers = + // dataTypes.map((t) => EntityController.getControllerFromDataType(t)); + // await Future.forEach(entityControllers, (e) => e.deleteMany()); + return Response.ok('{"status": "success"}'); + } catch (err) { + return Response.internalServerError(body: '{"err": "$err"}'); + } + } +} diff --git a/wrestling_scoreboard_server/lib/routes/api_route.dart b/wrestling_scoreboard_server/lib/routes/api_route.dart index 35d7b939..07c9ce42 100644 --- a/wrestling_scoreboard_server/lib/routes/api_route.dart +++ b/wrestling_scoreboard_server/lib/routes/api_route.dart @@ -1,8 +1,5 @@ import 'package:shelf/shelf.dart'; import 'package:shelf_router/shelf_router.dart'; -import 'package:wrestling_scoreboard_server/controllers/database_controller.dart'; -import 'package:wrestling_scoreboard_server/controllers/league_team_participation_controller.dart'; -import 'package:wrestling_scoreboard_server/controllers/division_weight_class_controller.dart'; import '../controllers/bout_action_controller.dart'; import '../controllers/bout_config_controller.dart'; @@ -10,14 +7,18 @@ import '../controllers/bout_controller.dart'; import '../controllers/club_controller.dart'; import '../controllers/competition_bout_controller.dart'; import '../controllers/competition_controller.dart'; +import '../controllers/database_controller.dart'; import '../controllers/organization_controller.dart'; import '../controllers/division_controller.dart'; import '../controllers/league_controller.dart'; +import '../controllers/league_team_participation_controller.dart'; +import '../controllers/league_weight_class_controller.dart'; import '../controllers/lineup_controller.dart'; import '../controllers/membership_controller.dart'; import '../controllers/participant_state_controller.dart'; import '../controllers/participation_controller.dart'; import '../controllers/person_controller.dart'; +import '../controllers/service_controller.dart'; import '../controllers/team_controller.dart'; import '../controllers/team_match_bout_controller.dart'; import '../controllers/team_match_controller.dart'; @@ -35,6 +36,9 @@ class ApiRoute { router.post('/database/restore', databaseController.restore); router.post('/database/restore_default', databaseController.restoreDefault); + final serviceController = ServiceController(); + router.post('/service/api//import', serviceController.import); + final boutConfigController = BoutConfigController(); router.post('/bout_config', boutConfigController.postSingle); router.get('/bout_configs', boutConfigController.requestMany);