diff --git a/packages/wyatt_architecture/lib/src/core/core.dart b/packages/wyatt_architecture/lib/src/core/core.dart index 122e2dfa..1dd64285 100644 --- a/packages/wyatt_architecture/lib/src/core/core.dart +++ b/packages/wyatt_architecture/lib/src/core/core.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 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/core/exceptions/exceptions.dart b/packages/wyatt_architecture/lib/src/core/exceptions/exceptions.dart index e1fa8e9a..1d1fb8e6 100644 --- a/packages/wyatt_architecture/lib/src/core/exceptions/exceptions.dart +++ b/packages/wyatt_architecture/lib/src/core/exceptions/exceptions.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -14,24 +14,43 @@ // 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} abstract class AppException implements Exception { /// {@macro app_exception} - const AppException([this.message]); + const AppException({this.message, this.underlyingException}); + /// {@macro app_exception} + /// + /// Constructs an [AppException] from an [Exception]. + factory AppException.from(Object e) { + if (e is AppException) { + return e; + } else { + return ClientException(underlyingException: e); + } + } + + /// The message of the exception. final String? message; + /// The exception which triggered this exception being thrown. + final Object? underlyingException; + @override String toString() { - if (message.isNotNullOrEmpty) { - return '$runtimeType: $message'; + final str = StringBuffer(runtimeType.toString()); + if (message != null && message!.isNotEmpty) { + str.write(': $message'); } else { - return '$runtimeType: An exception occured'; + str.write(': An exception occured'); } + if (underlyingException != null) { + str.write('\n$underlyingException'); + } + + return str.toString(); } } @@ -41,7 +60,7 @@ abstract class AppException implements Exception { /// {@endtemplate} class ClientException extends AppException { /// {@macro client_exception} - const ClientException([super.message]); + const ClientException({super.message, super.underlyingException}); } /// {@template server_exception} @@ -50,5 +69,5 @@ class ClientException extends AppException { /// {@endtemplate} class ServerException extends AppException { /// {@macro server_exception} - const ServerException([super.message]); + const ServerException({super.message, super.underlyingException}); } diff --git a/packages/wyatt_architecture/lib/src/domain/entities/entities.dart b/packages/wyatt_architecture/lib/src/data/data.dart similarity index 90% rename from packages/wyatt_architecture/lib/src/domain/entities/entities.dart rename to packages/wyatt_architecture/lib/src/data/data.dart index 937b195e..64dc51e3 100644 --- a/packages/wyatt_architecture/lib/src/domain/entities/entities.dart +++ b/packages/wyatt_architecture/lib/src/data/data.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -14,4 +14,4 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -export 'entity.dart'; +export 'data_sources/data_sources.dart'; diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/base_data_source.dart b/packages/wyatt_architecture/lib/src/data/data_sources/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/data_sources/base_data_source.dart index f5002603..95899cb5 100644 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/base_data_source.dart +++ b/packages/wyatt_architecture/lib/src/data/data_sources/base_data_source.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 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/data_sources/data_sources.dart b/packages/wyatt_architecture/lib/src/data/data_sources/data_sources.dart similarity index 84% rename from packages/wyatt_architecture/lib/src/domain/data_sources/data_sources.dart rename to packages/wyatt_architecture/lib/src/data/data_sources/data_sources.dart index 17ffc5c8..fdb1b5f4 100644 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/data_sources.dart +++ b/packages/wyatt_architecture/lib/src/data/data_sources/data_sources.dart @@ -1,19 +1,19 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 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_data_source.dart'; -export 'local/local.dart'; -export 'remote/remote.dart'; +export 'local/base_local_data_source.dart'; +export 'remote/base_remote_data_source.dart'; diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/local/base_local_data_source.dart b/packages/wyatt_architecture/lib/src/data/data_sources/local/base_local_data_source.dart similarity index 89% rename from packages/wyatt_architecture/lib/src/domain/data_sources/local/base_local_data_source.dart rename to packages/wyatt_architecture/lib/src/data/data_sources/local/base_local_data_source.dart index b091a13f..6e4c66fe 100644 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/local/base_local_data_source.dart +++ b/packages/wyatt_architecture/lib/src/data/data_sources/local/base_local_data_source.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -14,7 +14,7 @@ // 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'; +import 'package:wyatt_architecture/src/data/data_sources/base_data_source.dart'; /// {@template base_local_data_source} /// [BaseLocalDataSource] is a base class for all local data sources in the diff --git a/packages/wyatt_architecture/lib/src/domain/data_sources/remote/base_remote_data_source.dart b/packages/wyatt_architecture/lib/src/data/data_sources/remote/base_remote_data_source.dart similarity index 89% rename from packages/wyatt_architecture/lib/src/domain/data_sources/remote/base_remote_data_source.dart rename to packages/wyatt_architecture/lib/src/data/data_sources/remote/base_remote_data_source.dart index 8ed880c4..2d304a70 100644 --- a/packages/wyatt_architecture/lib/src/domain/data_sources/remote/base_remote_data_source.dart +++ b/packages/wyatt_architecture/lib/src/data/data_sources/remote/base_remote_data_source.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -14,7 +14,7 @@ // 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'; +import 'package:wyatt_architecture/src/data/data_sources/base_data_source.dart'; /// {@template base_remote_data_source} /// [BaseRemoteDataSource] is a base class for all remote data sources in the 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/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..76d01ba6 100644 --- a/packages/wyatt_architecture/lib/src/domain/domain.dart +++ b/packages/wyatt_architecture/lib/src/domain/domain.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -14,7 +14,6 @@ // 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 'entities/entity.dart'; +export 'repositories/base_repository.dart'; export 'usecases/usecases.dart'; diff --git a/packages/wyatt_architecture/lib/src/domain/entities/entity.dart b/packages/wyatt_architecture/lib/src/domain/entities/entity.dart index 41a8ca17..f23f3bfa 100644 --- a/packages/wyatt_architecture/lib/src/domain/entities/entity.dart +++ b/packages/wyatt_architecture/lib/src/domain/entities/entity.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 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/repositories/base_repository.dart index a7e26b12..079e0579 100644 --- a/packages/wyatt_architecture/lib/src/domain/repositories/base_repository.dart +++ b/packages/wyatt_architecture/lib/src/domain/repositories/base_repository.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) 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/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/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/observers.dart b/packages/wyatt_architecture/lib/src/domain/usecases/observers.dart deleted file mode 100644 index a1925577..00000000 --- a/packages/wyatt_architecture/lib/src/domain/usecases/observers.dart +++ /dev/null @@ -1,45 +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/wyatt_architecture.dart'; - -/// Usecase observers -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) {} -} - -/// Specific observer for classic usecase -mixin AsyncObserver { - /// Called when usecase is completed - FutureOr onComplete(ReturnType? data) {} -} - -/// 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) {} -} 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 index 445b2682..4c7e6425 100644 --- a/packages/wyatt_architecture/lib/src/domain/usecases/usecases.dart +++ b/packages/wyatt_architecture/lib/src/domain/usecases/usecases.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -14,6 +14,67 @@ // 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'; +import 'dart:async'; + +import 'package:generic_usecase/generic_usecase.dart' as uc; +import 'package:sealed_result/sealed_result.dart'; +import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart'; + +typedef FutureOrResult = FutureOr>; +typedef StreamResult = Stream>; + +/// {@template result_usecase} +/// A usecase that requires params of type [Input] and returns a result of type +/// [Output] or an error of type [AppException]. +/// {@endtemplate} +abstract class Usecase + extends uc.Usecase> { + /// {@macro result_usecase} + const Usecase() : super(); + + @override + FutureOrResult onException(Object e) => + Result.err(AppException.from(e)); +} + +/// {@template no_params_result_usecase} +/// A usecase that does not require any params, but returns a result of type +/// [Output] or an error of type [AppException]. +/// {@endtemplate} +abstract class NoParamsUsecase + extends uc.NoParamsUsecase> { + /// {@macro no_params_result_usecase} + const NoParamsUsecase() : super(); + + @override + FutureOrResult onException(Object e) => + Result.err(AppException.from(e)); +} + +/// {@template result_stream_usecase} +/// A stream usecase that requires params of type [Input] and returns a +/// stream of [Output] or [AppException]. +/// {@endtemplate} +abstract class StreamUsecase + extends uc.StreamUsecase> { + /// {@macro result_stream_usecase} + const StreamUsecase() : super(); + + @override + FutureOrResult onException(Object e) => + Result.err(AppException.from(e)); +} + +/// {@template no_params_result_stream_usecase} +/// A stream usecase that does not require any params, but returns a +/// stream of [Output] or [AppException]. +/// {@endtemplate} +abstract class NoParamsStreamUsecase + extends uc.NoParamsStreamUsecase> { + /// {@macro no_params_result_stream_usecase} + const NoParamsStreamUsecase() : super(); + + @override + FutureOrResult onException(Object e) => + Result.err(AppException.from(e)); +} diff --git a/packages/wyatt_architecture/lib/src/src.dart b/packages/wyatt_architecture/lib/src/src.dart index 2a39a026..85e8a24a 100644 --- a/packages/wyatt_architecture/lib/src/src.dart +++ b/packages/wyatt_architecture/lib/src/src.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify @@ -15,4 +15,5 @@ // along with this program. If not, see . export 'core/core.dart'; +export 'data/data.dart'; export 'domain/domain.dart'; diff --git a/packages/wyatt_architecture/lib/wyatt_architecture.dart b/packages/wyatt_architecture/lib/wyatt_architecture.dart index f07613aa..34d37a85 100644 --- a/packages/wyatt_architecture/lib/wyatt_architecture.dart +++ b/packages/wyatt_architecture/lib/wyatt_architecture.dart @@ -1,4 +1,4 @@ -// Copyright (C) 2022 WYATT GROUP +// Copyright (C) WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify