diff --git a/analysis_options.yaml b/analysis_options.yaml index a2cf24c..ab644f4 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -9,24 +9,24 @@ include: package:flutter_lints/flutter.yaml # - lib/app/core/l10n/localizations/** # Generated file # - lib/data/services/firebase/firebase_set_defaults.dart # To use dynamic type (because really need it) -# linter: -# rules: -# - always_declare_return_types -# - always_use_package_imports -# - avoid_dynamic_calls -# - avoid_relative_lib_imports -# - implementation_imports -# - require_trailing_commas -# - sized_box_for_whitespace -# - sized_box_shrink_expand -# - sort_child_properties_last -# - sort_constructors_first -# - prefer_const_constructors -# - prefer_const_constructors_in_immutables -# - prefer_const_literals_to_create_immutables -# - prefer_const_declarations -# - prefer_final_fields -# - prefer_single_quotes +linter: + rules: + - always_declare_return_types + - always_use_package_imports + - avoid_dynamic_calls + - avoid_relative_lib_imports + - implementation_imports + - require_trailing_commas + - sized_box_for_whitespace + - sized_box_shrink_expand + - sort_child_properties_last + - sort_constructors_first + - prefer_const_constructors + - prefer_const_constructors_in_immutables + - prefer_const_literals_to_create_immutables + - prefer_const_declarations + - prefer_final_fields + - prefer_single_quotes # dart_code_metrics: # metrics: diff --git a/lib/app/features/contact/data/data.dart b/lib/app/features/contact/data/data.dart index d08598b..5066431 100644 --- a/lib/app/features/contact/data/data.dart +++ b/lib/app/features/contact/data/data.dart @@ -1 +1,2 @@ export 'repositories/repositories.dart'; +export 'results/results.dart'; \ No newline at end of file diff --git a/lib/app/features/contact/data/repositories/contact_repository_impl.dart b/lib/app/features/contact/data/repositories/contact_repository_impl.dart index daadc72..3dfba86 100644 --- a/lib/app/features/contact/data/repositories/contact_repository_impl.dart +++ b/lib/app/features/contact/data/repositories/contact_repository_impl.dart @@ -1,6 +1,7 @@ import 'dart:developer'; import 'package:dio/dio.dart'; +import 'package:site/app/core/result/result.dart'; import 'package:site/app/features/contact/contact.dart'; import 'package:site/data/constants/constants_api.dart'; @@ -13,18 +14,21 @@ class ContactRepositoryImpl implements ContactRepository { final Dio _httpClient; @override - Future sendMail({ - required Contact contact, + Future> sendMail({ + required ContactModel contact, }) async { try { final response = await _httpClient.post( ConstantsAPI.apiSendMail, - data: Contact.toJson(contact), + data: ContactUser.toJson(contact), ); - return response.data; + // return response.data; + return Success(ContactAnswer.fromJson(response.data)); } catch (e, s) { log('Error: $e', stackTrace: s); + + return const Failure(ContactFailedResult.unknown); } } } diff --git a/lib/app/features/contact/data/results/contact_failed_result.dart b/lib/app/features/contact/data/results/contact_failed_result.dart new file mode 100644 index 0000000..13bc4af --- /dev/null +++ b/lib/app/features/contact/data/results/contact_failed_result.dart @@ -0,0 +1,5 @@ +enum ContactFailedResult { + tooManyRequests, + unauthorized, + unknown, +} diff --git a/lib/app/features/contact/data/results/results.dart b/lib/app/features/contact/data/results/results.dart new file mode 100644 index 0000000..5380ab9 --- /dev/null +++ b/lib/app/features/contact/data/results/results.dart @@ -0,0 +1 @@ +export 'contact_failed_result.dart'; diff --git a/lib/app/features/contact/domain/models/contact.dart b/lib/app/features/contact/domain/models/contact.dart deleted file mode 100644 index 38f00ad..0000000 --- a/lib/app/features/contact/domain/models/contact.dart +++ /dev/null @@ -1,23 +0,0 @@ -import 'dart:convert'; - -class Contact { - Contact({ - required this.name, - required this.email, - required this.message, - required this.subject, - }); - - final String name, email, message, subject; - - static Map toJson(Contact contact) { - return { - 'sender_name': contact.name, - 'source_email': contact.email, - 'template_subject': contact.subject, - 'template_body': contact.message, - }; - } - - static String toJsonString(Contact contact) => jsonEncode(toJson(contact)); -} diff --git a/lib/app/features/contact/domain/models/contact_answer.dart b/lib/app/features/contact/domain/models/contact_answer.dart new file mode 100644 index 0000000..760a24a --- /dev/null +++ b/lib/app/features/contact/domain/models/contact_answer.dart @@ -0,0 +1,26 @@ +import 'package:site/app/features/contact/domain/models/contact_model.dart'; + +class ContactAnswer extends ContactModel { + factory ContactAnswer.fromJson(Map json) { + return ContactAnswer( + name: json['sender_name'], + email: json['source_email'], + message: json['template_body'], + subject: json['template_subject'], + response: json['response'], + status: json['status'], + ); + } + + ContactAnswer({ + required super.name, + required super.email, + required super.message, + required super.subject, + required this.response, + required this.status, + }); + + final String response; + final String status; +} diff --git a/lib/app/features/contact/domain/models/contact_model.dart b/lib/app/features/contact/domain/models/contact_model.dart new file mode 100644 index 0000000..7246294 --- /dev/null +++ b/lib/app/features/contact/domain/models/contact_model.dart @@ -0,0 +1,10 @@ +abstract class ContactModel { + ContactModel({ + required this.name, + required this.email, + required this.message, + required this.subject, + }); + + final String name, email, message, subject; +} diff --git a/lib/app/features/contact/domain/models/contact_user.dart b/lib/app/features/contact/domain/models/contact_user.dart new file mode 100644 index 0000000..0aa423d --- /dev/null +++ b/lib/app/features/contact/domain/models/contact_user.dart @@ -0,0 +1,24 @@ +import 'dart:convert'; + +import 'package:site/app/features/contact/contact.dart'; + +class ContactUser extends ContactModel { + ContactUser({ + required super.name, + required super.email, + required super.message, + required super.subject, + }); + + static Map toJson(ContactModel contact) { + return { + 'sender_name': contact.name, + 'source_email': contact.email, + 'template_subject': contact.subject, + 'template_body': contact.message, + }; + } + + static String toJsonString(ContactModel contact) => + jsonEncode(toJson(contact)); +} diff --git a/lib/app/features/contact/domain/models/models.dart b/lib/app/features/contact/domain/models/models.dart index 4c72b19..d3da820 100644 --- a/lib/app/features/contact/domain/models/models.dart +++ b/lib/app/features/contact/domain/models/models.dart @@ -1 +1,3 @@ -export 'contact.dart'; +export 'contact_answer.dart'; +export 'contact_user.dart'; +export 'contact_model.dart'; diff --git a/lib/app/features/contact/domain/repositories/contact_repository.dart b/lib/app/features/contact/domain/repositories/contact_repository.dart index 3149b57..c44b821 100644 --- a/lib/app/features/contact/domain/repositories/contact_repository.dart +++ b/lib/app/features/contact/domain/repositories/contact_repository.dart @@ -1,5 +1,8 @@ -import 'package:site/app/features/contact/domain/models/contact.dart'; +import 'package:site/app/core/result/result.dart'; +import 'package:site/app/features/contact/contact.dart'; abstract class ContactRepository { - Future sendMail({required Contact contact}); + Future> sendMail({ + required ContactModel contact, + }); } diff --git a/lib/app/features/contact/presentation/controller/contact_controller.dart b/lib/app/features/contact/presentation/controller/contact_controller.dart index dc7486c..b772287 100644 --- a/lib/app/features/contact/presentation/controller/contact_controller.dart +++ b/lib/app/features/contact/presentation/controller/contact_controller.dart @@ -11,7 +11,7 @@ class ContactController extends ChangeNotifier { final ContactRepositoryImpl _contactRepository; Future sendMail({ - required Contact contact, + required ContactModel contact, }) { return _contactRepository.sendMail( contact: contact, diff --git a/lib/app/features/contact/presentation/cubit/contact_cubit.dart b/lib/app/features/contact/presentation/cubit/contact_cubit.dart index ee2e989..7627775 100644 --- a/lib/app/features/contact/presentation/cubit/contact_cubit.dart +++ b/lib/app/features/contact/presentation/cubit/contact_cubit.dart @@ -16,7 +16,7 @@ class ContactCubit extends Cubit { final ContactRepositoryImpl _contactRepository; Future sendMail({ - required Contact contact, + required ContactModel contact, }) async { emit(const ContactLoading()); diff --git a/lib/app/features/contact/presentation/cubit/contact_state.dart b/lib/app/features/contact/presentation/cubit/contact_state.dart index c566dbe..20690a1 100644 --- a/lib/app/features/contact/presentation/cubit/contact_state.dart +++ b/lib/app/features/contact/presentation/cubit/contact_state.dart @@ -28,7 +28,7 @@ final class ContactSuccess extends ContactState { required this.message, }); - final Contact contact; + final ContactModel contact; final String message; @override @@ -41,7 +41,7 @@ final class ContactError extends ContactState { required this.message, }); - final Contact contact; + final ContactModel contact; final String message; @override diff --git a/lib/app/features/contact/presentation/widgets/form/custom_form.dart b/lib/app/features/contact/presentation/widgets/form/custom_form.dart index 929efa2..32c733a 100644 --- a/lib/app/features/contact/presentation/widgets/form/custom_form.dart +++ b/lib/app/features/contact/presentation/widgets/form/custom_form.dart @@ -2,13 +2,10 @@ import 'package:flutter/material.dart'; import 'package:site/app/core/injections/injections.dart'; import 'package:site/app/core/l10n/l10n.dart'; -import 'package:site/app/features/contact/domain/models/contact.dart'; -import 'package:site/app/features/contact/presentation/controller/contact_controller.dart'; -import 'package:site/app/features/contact/presentation/widgets/widgets.dart'; +import 'package:site/app/features/contact/contact.dart'; import 'package:site/app/utils/contact_validators.dart'; import 'package:site/app/widgets/buttons/buttons.dart'; import 'package:site/app/widgets/dividers/dividers.dart'; -import 'package:site/app/features/contact/data/repositories/contact_repository_impl.dart'; class CustomForm extends StatelessWidget { CustomForm({ @@ -83,7 +80,7 @@ class CustomForm extends StatelessWidget { onPressed: onPressed, onLongPress: () { _contactController?.sendMail( - contact: Contact( + contact: ContactUser( name: 'felipe sales', email: 'soufeliposales@gmail.com', message: 'teste de mensagem', diff --git a/lib/app/features/contact/presentation/widgets/ui/contact_widget.dart b/lib/app/features/contact/presentation/widgets/ui/contact_widget.dart index 76ae4dd..36b271c 100644 --- a/lib/app/features/contact/presentation/widgets/ui/contact_widget.dart +++ b/lib/app/features/contact/presentation/widgets/ui/contact_widget.dart @@ -52,7 +52,7 @@ class ContactWidget extends StatelessWidget { } _contactController?.sendMail( - contact: Contact( + contact: ContactUser( name: nameController.text, email: emailController.text.trim(), message: messageController.text, diff --git a/test/utils/fixtures/app_fixtures.dart b/test/utils/fixtures/app_fixtures.dart index d7d2ede..fee255c 100644 --- a/test/utils/fixtures/app_fixtures.dart +++ b/test/utils/fixtures/app_fixtures.dart @@ -1,7 +1,7 @@ -import 'package:site/app/features/contact/domain/models/contact.dart'; +import 'package:site/app/features/contact/contact.dart'; class AppFixtures { - final tContact = Contact( + final tContact = ContactUser( name: 'felipecastrosales', email: 'fakeemail@gmail.com', message: 'Hello, World!',