Skip to content

Commit

Permalink
fix: Disallow adding existing membership of API (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Dec 7, 2024
1 parent bc6a955 commit 8698262
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,11 @@ class _MembershipDropdown extends ConsumerWidget {
authService: authService,
includeApiProviderResults: true,
);
final providerMemberships =
Iterable<Membership> providerMemberships =
providerResults[getTableNameFromType(Membership)]?.map((membership) => membership as Membership) ?? [];
// Remove all memberships, which are already in the list.
providerMemberships =
providerMemberships.where((m) => filteredMemberships.where((fm) => fm.no == m.no).isEmpty);
filteredMemberships.addAll(providerMemberships);
}
}
Expand Down
28 changes: 15 additions & 13 deletions wrestling_scoreboard_common/lib/src/services/apis/germany_by.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,15 @@ class ByGermanyWrestlingApi extends WrestlingApi {
return memberships.toSet();
}

Future<Membership?> _getMembership({
Future<Membership> _getMembership({
required int passCode,
required Future<Club?> Function(String clubId) getClub,
}) async {
final json = await _getSaisonWrestler(passCode: passCode);
if (json == null) return null;
final wrestlerJson = json['wrestler'];
final club = await getClub(wrestlerJson['clubId']);
if (wrestlerJson == null || club == null) {
return null;
throw Exception('No field "wrestler" and/or "clubId" found for json:\n$json');
}
return Membership(
club: club,
Expand Down Expand Up @@ -694,15 +693,16 @@ class ByGermanyWrestlingApi extends WrestlingApi {
Future<List<DataObject>> search({required String searchStr, required Type searchType}) async {
switch (searchType) {
case const (Membership):
final membership = await _getMembership(
passCode: int.parse(searchStr),
getClub: (clubId) async {
return await _getSingleBySyncId<Club>(clubId);
});
if (membership == null) {
throw Exception('Membership with search string "$searchStr" and type "$searchType" was not found.');
try {
final membership = await _getMembership(
passCode: int.parse(searchStr),
getClub: (clubId) async {
return await _getSingleBySyncId<Club>(clubId);
});
return [membership];
} catch (_) {
return [];
}
return [membership];
default:
throw Exception('API provider search for type $searchType is not supported yet.');
}
Expand Down Expand Up @@ -907,7 +907,7 @@ class ByGermanyWrestlingApi extends WrestlingApi {

final Map<String, Map<String, dynamic>> cachedWrestlers = {};

Future<Map<String, dynamic>?> _getSaisonWrestler({
Future<Map<String, dynamic>> _getSaisonWrestler({
required int passCode,
}) async {
final cacheId = 'p:$passCode';
Expand All @@ -932,7 +932,9 @@ class ByGermanyWrestlingApi extends WrestlingApi {
} else {
body = getWrestlerJson(passCode);
}
if (body == null) return null;
if (body == null) {
throw Exception('Response for the wrestler (passcode: $passCode) was null.');
}
cachedWrestlers[cacheId] = jsonDecode(body);
return cachedWrestlers[cacheId]!;
},
Expand Down

0 comments on commit 8698262

Please sign in to comment.