From a3ae6ec291985398456f41f9d334171bbb2bd6d5 Mon Sep 17 00:00:00 2001 From: Gustl22 Date: Wed, 29 Nov 2023 19:42:34 +0100 Subject: [PATCH] fix: Update team matches on add to league --- .../lib/ui/edit/team_match_edit.dart | 48 ++++++++++++-- .../ui/overview/shared/matches_widget.dart | 1 + .../lib/src/data/team_match/team_match.dart | 1 + .../lib/controllers/websocket_handler.dart | 63 ++++++++++++------- 4 files changed, 84 insertions(+), 29 deletions(-) diff --git a/wrestling_scoreboard_client/lib/ui/edit/team_match_edit.dart b/wrestling_scoreboard_client/lib/ui/edit/team_match_edit.dart index 39320e1b..1ccf850e 100644 --- a/wrestling_scoreboard_client/lib/ui/edit/team_match_edit.dart +++ b/wrestling_scoreboard_client/lib/ui/edit/team_match_edit.dart @@ -10,8 +10,15 @@ class TeamMatchEdit extends StatefulWidget { final TeamMatch? teamMatch; final Team? initialHomeTeam; final Team? initialGuestTeam; + final League? initialLeague; - const TeamMatchEdit({this.teamMatch, this.initialHomeTeam, this.initialGuestTeam, Key? key}) : super(key: key); + const TeamMatchEdit({ + this.teamMatch, + this.initialHomeTeam, + this.initialGuestTeam, + this.initialLeague, + Key? key, + }) : super(key: key); @override State createState() => TeamMatchEditState(); @@ -20,12 +27,14 @@ class TeamMatchEdit extends StatefulWidget { class TeamMatchEditState extends State { final _formKey = GlobalKey(); - List? teams; + List? _availableLeagues; + List? _availableTeams; String? _location; String? _no; Team? _homeTeam; Team? _guestTeam; + League? _league; late DateTime _date; @override @@ -34,6 +43,7 @@ class TeamMatchEditState extends State { _homeTeam = widget.teamMatch?.home.team ?? widget.initialHomeTeam; _guestTeam = widget.teamMatch?.guest.team ?? widget.initialGuestTeam; _date = widget.teamMatch?.date ?? DateTime.now(); + _league = widget.teamMatch?.league ?? widget.initialLeague; } @override @@ -105,8 +115,11 @@ class TeamMatchEditState extends State { itemAsString: (u) => u.name, onFind: (String? filter) async { // TODO: filter by teams of same league, but may add an option to search all teams - teams ??= await dataProvider.readMany(); - return (filter == null ? teams! : teams!.where((element) => element.name.contains(filter))).toList(); + _availableTeams ??= await dataProvider.readMany(); + return (filter == null + ? _availableTeams! + : _availableTeams!.where((element) => element.name.contains(filter))) + .toList(); }, ), ), @@ -122,8 +135,30 @@ class TeamMatchEditState extends State { itemAsString: (u) => u.name, onFind: (String? filter) async { // TODO: filter by teams of same league, but may add an option to search all teams - teams ??= await dataProvider.readMany(); - return (filter == null ? teams! : teams!.where((element) => element.name.contains(filter))).toList(); + _availableTeams ??= await dataProvider.readMany(); + return (filter == null + ? _availableTeams! + : _availableTeams!.where((element) => element.name.contains(filter))) + .toList(); + }, + ), + ), + ListTile( + title: getDropdown( + icon: const Icon(Icons.emoji_events), + selectedItem: _league, + label: localizations.league, + context: context, + onSaved: (League? value) => setState(() { + _league = value; + }), + itemAsString: (u) => u.name, + onFind: (String? filter) async { + _availableLeagues ??= await dataProvider.readMany(); + return (filter == null + ? _availableLeagues! + : _availableLeagues!.where((element) => element.name.contains(filter))) + .toList(); }, ), ), @@ -170,6 +205,7 @@ class TeamMatchEditState extends State { home: home, guest: guest, date: _date, + league: _league, ), ); navigator.pop(); diff --git a/wrestling_scoreboard_client/lib/ui/overview/shared/matches_widget.dart b/wrestling_scoreboard_client/lib/ui/overview/shared/matches_widget.dart index 14e26814..29084d04 100644 --- a/wrestling_scoreboard_client/lib/ui/overview/shared/matches_widget.dart +++ b/wrestling_scoreboard_client/lib/ui/overview/shared/matches_widget.dart @@ -31,6 +31,7 @@ class MatchesWidget extends StatelessWidget { TeamMatchEdit( initialHomeTeam: filterObject is Team ? filterObject as Team : null, initialGuestTeam: filterObject is Team ? filterObject as Team : null, + initialLeague: filterObject is League ? filterObject as League : null, ), ), ), diff --git a/wrestling_scoreboard_common/lib/src/data/team_match/team_match.dart b/wrestling_scoreboard_common/lib/src/data/team_match/team_match.dart index c95a656e..3591422f 100644 --- a/wrestling_scoreboard_common/lib/src/data/team_match/team_match.dart +++ b/wrestling_scoreboard_common/lib/src/data/team_match/team_match.dart @@ -77,6 +77,7 @@ class TeamMatch extends WrestlingEvent with _$TeamMatch { ..addAll({ 'home_id': home.id, 'guest_id': guest.id, + 'league_id': league?.id, 'referee_id': referee?.id, 'judge_id': judge?.id, 'mat_chairman_id': matChairman?.id, diff --git a/wrestling_scoreboard_server/lib/controllers/websocket_handler.dart b/wrestling_scoreboard_server/lib/controllers/websocket_handler.dart index 50852db8..ec6bba31 100644 --- a/wrestling_scoreboard_server/lib/controllers/websocket_handler.dart +++ b/wrestling_scoreboard_server/lib/controllers/websocket_handler.dart @@ -96,16 +96,29 @@ void broadcastSingle(T single) async { filterType: Club, filterId: single.club.id))); } else if (single is TeamMatch) { - final homeMatches = await TeamMatchController() - .getManyFromQuery(TeamController.teamMatchesQuery, substitutionValues: {'id': single.home.team.id}); - - final guestMatches = await TeamMatchController() - .getManyFromQuery(TeamController.teamMatchesQuery, substitutionValues: {'id': single.guest.team.id}); + final teamMatchController = TeamMatchController(); + final homeMatches = await teamMatchController + .getManyFromQuery(TeamController.teamMatchesQuery, substitutionValues: {'id': single.home.team.id}); broadcast( jsonEncode(manyToJson(homeMatches, TeamMatch, CRUD.update, filterType: Team, filterId: single.home.team.id))); + + final guestMatches = await teamMatchController + .getManyFromQuery(TeamController.teamMatchesQuery, substitutionValues: {'id': single.guest.team.id}); broadcast( jsonEncode(manyToJson(guestMatches, TeamMatch, CRUD.update, filterType: Team, filterId: single.guest.team.id))); + + if (single.league?.id != null) { + final leagueMatches = await teamMatchController + .getMany(conditions: ['league_id = @id'], substitutionValues: {'id': single.league!.id}); + broadcast(jsonEncode(manyToJson( + leagueMatches, + TeamMatch, + CRUD.update, + filterType: League, + filterId: single.league!.id, + ))); + } } else if (single is TeamMatchFight) { } else if (single is WeightClass) { } else { @@ -142,14 +155,14 @@ void broadcastSingleRaw(Map single) async final leagueTeamParticipationController = LeagueTeamParticipationController(); broadcast(jsonEncode(manyToJson( await leagueTeamParticipationController - .getMany(conditions: ['league_id = @id'], substitutionValues: {'id': single['league_id']}), + .getManyRaw(conditions: ['league_id = @id'], substitutionValues: {'id': single['league_id']}), LeagueTeamParticipation, CRUD.update, filterType: League, filterId: single['league_id']))); broadcast(jsonEncode(manyToJson( await leagueTeamParticipationController - .getMany(conditions: ['team_id = @id'], substitutionValues: {'id': single['team_id']}), + .getManyRaw(conditions: ['team_id = @id'], substitutionValues: {'id': single['team_id']}), LeagueTeamParticipation, CRUD.update, filterType: Team, @@ -159,7 +172,7 @@ void broadcastSingleRaw(Map single) async } else if (T == Membership) { broadcast(jsonEncode(manyToJson( await MembershipController() - .getMany(conditions: ['club_id = @id'], substitutionValues: {'id': single['club_id']}), + .getManyRaw(conditions: ['club_id = @id'], substitutionValues: {'id': single['club_id']}), Membership, CRUD.update, filterType: Club, @@ -182,28 +195,32 @@ void broadcastSingleRaw(Map single) async CRUD.update, filterType: Club, filterId: single['club_id']))); - if (single['league_id'] != null) { - broadcast(jsonEncode(manyToJson( - await TeamController() - .getManyRaw(conditions: ['league_id = @id'], substitutionValues: {'id': single['league_id']}), - Team, - CRUD.update, - filterType: League, - filterId: single['league_id']))); - } } else if (T == TeamMatch) { + final teamMatchController = TeamMatchController(); + final homeTeamId = (await EntityController.query(LineupController.teamIdQuery, substitutionValues: {'id': single['home_id']}))['team_id']; - final guestTeamId = (await EntityController.query(LineupController.teamIdQuery, - substitutionValues: {'id': single['guest_id']}))['team_id']; - final homeMatches = await TeamMatchController() + final homeMatches = await teamMatchController .getManyRawFromQuery(TeamController.teamMatchesQuery, substitutionValues: {'id': homeTeamId}); + broadcast(jsonEncode(manyToJson(homeMatches, TeamMatch, CRUD.update, filterType: Team, filterId: homeTeamId))); - final guestMatches = await TeamMatchController() + final guestTeamId = (await EntityController.query(LineupController.teamIdQuery, + substitutionValues: {'id': single['guest_id']}))['team_id']; + final guestMatches = await teamMatchController .getManyRawFromQuery(TeamController.teamMatchesQuery, substitutionValues: {'id': guestTeamId}); - - broadcast(jsonEncode(manyToJson(homeMatches, TeamMatch, CRUD.update, filterType: Team, filterId: homeTeamId))); broadcast(jsonEncode(manyToJson(guestMatches, TeamMatch, CRUD.update, filterType: Team, filterId: guestTeamId))); + + if (single['league_id'] != null) { + final leagueMatches = await teamMatchController + .getManyRaw(conditions: ['league_id = @id'], substitutionValues: {'id': single['league_id']}); + broadcast(jsonEncode(manyToJson( + leagueMatches, + TeamMatch, + CRUD.update, + filterType: League, + filterId: single['league_id'], + ))); + } } else if (T == TeamMatchFight) { } else if (T == WeightClass) { } else {