From 59bca9045e98f930ce90eade6995f28a491a99a9 Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Sun, 18 Feb 2024 22:49:11 +0100 Subject: [PATCH] refactor(wyatt_architecture): upgrade usecases to add NoParams --- .../repositories/photo_repository_impl.dart | 4 +- .../local/favorite_local_data_source.dart | 2 +- .../remote/album_remote_data_source.dart | 2 +- .../remote/photo_remote_data_source.dart | 2 +- .../photos/add_photo_to_favorites.dart | 15 +- .../check_if_photo_is_in_favorites.dart | 15 +- .../usecases/photos/display_favorites.dart | 6 +- .../domain/usecases/photos/display_photo.dart | 15 +- .../domain/usecases/photos/open_album.dart | 15 +- .../photos/remove_photo_from_favorites.dart | 15 +- .../usecases/photos/retrieve_all_albums.dart | 15 +- .../wyatt_architecture/lib/src/core/core.dart | 2 + .../lib/src/core/exceptions/exceptions.dart | 4 +- .../extensions/object_extension.dart} | 12 +- .../usecases => core/mixins}/observers.dart | 34 ++-- .../base_data_source.dart | 2 +- .../{repositories => }/base_repository.dart | 0 .../local/base_local_data_source.dart | 26 --- .../src/domain/data_sources/local/local.dart | 17 -- .../remote/base_remote_data_source.dart | 26 --- .../domain/data_sources/remote/remote.dart | 17 -- .../lib/src/domain/domain.dart | 15 +- .../lib/src/domain/entities/entities.dart | 17 -- .../lib/src/domain/{entities => }/entity.dart | 0 .../src/domain/repositories/repositories.dart | 17 -- .../lib/src/domain/usecase.dart | 153 ++++++++++++++++++ .../lib/src/domain/usecases/no_param.dart | 26 --- .../lib/src/domain/usecases/usecase.dart | 100 ------------ .../lib/src/domain/usecases/usecases.dart | 19 --- packages/wyatt_architecture/lib/src/src.dart | 1 + packages/wyatt_architecture/pubspec.yaml | 11 +- 31 files changed, 220 insertions(+), 385 deletions(-) rename packages/wyatt_architecture/lib/src/{domain/data_sources/data_sources.dart => core/extensions/object_extension.dart} (71%) rename packages/wyatt_architecture/lib/src/{domain/usecases => core/mixins}/observers.dart (51%) rename packages/wyatt_architecture/lib/src/{domain/data_sources => data}/base_data_source.dart (96%) rename packages/wyatt_architecture/lib/src/domain/{repositories => }/base_repository.dart (100%) delete mode 100644 packages/wyatt_architecture/lib/src/domain/data_sources/local/base_local_data_source.dart delete mode 100644 packages/wyatt_architecture/lib/src/domain/data_sources/local/local.dart delete mode 100644 packages/wyatt_architecture/lib/src/domain/data_sources/remote/base_remote_data_source.dart delete mode 100644 packages/wyatt_architecture/lib/src/domain/data_sources/remote/remote.dart delete mode 100644 packages/wyatt_architecture/lib/src/domain/entities/entities.dart rename packages/wyatt_architecture/lib/src/domain/{entities => }/entity.dart (100%) delete mode 100644 packages/wyatt_architecture/lib/src/domain/repositories/repositories.dart create mode 100644 packages/wyatt_architecture/lib/src/domain/usecase.dart delete mode 100644 packages/wyatt_architecture/lib/src/domain/usecases/no_param.dart delete mode 100644 packages/wyatt_architecture/lib/src/domain/usecases/usecase.dart delete mode 100644 packages/wyatt_architecture/lib/src/domain/usecases/usecases.dart diff --git a/packages/wyatt_architecture/example/lib/data/repositories/photo_repository_impl.dart b/packages/wyatt_architecture/example/lib/data/repositories/photo_repository_impl.dart index 274502f1..1ab0279b 100644 --- a/packages/wyatt_architecture/example/lib/data/repositories/photo_repository_impl.dart +++ b/packages/wyatt_architecture/example/lib/data/repositories/photo_repository_impl.dart @@ -88,8 +88,8 @@ class PhotoRepositoryImpl extends PhotoRepository { } return Ok(response); } catch (_) { - return const Err( - ClientException('Cannot retrieve all photos from favorites.'), + return Err( + const ClientException('Cannot retrieve all photos from favorites.'), ); } } diff --git a/packages/wyatt_architecture/example/lib/domain/data_sources/local/favorite_local_data_source.dart b/packages/wyatt_architecture/example/lib/domain/data_sources/local/favorite_local_data_source.dart index 587a99ee..eddb1e4d 100644 --- a/packages/wyatt_architecture/example/lib/domain/data_sources/local/favorite_local_data_source.dart +++ b/packages/wyatt_architecture/example/lib/domain/data_sources/local/favorite_local_data_source.dart @@ -17,7 +17,7 @@ import 'package:architecture_example/domain/entities/photo.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; -abstract class FavoriteLocalDataSource extends BaseLocalDataSource { +abstract class FavoriteLocalDataSource extends BaseDataSource { Future addPhotoToFavorites(Photo photo); Future deletePhotoFromFavorites(int id); Future> getAllPhotosFromFavorites(); diff --git a/packages/wyatt_architecture/example/lib/domain/data_sources/remote/album_remote_data_source.dart b/packages/wyatt_architecture/example/lib/domain/data_sources/remote/album_remote_data_source.dart index fbef7f9a..a886c6ab 100644 --- a/packages/wyatt_architecture/example/lib/domain/data_sources/remote/album_remote_data_source.dart +++ b/packages/wyatt_architecture/example/lib/domain/data_sources/remote/album_remote_data_source.dart @@ -17,7 +17,7 @@ import 'package:architecture_example/domain/entities/album.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; -abstract class AlbumRemoteDataSource extends BaseRemoteDataSource { +abstract class AlbumRemoteDataSource extends BaseDataSource { Future getAlbum(int id); Future> getAllAlbums({int? start, int? limit}); } diff --git a/packages/wyatt_architecture/example/lib/domain/data_sources/remote/photo_remote_data_source.dart b/packages/wyatt_architecture/example/lib/domain/data_sources/remote/photo_remote_data_source.dart index 81d03a9a..2f5d3a5c 100644 --- a/packages/wyatt_architecture/example/lib/domain/data_sources/remote/photo_remote_data_source.dart +++ b/packages/wyatt_architecture/example/lib/domain/data_sources/remote/photo_remote_data_source.dart @@ -17,7 +17,7 @@ import 'package:architecture_example/domain/entities/photo.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; -abstract class PhotoRemoteDataSource extends BaseRemoteDataSource { +abstract class PhotoRemoteDataSource extends BaseDataSource { Future getPhoto(int id); Future> getAllPhotos({int? start, int? limit}); Future> getPhotosFromAlbum(int albumId, {int? start, int? limit}); diff --git a/packages/wyatt_architecture/example/lib/domain/usecases/photos/add_photo_to_favorites.dart b/packages/wyatt_architecture/example/lib/domain/usecases/photos/add_photo_to_favorites.dart index ef8a0ec9..7a65835b 100644 --- a/packages/wyatt_architecture/example/lib/domain/usecases/photos/add_photo_to_favorites.dart +++ b/packages/wyatt_architecture/example/lib/domain/usecases/photos/add_photo_to_favorites.dart @@ -14,26 +14,17 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -import 'dart:async'; - import 'package:architecture_example/domain/entities/photo.dart'; import 'package:architecture_example/domain/repositories/photo_repository.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; class AddPhotoToFavorites extends AsyncUseCase> { - AddPhotoToFavorites(this._photoRepository); + const AddPhotoToFavorites(this._photoRepository); final PhotoRepository _photoRepository; @override - FutureOrResult> execute(Photo? params) async { - await _photoRepository.addPhotoToFavorites(params!); + FutureOrResult> execute(Photo params) async { + await _photoRepository.addPhotoToFavorites(params); return _photoRepository.getAllPhotosFromFavorites(); } - - @override - FutureOr onStart(Photo? params) { - if (params == null) { - throw const ClientException('Photo cannot be null'); - } - } } diff --git a/packages/wyatt_architecture/example/lib/domain/usecases/photos/check_if_photo_is_in_favorites.dart b/packages/wyatt_architecture/example/lib/domain/usecases/photos/check_if_photo_is_in_favorites.dart index f0dcfa5c..a9c082df 100644 --- a/packages/wyatt_architecture/example/lib/domain/usecases/photos/check_if_photo_is_in_favorites.dart +++ b/packages/wyatt_architecture/example/lib/domain/usecases/photos/check_if_photo_is_in_favorites.dart @@ -14,23 +14,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -import 'dart:async'; - import 'package:architecture_example/domain/repositories/photo_repository.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; class CheckIfPhotoIsInFavorites extends AsyncUseCase { - CheckIfPhotoIsInFavorites(this._photoRepository); + const CheckIfPhotoIsInFavorites(this._photoRepository); final PhotoRepository _photoRepository; @override - FutureOrResult execute(int? params) async => - _photoRepository.checkIfPhotoIsInFavorites(params!); - - @override - FutureOr onStart(int? params) { - if (params == null) { - throw const ClientException('id cannot be null'); - } - } + FutureOrResult execute(int params) async => + _photoRepository.checkIfPhotoIsInFavorites(params); } diff --git a/packages/wyatt_architecture/example/lib/domain/usecases/photos/display_favorites.dart b/packages/wyatt_architecture/example/lib/domain/usecases/photos/display_favorites.dart index 8bdf027f..61296d12 100644 --- a/packages/wyatt_architecture/example/lib/domain/usecases/photos/display_favorites.dart +++ b/packages/wyatt_architecture/example/lib/domain/usecases/photos/display_favorites.dart @@ -18,12 +18,12 @@ import 'package:architecture_example/domain/entities/photo.dart'; import 'package:architecture_example/domain/repositories/photo_repository.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; -class DisplayFavorites extends AsyncUseCase> { - DisplayFavorites(this._photoRepository); +class DisplayFavorites extends NoParamsAsyncUseCase> { + const DisplayFavorites(this._photoRepository); final PhotoRepository _photoRepository; @override - FutureOrResult> execute(void params) { + FutureOrResult> execute() { final photos = _photoRepository.getAllPhotosFromFavorites(); return photos; } diff --git a/packages/wyatt_architecture/example/lib/domain/usecases/photos/display_photo.dart b/packages/wyatt_architecture/example/lib/domain/usecases/photos/display_photo.dart index 79a06b8a..94697367 100644 --- a/packages/wyatt_architecture/example/lib/domain/usecases/photos/display_photo.dart +++ b/packages/wyatt_architecture/example/lib/domain/usecases/photos/display_photo.dart @@ -14,26 +14,17 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -import 'dart:async'; - import 'package:architecture_example/domain/entities/photo.dart'; import 'package:architecture_example/domain/repositories/photo_repository.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; class DisplayPhoto extends AsyncUseCase { - DisplayPhoto(this._photoRepository); + const DisplayPhoto(this._photoRepository); final PhotoRepository _photoRepository; @override - FutureOrResult execute(int? params) { - final photo = _photoRepository.getPhoto(params!); + FutureOrResult execute(int params) { + final photo = _photoRepository.getPhoto(params); return photo; } - - @override - FutureOr onStart(int? params) { - if (params == null) { - throw const ClientException('id cannot be null'); - } - } } diff --git a/packages/wyatt_architecture/example/lib/domain/usecases/photos/open_album.dart b/packages/wyatt_architecture/example/lib/domain/usecases/photos/open_album.dart index 358e86a2..49ad971a 100644 --- a/packages/wyatt_architecture/example/lib/domain/usecases/photos/open_album.dart +++ b/packages/wyatt_architecture/example/lib/domain/usecases/photos/open_album.dart @@ -14,32 +14,23 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -import 'dart:async'; - import 'package:architecture_example/domain/entities/photo.dart'; import 'package:architecture_example/domain/repositories/photo_repository.dart'; import 'package:architecture_example/domain/usecases/photos/params/query_parameters.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; class OpenAlbum extends AsyncUseCase> { - OpenAlbum(this._photoRepository); + const OpenAlbum(this._photoRepository); final PhotoRepository _photoRepository; @override - FutureOrResult> execute(QueryParameters? params) { + FutureOrResult> execute(QueryParameters params) { final photos = _photoRepository.getPhotosFromAlbum( - params!.albumId, + params.albumId, start: params.start, limit: params.limit, ); return photos; } - - @override - FutureOr onStart(QueryParameters? params) { - if (params == null) { - throw const ClientException('params cannot be null'); - } - } } diff --git a/packages/wyatt_architecture/example/lib/domain/usecases/photos/remove_photo_from_favorites.dart b/packages/wyatt_architecture/example/lib/domain/usecases/photos/remove_photo_from_favorites.dart index e73206b3..99ce521a 100644 --- a/packages/wyatt_architecture/example/lib/domain/usecases/photos/remove_photo_from_favorites.dart +++ b/packages/wyatt_architecture/example/lib/domain/usecases/photos/remove_photo_from_favorites.dart @@ -14,26 +14,17 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -import 'dart:async'; - import 'package:architecture_example/domain/entities/photo.dart'; import 'package:architecture_example/domain/repositories/photo_repository.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; class RemovePhotoFromFavorites extends AsyncUseCase> { - RemovePhotoFromFavorites(this._photoRepository); + const RemovePhotoFromFavorites(this._photoRepository); final PhotoRepository _photoRepository; @override - FutureOrResult> execute(int? params) async { - await _photoRepository.deletePhotoFromFavorites(params!); + FutureOrResult> execute(int params) async { + await _photoRepository.deletePhotoFromFavorites(params); return _photoRepository.getAllPhotosFromFavorites(); } - - @override - FutureOr onStart(int? params) { - if (params == null) { - throw const ClientException('id cannot be null'); - } - } } diff --git a/packages/wyatt_architecture/example/lib/domain/usecases/photos/retrieve_all_albums.dart b/packages/wyatt_architecture/example/lib/domain/usecases/photos/retrieve_all_albums.dart index e88f8a12..08ae48ff 100644 --- a/packages/wyatt_architecture/example/lib/domain/usecases/photos/retrieve_all_albums.dart +++ b/packages/wyatt_architecture/example/lib/domain/usecases/photos/retrieve_all_albums.dart @@ -14,30 +14,21 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -import 'dart:async'; - import 'package:architecture_example/domain/entities/album.dart'; import 'package:architecture_example/domain/repositories/photo_repository.dart'; import 'package:architecture_example/domain/usecases/photos/params/query_parameters.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart'; class RetrieveAllAlbums extends AsyncUseCase> { - RetrieveAllAlbums(this._photoRepository); + const RetrieveAllAlbums(this._photoRepository); final PhotoRepository _photoRepository; @override - FutureOrResult> execute(QueryParameters? params) { + FutureOrResult> execute(QueryParameters params) { final albums = _photoRepository.getAllAlbums( - start: params!.start, + start: params.start, limit: params.limit, ); return albums; } - - @override - FutureOr onStart(QueryParameters? params) { - if (params == null) { - throw const ClientException('params cannot be null'); - } - } } diff --git a/packages/wyatt_architecture/lib/src/core/core.dart b/packages/wyatt_architecture/lib/src/core/core.dart index 122e2dfa..2d68b527 100644 --- a/packages/wyatt_architecture/lib/src/core/core.dart +++ b/packages/wyatt_architecture/lib/src/core/core.dart @@ -15,3 +15,5 @@ // along with this program. If not, see . export 'exceptions/exceptions.dart'; +export 'extensions/object_extension.dart'; +export 'mixins/observers.dart'; diff --git a/packages/wyatt_architecture/lib/src/core/exceptions/exceptions.dart b/packages/wyatt_architecture/lib/src/core/exceptions/exceptions.dart index e1fa8e9a..7bbda2ab 100644 --- a/packages/wyatt_architecture/lib/src/core/exceptions/exceptions.dart +++ b/packages/wyatt_architecture/lib/src/core/exceptions/exceptions.dart @@ -14,8 +14,6 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -import 'package:wyatt_type_utils/wyatt_type_utils.dart'; - /// {@template app_exception} /// [AppException] is a base class for all exceptions in the wyatt architecture. /// {@endtemplate} @@ -27,7 +25,7 @@ abstract class AppException implements Exception { @override String toString() { - if (message.isNotNullOrEmpty) { + if (message?.isNotEmpty ?? false) { return '$runtimeType: $message'; } else { return '$runtimeType: An exception occured'; diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/data_sources.dart b/packages/wyatt_architecture/lib/src/core/extensions/object_extension.dart similarity index 71% rename from packages/wyatt_architecture/lib/src/domain/data_sources/data_sources.dart rename to packages/wyatt_architecture/lib/src/core/extensions/object_extension.dart index 17ffc5c8..b0ea8174 100644 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/data_sources.dart +++ b/packages/wyatt_architecture/lib/src/core/extensions/object_extension.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 2024 WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -14,6 +14,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -export 'base_data_source.dart'; -export 'local/local.dart'; -export 'remote/remote.dart'; +import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart'; + +extension ObjectExtension on Object? { + AppException toException() => this is AppException + ? this! as AppException + : ClientException(this?.toString()); +} diff --git a/packages/wyatt_architecture/lib/src/domain/usecases/observers.dart b/packages/wyatt_architecture/lib/src/core/mixins/observers.dart similarity index 51% rename from packages/wyatt_architecture/lib/src/domain/usecases/observers.dart rename to packages/wyatt_architecture/lib/src/core/mixins/observers.dart index a1925577..62e87b91 100644 --- a/packages/wyatt_architecture/lib/src/domain/usecases/observers.dart +++ b/packages/wyatt_architecture/lib/src/core/mixins/observers.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 2024 WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -16,30 +16,26 @@ import 'dart:async'; -import 'package:wyatt_architecture/wyatt_architecture.dart'; +import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart'; +import 'package:wyatt_architecture/src/domain/usecase.dart'; /// Usecase observers -mixin Observer { +mixin Observer { /// Called before usecase is runned. /// Useful to check the preconditions - FutureOr onStart(Parameters? params) {} - - /// Called when error occures during main scenario - /// Useful to run alternative scenario - FutureOr onError(AppException? error) {} + FutureOr onStart(I? params) {} } -/// Specific observer for classic usecase -mixin AsyncObserver { - /// Called when usecase is completed - FutureOr onComplete(ReturnType? data) {} +/// Exception observer +mixin ExceptionObserver { + /// Called on error + /// Useful to handle exceptions + FutureOr onError(AppException e) {} } -/// Specific observer for stream case usecase -mixin StreamObserver { - /// Replaces the data event handler of this subscription. - void onDone() {} - - /// Replaces the done event handler of this subscription. - void onData(ReturnType? data) {} +/// Completer observer +mixin CompleterObserver { + /// Called after usecase is runned. + /// Useful to log the result + FutureOr onComplete(Res? result) {} } diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/base_data_source.dart b/packages/wyatt_architecture/lib/src/data/base_data_source.dart similarity index 96% rename from packages/wyatt_architecture/lib/src/domain/data_sources/base_data_source.dart rename to packages/wyatt_architecture/lib/src/data/base_data_source.dart index f5002603..0b73575b 100644 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/base_data_source.dart +++ b/packages/wyatt_architecture/lib/src/data/base_data_source.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 2024 WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify diff --git a/packages/wyatt_architecture/lib/src/domain/repositories/base_repository.dart b/packages/wyatt_architecture/lib/src/domain/base_repository.dart similarity index 100% rename from packages/wyatt_architecture/lib/src/domain/repositories/base_repository.dart rename to packages/wyatt_architecture/lib/src/domain/base_repository.dart diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/local/base_local_data_source.dart b/packages/wyatt_architecture/lib/src/domain/data_sources/local/base_local_data_source.dart deleted file mode 100644 index b091a13f..00000000 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/local/base_local_data_source.dart +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'package:wyatt_architecture/src/domain/data_sources/base_data_source.dart'; - -/// {@template base_local_data_source} -/// [BaseLocalDataSource] is a base class for all local data sources in the -/// wyatt architecture. -/// {@endtemplate} -abstract class BaseLocalDataSource extends BaseDataSource { - /// {@macro base_local_data_source} - const BaseLocalDataSource(); -} diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/local/local.dart b/packages/wyatt_architecture/lib/src/domain/data_sources/local/local.dart deleted file mode 100644 index e45c6a57..00000000 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/local/local.dart +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -export 'base_local_data_source.dart'; diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/remote/base_remote_data_source.dart b/packages/wyatt_architecture/lib/src/domain/data_sources/remote/base_remote_data_source.dart deleted file mode 100644 index 8ed880c4..00000000 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/remote/base_remote_data_source.dart +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'package:wyatt_architecture/src/domain/data_sources/base_data_source.dart'; - -/// {@template base_remote_data_source} -/// [BaseRemoteDataSource] is a base class for all remote data sources in the -/// wyatt architecture. -/// {@endtemplate} -abstract class BaseRemoteDataSource extends BaseDataSource { - /// {@macro base_remote_data_source} - const BaseRemoteDataSource(); -} diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/remote/remote.dart b/packages/wyatt_architecture/lib/src/domain/data_sources/remote/remote.dart deleted file mode 100644 index 7403e595..00000000 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/remote/remote.dart +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -export 'base_remote_data_source.dart'; diff --git a/packages/wyatt_architecture/lib/src/domain/domain.dart b/packages/wyatt_architecture/lib/src/domain/domain.dart index 35c41307..4b157e93 100644 --- a/packages/wyatt_architecture/lib/src/domain/domain.dart +++ b/packages/wyatt_architecture/lib/src/domain/domain.dart @@ -1,20 +1,19 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 2024 WYATT GROUP // Please see the AUTHORS file for details. -// +// // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. -// +// // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -// +// // You should have received a copy of the GNU General Public License // along with this program. If not, see . -export 'data_sources/data_sources.dart'; -export 'entities/entities.dart'; -export 'repositories/repositories.dart'; -export 'usecases/usecases.dart'; +export 'base_repository.dart'; +export 'entity.dart'; +export 'usecase.dart'; diff --git a/packages/wyatt_architecture/lib/src/domain/entities/entities.dart b/packages/wyatt_architecture/lib/src/domain/entities/entities.dart deleted file mode 100644 index 937b195e..00000000 --- a/packages/wyatt_architecture/lib/src/domain/entities/entities.dart +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -export 'entity.dart'; diff --git a/packages/wyatt_architecture/lib/src/domain/entities/entity.dart b/packages/wyatt_architecture/lib/src/domain/entity.dart similarity index 100% rename from packages/wyatt_architecture/lib/src/domain/entities/entity.dart rename to packages/wyatt_architecture/lib/src/domain/entity.dart diff --git a/packages/wyatt_architecture/lib/src/domain/repositories/repositories.dart b/packages/wyatt_architecture/lib/src/domain/repositories/repositories.dart deleted file mode 100644 index bac1ea4c..00000000 --- a/packages/wyatt_architecture/lib/src/domain/repositories/repositories.dart +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -export 'base_repository.dart'; diff --git a/packages/wyatt_architecture/lib/src/domain/usecase.dart b/packages/wyatt_architecture/lib/src/domain/usecase.dart new file mode 100644 index 00000000..08c0db10 --- /dev/null +++ b/packages/wyatt_architecture/lib/src/domain/usecase.dart @@ -0,0 +1,153 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'dart:async'; + +import 'package:generic_usecase/generic_usecase.dart' as generic; +import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart'; +import 'package:wyatt_architecture/src/core/extensions/object_extension.dart'; +import 'package:wyatt_architecture/src/core/mixins/observers.dart'; +import 'package:wyatt_type_utils/wyatt_type_utils.dart'; + +typedef Res = Result; +typedef FutureOrResult = FutureOr>; +typedef StreamResult = Stream>; + +/// A wrapper around [Result] to make it easier to use with AppException. +Future> unsafe(FutureOr Function() fn) => + Result.tryCatchAsync( + () => Future.sync(fn), + (e) => e.toException(), + ); + +abstract class AsyncUseCase extends generic.Usecase> + with Observer, ExceptionObserver, CompleterObserver { + const AsyncUseCase() : super(); + + @override + FutureOr checkPreconditions(I? params) async { + try { + await onStart(params); + return generic.ConditionsResult(isValid: true); + } catch (e) { + return generic.ConditionsResult(isValid: false, message: e.toString()); + } + } + + @override + FutureOr checkPostconditions(Res? result) async { + try { + await onComplete(result); + return generic.ConditionsResult(isValid: true); + } catch (e) { + return generic.ConditionsResult(isValid: false, message: e.toString()); + } + } + + @override + FutureOrResult onException(Object e) async { + final exception = e.toException(); + await onError(exception); + + return Err(exception); + } +} + +abstract class NoParamsAsyncUseCase extends generic.NoParamsUsecase> + with ExceptionObserver, CompleterObserver { + const NoParamsAsyncUseCase() : super(); + + @override + FutureOr checkPostconditions(Res? result) async { + try { + await onComplete(result); + return generic.ConditionsResult(isValid: true); + } catch (e) { + return generic.ConditionsResult(isValid: false, message: e.toString()); + } + } + + @override + FutureOrResult onException(Object e) async { + final exception = e.toException(); + await onError(exception); + + return Err(exception); + } +} + +abstract class StreamUseCase extends generic.StreamUsecase> + with Observer, ExceptionObserver { + const StreamUseCase() : super(); + + @override + FutureOr checkPreconditions(I? params) async { + try { + await onStart(params); + return generic.ConditionsResult(isValid: true); + } catch (e) { + return generic.ConditionsResult(isValid: false, message: e.toString()); + } + } + + StreamSubscription> listen( + I params, + void Function(Res)? onData, { + Function? onError, + void Function()? onDone, + bool? cancelOnError, + }) => + super.call(params).listen( + onData, + onError: onError, + onDone: onDone, + cancelOnError: cancelOnError, + ); + + @override + FutureOrResult onException(Object e) async { + final exception = e.toException(); + await onError(exception); + + return Err(exception); + } +} + +abstract class NoParamsStreamUseCase + extends generic.NoParamsStreamUsecase> with ExceptionObserver { + const NoParamsStreamUseCase() : super(); + + StreamSubscription> listen( + void Function(Res)? onData, { + Function? onError, + void Function()? onDone, + bool? cancelOnError, + }) => + super.call().listen( + onData, + onError: onError, + onDone: onDone, + cancelOnError: cancelOnError, + ); + + @override + FutureOrResult onException(Object e) async { + final exception = e.toException(); + await onError(exception); + + return Err(exception); + } +} diff --git a/packages/wyatt_architecture/lib/src/domain/usecases/no_param.dart b/packages/wyatt_architecture/lib/src/domain/usecases/no_param.dart deleted file mode 100644 index 4509bd59..00000000 --- a/packages/wyatt_architecture/lib/src/domain/usecases/no_param.dart +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'package:wyatt_architecture/src/domain/entities/entity.dart'; - -/// {@template no_param} -/// [NoParam] is a class that is used when a use case does not require any -/// parameters. -/// {@endtemplate} -class NoParam extends Entity { - /// {@macro no_param} - const NoParam(); -} diff --git a/packages/wyatt_architecture/lib/src/domain/usecases/usecase.dart b/packages/wyatt_architecture/lib/src/domain/usecases/usecase.dart deleted file mode 100644 index 75c4c905..00000000 --- a/packages/wyatt_architecture/lib/src/domain/usecases/usecase.dart +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'dart:async'; - -import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart'; -import 'package:wyatt_architecture/src/domain/usecases/observers.dart'; -import 'package:wyatt_type_utils/wyatt_type_utils.dart'; - -typedef FutureOrResult = FutureOr>; -typedef StreamResult = Stream>; - -/// {@template base_usecase} -/// Abstract class of any use case. -/// {@endtemplate} -abstract class BaseUseCase { - /// {@macro base_usecase} - const BaseUseCase(); - - /// Run use case scenarios - ReturnType call(Parameters parameters); - - /// Private function to implement main scenario - /// of your usecase. - ReturnType execute(Parameters params); -} - -/// {@template usecase} -/// Abstract class of a use case that deals specifically -/// with the response and its state. -/// {@endtemplate} -abstract class UseCase - extends BaseUseCase> - with Observer { - /// {@macro usecase} - const UseCase(); - - FutureOr _onSuccess(ReturnType data); - - /// Supports the result of the main scenario and integrates - /// some alternative scenarios if necessary. - @override - FutureOrResult call(Parameters? parameters) async { - try { - await onStart(parameters); - final response = await execute(parameters); - if (response.isErr) { - await onError(response.err); - } else if (response.isOk && response.ok != null) { - await _onSuccess(response.ok as ReturnType); - } - return response; - } catch (e) { - return Err(ClientException(e.toString())); - } - } -} - -/// {@template async_usecase} -/// Abtstract classic usecase bases on futures -/// {@endtemplate} -abstract class AsyncUseCase - extends UseCase with AsyncObserver { - /// {@macro async_usecase} - const AsyncUseCase(); - - @override - FutureOr _onSuccess(ReturnType data) => onComplete(data); -} - -/// {@template stream_usecase} -/// Abstract specific usecase bases on streams -/// {@endtemplate} -abstract class StreamUseCase - extends UseCase> - with StreamObserver { - /// {@macro stream_usecase} - const StreamUseCase(); - - @override - FutureOr _onSuccess(Stream data) { - data.listen( - onData, - onDone: onDone, - ); - } -} diff --git a/packages/wyatt_architecture/lib/src/domain/usecases/usecases.dart b/packages/wyatt_architecture/lib/src/domain/usecases/usecases.dart deleted file mode 100644 index 445b2682..00000000 --- a/packages/wyatt_architecture/lib/src/domain/usecases/usecases.dart +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -export 'no_param.dart'; -export 'observers.dart'; -export 'usecase.dart'; diff --git a/packages/wyatt_architecture/lib/src/src.dart b/packages/wyatt_architecture/lib/src/src.dart index 2a39a026..cda1aa95 100644 --- a/packages/wyatt_architecture/lib/src/src.dart +++ b/packages/wyatt_architecture/lib/src/src.dart @@ -15,4 +15,5 @@ // along with this program. If not, see . export 'core/core.dart'; +export 'data/base_data_source.dart'; export 'domain/domain.dart'; diff --git a/packages/wyatt_architecture/pubspec.yaml b/packages/wyatt_architecture/pubspec.yaml index e3379349..4f6dacba 100644 --- a/packages/wyatt_architecture/pubspec.yaml +++ b/packages/wyatt_architecture/pubspec.yaml @@ -1,16 +1,16 @@ # Copyright (C) 2023 WYATT GROUP # Please see the AUTHORS file for details. -# +# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. -# +# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with this program. If not, see . @@ -22,9 +22,10 @@ version: 0.2.0+1 publish_to: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub environment: - sdk: ">=2.17.0 <3.0.0" + sdk: "^3.0.0" dependencies: + generic_usecase: ^3.0.0 wyatt_type_utils: hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub version: ^0.0.5 @@ -32,4 +33,4 @@ dependencies: dev_dependencies: wyatt_analysis: hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub - version: ^2.5.0 + version: ^2.6.1