Skip to content

Commit

Permalink
Reduce the amount of abstraction in the data layer, improve data search
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellet committed Dec 31, 2023
1 parent 9bed5ae commit 5ceaf54
Show file tree
Hide file tree
Showing 36 changed files with 683 additions and 570 deletions.
10 changes: 9 additions & 1 deletion lib/core/services/s_app.dart → lib/core/service/s_app.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import 'exceptions.dart';
import '../errors/exceptions.dart';

class ServiceNotInitializedException extends AppException {
const ServiceNotInitializedException(super.message);
}

class ServiceAlreadyInitilizedException extends AppException {
const ServiceAlreadyInitilizedException(super.message);
}

abstract class AppService {
const AppService();
Expand Down
9 changes: 0 additions & 9 deletions lib/core/services/exceptions.dart

This file was deleted.

2 changes: 1 addition & 1 deletion lib/core/start/app_startup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:path_provider/path_provider.dart'
import '../../logic/auth/auth_service.dart';
import '../../logic/native/image/s_image_picker.dart';
import '../log/logger.dart';
import '../services/s_app.dart';
import '../service/s_app.dart';
import 'packages/firebase.dart';
import 'packages/flutter_local_notifications.dart';
import 'packages/hydrated_bloc.dart';
Expand Down
3 changes: 1 addition & 2 deletions lib/core/start/packages/firebase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart' show dotenv;
import '../../../firebase_options.dart';
import '../../log/logger.dart';
import '../../services/exceptions.dart';
import '../../services/s_app.dart';
import '../../service/s_app.dart';

class FirebaseService extends AppService {
factory FirebaseService.getInstance() => _instance;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/start/packages/flutter_local_notifications.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

import '../../errors/exceptions.dart';
import '../../services/s_app.dart';
import '../../service/s_app.dart';

class FlutterLocalNotificationsService extends AppService {
factory FlutterLocalNotificationsService.getInstance() => instance;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/start/packages/hydrated_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

import '../../services/s_app.dart';
import '../../service/s_app.dart';

class HydratedBlocService extends AppService {
var _isInitialized = false;
Expand Down
8 changes: 0 additions & 8 deletions lib/data/core/local/database/local_database_repository.dart

This file was deleted.

1 change: 1 addition & 0 deletions lib/data/core/shared/database_operation_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ abstract class DatabaseOperationRepository<Output, CreateInput, UpdateInput,
required int limit,
required int page,
}); // Default -1, page 1
Future<List<Output>> searchAll({required String query});
Future<List<Output>> getAllByIds(Iterable<EntityId> ids);
Future<void> deleteByIds(Iterable<EntityId> ids);
Future<void> updateByIds(Iterable<UpdateInput> entities);
Expand Down
9 changes: 6 additions & 3 deletions lib/data/note_folder/local/note_local_folder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '../../notes/universal/models/m_note.dart';
import '../models/m_note_folder.dart';
import '../note_folder_repository.dart';

class LocalNoteFolderImpl extends NoteFolderRepository {
class LocalNoteFolderImpl extends NotesFolderRepository {
Future<Directory> _getNoteFoldersDirectory() async {
final documentsDirectory = await getApplicationDocumentsDirectory();
final noteFoldersDirectory =
Expand All @@ -16,7 +16,8 @@ class LocalNoteFolderImpl extends NoteFolderRepository {
}

@override
Future<NoteFolder> createFolder({required String folderName}) async {
Future<NoteFolder> createFolder(
{required String folderName, NoteFolder? currentFolder}) async {
final newFolderPath =
path.join((await _getNoteFoldersDirectory()).path, folderName);
await Directory(newFolderPath).create(recursive: true);
Expand All @@ -32,7 +33,9 @@ class LocalNoteFolderImpl extends NoteFolderRepository {

@override
Future<List<NoteFolder>> getNoteFolders() async {
final folders = (await _getNoteFoldersDirectory())
final noteFoldersDirectory = await _getNoteFoldersDirectory();
await noteFoldersDirectory.create(recursive: true);
final folders = noteFoldersDirectory
.listSync()
.map(
(event) => NoteFolder(
Expand Down
5 changes: 3 additions & 2 deletions lib/data/note_folder/note_folder_repository.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import '../notes/universal/models/m_note.dart';
import 'models/m_note_folder.dart';

abstract class NoteFolderRepository {
Future<NoteFolder> createFolder({required String folderName});
abstract class NotesFolderRepository {
Future<NoteFolder> createFolder(
{required String folderName, NoteFolder? currentFolder});
Future<void> deleteFolder({required String folderName});
Future<void> updateFolder({
required String folderName,
Expand Down
6 changes: 0 additions & 6 deletions lib/data/notes/cloud/models/cloud_note_repository.dart

This file was deleted.

96 changes: 64 additions & 32 deletions lib/data/notes/cloud/packages/firebase_cloud_notes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,36 @@ import 'package:cloud_firestore/cloud_firestore.dart' show FirebaseFirestore;

import '../../../../logic/auth/auth_service.dart';
import '../../../core/shared/database_operations_exceptions.dart';
import '../../note_repository.dart';
import '../../universal/models/m_note.dart';
import '../../universal/models/m_note_inputs.dart';
import '../models/cloud_note_repository.dart';
import '../models/m_cloud_note.dart';

class FirebaseCloudNotesImpl extends CloudNotesRepository {
class FirebaseCloudNotesImpl extends NotesRepository {
final _notesCollection = FirebaseFirestore.instance.collection(
CloudNoteProperties.notes,
);

@override
Future<CloudNote> createOne(CreateNoteInput createInput) async {
Future<UniversalNote> insertNote(CreateNoteInput createInput) async {
final result = await _notesCollection.add(
CloudNote.toFirebaseMapFromCreateInput(
input: createInput,
),
);
final currentDate = DateTime.now();
return CloudNote.fromCreateNoteInput(
input: createInput,
cloudId: result.id,
createdAt: currentDate,
updatedAt: currentDate,
return UniversalNote.fromCloudNote(
CloudNote.fromCreateNoteInput(
input: createInput,
cloudId: result.id,
createdAt: currentDate,
updatedAt: currentDate,
),
);
}

@override
Future<void> deleteAll() async {
Future<void> deleteAllNotes() async {
final userId = AuthService.getInstance()
.requireCurrentUser(
'To delete all notes from the cloud, user must be authenticated.',
Expand All @@ -48,7 +51,7 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {
}

@override
Future<void> deleteByIds(Iterable<String> ids) async {
Future<void> deleteNotesByIds(Iterable<String> ids) async {
final batch = FirebaseFirestore.instance.batch();
final notes = await _notesCollection
.where(CloudNoteProperties.noteId, whereIn: ids)
Expand All @@ -60,10 +63,10 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {
}

@override
Future<List<CloudNote>> createMultiples(
Iterable<CreateNoteInput> list) async {
Future<List<UniversalNote>> insertNotes(
Iterable<CreateNoteInput> inputs) async {
final batch = FirebaseFirestore.instance.batch();
final notes = list.map((createInput) {
final notes = inputs.map((createInput) {
final documentId = _notesCollection.doc();
batch.set(
documentId,
Expand All @@ -80,11 +83,11 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {
);
});
await batch.commit();
return notes.toList();
return notes.map(UniversalNote.fromCloudNote).toList();
}

@override
Future<void> deleteOneById(String id) async {
Future<void> deleteNoteById(String id) async {
final userId = AuthService.getInstance().requireCurrentUser().id;
final result = await _notesCollection
.where(CloudNoteProperties.userId, isEqualTo: userId)
Expand All @@ -98,7 +101,7 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {
}

@override
Future<List<CloudNote>> getAll({
Future<List<UniversalNote>> getAllNotes({
required int limit,
required int page,
}) async {
Expand All @@ -118,17 +121,17 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {
)
.get();
if (documents.docs.isEmpty) {
return List.empty();
return [];
}
final notes = documents.docs.map((e) => CloudNote.fromFirebase(
e.data(),
id: e.id,
));
return notes.toList();
return notes.map(UniversalNote.fromCloudNote).toList();
}

@override
Future<List<CloudNote>> getAllByIds(Iterable<String> ids) async {
Future<List<UniversalNote>> getAllNotesByIds(Iterable<String> ids) async {
if (ids.isEmpty) {
return [];
}
Expand Down Expand Up @@ -157,11 +160,11 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {
id: ids.toList()[document.key],
),
);
return notes.toList();
return notes.map(UniversalNote.fromCloudNote).toList();
}

@override
Future<CloudNote?> getOneById(String id) async {
Future<UniversalNote?> getNoteById(String id) async {
final userId = AuthService.getInstance().requireCurrentUser().id;
final result = (await _notesCollection
.where(CloudNoteProperties.userId, isEqualTo: userId)
Expand All @@ -175,17 +178,19 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {
}
if (!result.exists) return null;
final data = result.data();
return CloudNote.fromFirebase(
data,
id: result.id,
return UniversalNote.fromCloudNote(
CloudNote.fromFirebase(
data,
id: result.id,
),
);
}

@override
Future<CloudNote> updateOne(
Future<UniversalNote> updateNote(
UpdateNoteInput updateInput,
) async {
final currentNote = await getOneById(updateInput.noteId);
final currentNote = await getNoteById(updateInput.noteId);
if (currentNote == null) {
throw const DatabaseOperationCannotFindResourcesException(
'We could not find this note to update it.',
Expand All @@ -206,20 +211,19 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {
),
);
}
return CloudNote.fromUpdateNoteInput(
input: updateInput,
cloudId: currentNote.id,
return UniversalNote.fromUpdateInput(
updateInput,
createdAt: currentNote.createdAt,
updatedAt: DateTime.now(),
userId: currentNote.userId,
userId: userId,
);
}

@override
Future<void> updateByIds(Iterable<UpdateNoteInput> entities) async {
Future<void> updateNotesByIds(Iterable<UpdateNoteInput> inputs) async {
final batch = FirebaseFirestore.instance.batch();

for (final noteInput in entities) {
for (final noteInput in inputs) {
final noteDoc = (await _notesCollection
.where(
CloudNoteProperties.noteId,
Expand All @@ -244,4 +248,32 @@ class FirebaseCloudNotesImpl extends CloudNotesRepository {

await batch.commit();
}

@override
Future<Iterable<UniversalNote>> searchAllNotes({
required String searchQuery,
}) async {
final results = await _notesCollection
.where(
CloudNoteProperties.text,
arrayContains: searchQuery,
)
.where(
CloudNoteProperties.title,
arrayContains: searchQuery,
)
.orderBy(
CloudNoteProperties.updatedAt,
descending: true,
)
.get();
if (results.docs.isEmpty) {
return [];
}
return results.docs.map(
(e) => UniversalNote.fromCloudNote(
CloudNote.fromFirebase(e.data(), id: e.id),
),
);
}
}
Loading

0 comments on commit 5ceaf54

Please sign in to comment.