Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sections collection name #296

Merged
merged 8 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class CrosswordRepository {

final DbClient _dbClient;

static const _sectionsCollection = 'boardChunks';

/// Fetches all sections from the board.
Future<List<BoardSection>> listAllSections() async {
final sections = await _dbClient.listAll('boardSections');
final sections = await _dbClient.listAll(_sectionsCollection);

return sections.map((sectionDoc) {
return BoardSection.fromJson({
Expand All @@ -29,7 +31,7 @@ class CrosswordRepository {
/// Fetches a section by its position.
Future<BoardSection?> findSectionByPosition(int x, int y) async {
final result = await _dbClient.find(
'boardSections',
_sectionsCollection,
{
'position.x': x,
'position.y': y,
Expand All @@ -49,7 +51,7 @@ class CrosswordRepository {
/// Updates a section.
Future<void> updateSection(BoardSection section) async {
await _dbClient.update(
'boardSections',
_sectionsCollection,
DbEntityRecord(
id: section.id,
data: section.toJson()..remove('id'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ void main() {
group('CrosswordRepository', () {
late DbClient dbClient;

const sectionsCollection = 'boardChunks';

setUpAll(() {
registerFallbackValue(_MockDbEntityRecord());
});
Expand All @@ -37,7 +39,7 @@ void main() {
'borderWords': const <dynamic>[],
},
);
when(() => dbClient.listAll('boardSections'))
when(() => dbClient.listAll(sectionsCollection))
.thenAnswer((_) async => [record]);
final repository = CrosswordRepository(dbClient: dbClient);
final sections = await repository.listAllSections();
Expand Down Expand Up @@ -65,7 +67,7 @@ void main() {
);
when(
() => dbClient.find(
'boardSections',
sectionsCollection,
{
'position.x': 1,
'position.y': 1,
Expand All @@ -90,7 +92,7 @@ void main() {
test('findSectionByPosition returns null when empty', () async {
when(
() => dbClient.find(
'boardSections',
sectionsCollection,
{
'position.x': 1,
'position.y': 1,
Expand All @@ -106,7 +108,7 @@ void main() {
test('updateSection updates the section in the db', () async {
when(
() => dbClient.update(
'boardSections',
sectionsCollection,
any(that: isA<DbEntityRecord>()),
),
).thenAnswer((_) async {});
Expand All @@ -121,7 +123,7 @@ void main() {
await repository.updateSection(section);
final captured = verify(
() => dbClient.update(
'boardSections',
sectionsCollection,
captureAny(),
),
).captured.single as DbEntityRecord;
Expand Down Expand Up @@ -168,7 +170,7 @@ void main() {
);
when(
() => dbClient.find(
'boardSections',
sectionsCollection,
{
'position.x': 1,
'position.y': 1,
Expand All @@ -177,7 +179,7 @@ void main() {
).thenAnswer((_) async => [record]);
when(
() => dbClient.update(
'boardSections',
sectionsCollection,
any(that: isA<DbEntityRecord>()),
),
).thenAnswer((_) async {});
Expand All @@ -193,7 +195,7 @@ void main() {
expect(valid, isTrue);
final captured = verify(
() => dbClient.update(
'boardSections',
sectionsCollection,
captureAny(),
),
).captured.single as DbEntityRecord;
Expand Down Expand Up @@ -228,7 +230,7 @@ void main() {
test('answerWord returns false if section does not exist', () async {
when(
() => dbClient.find(
'boardSections',
sectionsCollection,
{
'position.x': 0,
'position.y': 0,
Expand Down
2 changes: 2 additions & 0 deletions packages/board_generator/lib/create_sections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ void main(List<String> args) async {
}
}

print('Uploading answers...');
await crosswordRepository.addAnswers(answers);
print('Added all answers to the database.');

print('Uploading sections...');
await crosswordRepository.addSections(sections);
print('Added all ${sections.length} section to the database.');
}
Expand Down
20 changes: 16 additions & 4 deletions packages/board_generator/lib/src/crossword_repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:dart_firebase_admin/firestore.dart';
import 'package:game_domain/game_domain.dart';

Expand All @@ -11,18 +12,29 @@ class CrosswordRepository {
/// The firestore instance.
final Firestore firestore;

/// Adds a list of word answers to the database.
/// Adds a map of word id: answer to the database.
Future<void> addAnswers(Map<String, String> answers) async {
final answersCollection = firestore.collection('answers');
for (final answer in answers.entries) {
await answersCollection.doc(answer.key).set({'answer': answer.value});

// Firestore has a limit that prevents having a document with more than
// 20000 answers.
const size = 20000;
for (final subset in answers.entries.slices(size)) {
final map = subset.fold<Map<String, String>>(
{},
(previousValue, element) {
previousValue[element.key] = element.value;
return previousValue;
},
);
await answersCollection.add(map);
}
}

/// Adds a list of sections to the database.
Future<void> addSections(List<BoardSection> sections) async {
for (final section in sections) {
await firestore.collection('newBoardSections').add(section.toJson());
await firestore.collection('boardChunks').add(section.toJson());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CrosswordRepository {
CrosswordRepository({
required this.db,
}) {
sectionCollection = db.collection('boardSections');
sectionCollection = db.collection('boardChunks');
}

/// The [FirebaseFirestore] instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main() {
],
borderWords: const [],
);
const sectionsCollection = 'boardSections';
const sectionsCollection = 'boardChunks';

late FirebaseFirestore firebaseFirestore;
late CrosswordRepository crosswordRepository;
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ dev_dependencies:
very_good_analysis: ^5.1.0

dependency_overrides:
# Adds support for wasm while flame_audio does not update this dependency
audioplayers: 6.0.0
flame:
git:
url: https://github.com/flame-engine/flame.git
ref: main
path: packages/flame
# Adds support for wasm while flame_audio does not update this dependency
audioplayers: 6.0.0

flutter:
uses-material-design: true
Expand Down
Loading