Skip to content

Commit

Permalink
add duplicate data for easy fetch (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p authored Jul 5, 2024
1 parent d12480c commit 3d730ae
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 130 deletions.
6 changes: 6 additions & 0 deletions data/lib/api/match/match_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class MatchModel with _$MatchModel {
required MatchType match_type,
required int number_of_over,
required int over_per_bowler,
@Default([]) List<String> players,
@Default([]) List<String> team_ids,
@Default([]) List<String> team_creator_ids,
@Default([]) List<int> power_play_overs1,
@Default([]) List<int> power_play_overs2,
@Default([]) List<int> power_play_overs3,
Expand Down Expand Up @@ -139,6 +142,9 @@ class AddEditMatchRequest with _$AddEditMatchRequest {
required List<AddMatchTeamRequest> teams,
required MatchType match_type,
required int number_of_over,
@Default([]) List<String> players,
@Default([]) List<String> team_ids,
@Default([]) List<String> team_creator_ids,
required int over_per_bowler,
List<int>? power_play_overs1,
List<int>? power_play_overs2,
Expand Down
182 changes: 180 additions & 2 deletions data/lib/api/match/match_model.freezed.dart

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions data/lib/api/match/match_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

235 changes: 109 additions & 126 deletions data/lib/service/match/match_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,6 @@ class MatchService {
this._currentUserId,
);

Future<List<MatchModel>> getMatches() async {
try {
CollectionReference matchCollection =
_firestore.collection(FireStoreConst.matchesCollection);

QuerySnapshot mainCollectionSnapshot = await matchCollection.get();
List<MatchModel> matches = [];

for (QueryDocumentSnapshot mainDoc in mainCollectionSnapshot.docs) {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);

List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}

return matches;
} catch (error, stack) {
throw AppError.fromError(error, stack);
}
}

Stream<List<MatchModel>> getCurrentUserPlayedMatches() {
if (_currentUserId == null) {
return Stream.value([]);
Expand All @@ -83,38 +44,44 @@ class MatchService {
StreamController<List<MatchModel>> controller =
StreamController<List<MatchModel>>();

_firestore.collection(FireStoreConst.matchesCollection).snapshots().listen(
(QuerySnapshot<Map<String, dynamic>> snapshot) async {
List<MatchModel> matches = [];
_firestore
.collection(FireStoreConst.matchesCollection)
.where(
Filter.and(
Filter(FireStoreConst.matchStatus,
isEqualTo: MatchStatus.finish.value),
Filter(FireStoreConst.players, arrayContains: _currentUserId),
),
)
.snapshots()
.listen((QuerySnapshot<Map<String, dynamic>> snapshot) async {
try {
for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
List<MatchModel> matches = [];
matches = await Future.wait(snapshot.docs.map((mainDoc) async {
Map<String, dynamic> mainDocData = mainDoc.data();
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
if (match.teams
.map((e) => e.squad.map((e) => e.id).contains(_currentUserId))
.contains(true) &&
match.match_status == MatchStatus.finish) {
List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}
}
List<MatchTeamModel> teams = await getTeamsList(match.teams);
return MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
players: match.players,
team_ids: match.team_ids,
team_creator_ids: match.team_creator_ids,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
);
}).toList());
controller.add(matches);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
Expand All @@ -134,41 +101,43 @@ class MatchService {
}
StreamController<List<MatchModel>> controller =
StreamController<List<MatchModel>>();

_firestore.collection(FireStoreConst.matchesCollection).snapshots().listen(
(QuerySnapshot<Map<String, dynamic>> snapshot) async {
List<MatchModel> matches = [];

_firestore
.collection(FireStoreConst.matchesCollection)
.where(Filter.or(
Filter(FireStoreConst.createdBy, isEqualTo: _currentUserId),
Filter(FireStoreConst.players, arrayContains: _currentUserId),
Filter(FireStoreConst.teamCreatorIds, arrayContains: _currentUserId),
))
.snapshots()
.listen((QuerySnapshot<Map<String, dynamic>> snapshot) async {
try {
for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
List<MatchModel> matches = [];
matches = await Future.wait(snapshot.docs.map((mainDoc) async {
Map<String, dynamic> mainDocData = mainDoc.data();
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
List<MatchTeamModel> teams = await getTeamsList(match.teams);
if (teams.any((team) =>
team.team.players
?.map((player) => player.id)
.contains(_currentUserId) ??
false || team.team.created_by == _currentUserId)) {
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}
}
return MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
players: match.players,
team_ids: match.team_ids,
team_creator_ids: match.team_creator_ids,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
);
}).toList());

controller.add(matches);
} catch (error, stack) {
controller.addError(AppError.fromError(error, stack));
Expand All @@ -187,36 +156,40 @@ class MatchService {
CollectionReference matchCollection =
_firestore.collection(FireStoreConst.matchesCollection);

QuerySnapshot mainCollectionSnapshot = await matchCollection.get();
QuerySnapshot mainCollectionSnapshot = await matchCollection
.where(FireStoreConst.teamIds, arrayContains: teamId)
.get();

List<MatchModel> matches = [];

for (QueryDocumentSnapshot mainDoc in mainCollectionSnapshot.docs) {
matches =
await Future.wait(mainCollectionSnapshot.docs.map((mainDoc) async {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
AddEditMatchRequest match = AddEditMatchRequest.fromJson(mainDocData);
if (match.teams.map((e) => e.team_id).contains(teamId)) {
List<MatchTeamModel> teams = await getTeamsList(match.teams);
matches.add(MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
));
}
}

List<MatchTeamModel> teams = await getTeamsList(match.teams);
return MatchModel(
id: match.id,
teams: teams,
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
players: match.players,
team_ids: match.team_ids,
team_creator_ids: match.team_creator_ids,
city: match.city,
ground: match.ground,
start_time: match.start_time,
created_by: match.created_by,
ball_type: match.ball_type,
pitch_type: match.pitch_type,
match_status: match.match_status,
toss_winner_id: match.toss_winner_id,
toss_decision: match.toss_decision,
current_playing_team_id: match.current_playing_team_id,
);
}).toList());
return matches;
} catch (error, stack) {
throw AppError.fromError(error, stack);
Expand All @@ -229,15 +202,16 @@ class MatchService {

_firestore
.collection(FireStoreConst.matchesCollection)
.where(FireStoreConst.matchStatus, isEqualTo: 2)
.where(FireStoreConst.matchStatus, isEqualTo: MatchStatus.running.value)
.snapshots()
.listen((QuerySnapshot<Map<String, dynamic>> snapshot) async {
List<MatchModel> matches = [];
try {
for (QueryDocumentSnapshot mainDoc in snapshot.docs) {
Map<String, dynamic> mainDocData =
mainDoc.data() as Map<String, dynamic>;
if (mainDocData[FireStoreConst.matchStatus] == 2) {
if (mainDocData[FireStoreConst.matchStatus] ==
MatchStatus.running.value) {
AddEditMatchRequest match =
AddEditMatchRequest.fromJson(mainDocData);

Expand All @@ -248,6 +222,9 @@ class MatchService {
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
players: match.players,
team_ids: match.team_ids,
team_creator_ids: match.team_creator_ids,
city: match.city,
ground: match.ground,
start_time: match.start_time,
Expand Down Expand Up @@ -306,6 +283,9 @@ class MatchService {
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
players: match.players,
team_ids: match.team_ids,
team_creator_ids: match.team_creator_ids,
city: match.city,
ground: match.ground,
start_time: match.start_time,
Expand Down Expand Up @@ -370,6 +350,9 @@ class MatchService {
match_type: match.match_type,
number_of_over: match.number_of_over,
over_per_bowler: match.over_per_bowler,
players: match.players,
team_ids: match.team_ids,
team_creator_ids: match.team_creator_ids,
city: match.city,
ground: match.ground,
start_time: match.start_time,
Expand Down
2 changes: 2 additions & 0 deletions data/lib/utils/constant/firestore_constant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class FireStoreConst {
static const String matchStatus = "match_status";
static const String tossWinnerId = "toss_winner_id";
static const String tossDecision = "toss_decision";
static const String teamIds = "team_ids";
static const String teamCreatorIds = "team_creator_ids";

// innings field const
static const String matchId = "match_id";
Expand Down
Loading

0 comments on commit 3d730ae

Please sign in to comment.