Skip to content

Commit

Permalink
fix: Update team matches on add to league
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Nov 29, 2023
1 parent 01eacea commit a3ae6ec
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 29 deletions.
48 changes: 42 additions & 6 deletions wrestling_scoreboard_client/lib/ui/edit/team_match_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<StatefulWidget> createState() => TeamMatchEditState();
Expand All @@ -20,12 +27,14 @@ class TeamMatchEdit extends StatefulWidget {
class TeamMatchEditState extends State<TeamMatchEdit> {
final _formKey = GlobalKey<FormState>();

List<Team>? teams;
List<League>? _availableLeagues;
List<Team>? _availableTeams;

String? _location;
String? _no;
Team? _homeTeam;
Team? _guestTeam;
League? _league;
late DateTime _date;

@override
Expand All @@ -34,6 +43,7 @@ class TeamMatchEditState extends State<TeamMatchEdit> {
_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
Expand Down Expand Up @@ -105,8 +115,11 @@ class TeamMatchEditState extends State<TeamMatchEdit> {
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<Team, Null>();
return (filter == null ? teams! : teams!.where((element) => element.name.contains(filter))).toList();
_availableTeams ??= await dataProvider.readMany<Team, Null>();
return (filter == null
? _availableTeams!
: _availableTeams!.where((element) => element.name.contains(filter)))
.toList();
},
),
),
Expand All @@ -122,8 +135,30 @@ class TeamMatchEditState extends State<TeamMatchEdit> {
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<Team, Null>();
return (filter == null ? teams! : teams!.where((element) => element.name.contains(filter))).toList();
_availableTeams ??= await dataProvider.readMany<Team, Null>();
return (filter == null
? _availableTeams!
: _availableTeams!.where((element) => element.name.contains(filter)))
.toList();
},
),
),
ListTile(
title: getDropdown<League>(
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<League, Null>();
return (filter == null
? _availableLeagues!
: _availableLeagues!.where((element) => element.name.contains(filter)))
.toList();
},
),
),
Expand Down Expand Up @@ -170,6 +205,7 @@ class TeamMatchEditState extends State<TeamMatchEdit> {
home: home,
guest: guest,
date: _date,
league: _league,
),
);
navigator.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MatchesWidget<T extends DataObject?> 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,
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
63 changes: 40 additions & 23 deletions wrestling_scoreboard_server/lib/controllers/websocket_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,29 @@ void broadcastSingle<T extends DataObject>(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 {
Expand Down Expand Up @@ -142,14 +155,14 @@ void broadcastSingleRaw<T extends DataObject>(Map<String, dynamic> 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,
Expand All @@ -159,7 +172,7 @@ void broadcastSingleRaw<T extends DataObject>(Map<String, dynamic> 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,
Expand All @@ -182,28 +195,32 @@ void broadcastSingleRaw<T extends DataObject>(Map<String, dynamic> 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 {
Expand Down

0 comments on commit a3ae6ec

Please sign in to comment.