From 98869c05b9d4f01e31514468a2ec5be70c97340c Mon Sep 17 00:00:00 2001 From: Felipe Sales Date: Sun, 12 May 2024 11:59:34 -0300 Subject: [PATCH] feat: update bloc and repository layers for better control --- lib/app/core/shared/shared.dart | 1 + .../repositories/contact_repository_impl.dart | 28 +++++++++++++------ .../data/results/contact_failed_result.dart | 1 + .../contact/domain/models/contact_answer.dart | 13 +++++---- .../presentation/cubit/contact_cubit.dart | 4 ++- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/app/core/shared/shared.dart b/lib/app/core/shared/shared.dart index 07d8ef82..9243f7a9 100644 --- a/lib/app/core/shared/shared.dart +++ b/lib/app/core/shared/shared.dart @@ -2,3 +2,4 @@ export 'app_assets.dart'; export 'app_datas.dart'; export 'app_keys.dart'; export 'app_urls.dart'; +export 'app_values.dart'; 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 99be92a9..2cc20b75 100644 --- a/lib/app/features/contact/data/repositories/contact_repository_impl.dart +++ b/lib/app/features/contact/data/repositories/contact_repository_impl.dart @@ -1,4 +1,5 @@ import 'dart:developer'; +import 'dart:io'; import 'package:dio/dio.dart'; import 'package:site/app/core/result/result.dart'; @@ -18,22 +19,33 @@ class ContactRepositoryImpl implements ContactRepository { required ContactModel contact, }) async { try { - final Response response = await _httpClient.post( + final response = await _httpClient.post( ConstantsAPI.apiSendMail, data: ContactUser.toJson(contact), ); - return Success( - ContactAnswer.fromResponse( - contact: contact, - response: response, - ), - ); + return switch (response.statusCode) { + HttpStatus.ok => Success( + ContactAnswer.fromResponse( + contact: contact, + response: response, + ), + ), + HttpStatus.unauthorized => const Failure( + ContactFailedResult.unauthorized, + ), + HttpStatus.tooManyRequests => const Failure( + ContactFailedResult.tooManyRequests, + ), + _ => const Failure( + ContactFailedResult.unknown, + ), + }; } catch (e, s) { log('[Error]: ContactRepositoryImpl.sendMail', error: e, stackTrace: s); return const Failure( - ContactFailedResult.unknown, + ContactFailedResult.error, ); } } diff --git a/lib/app/features/contact/data/results/contact_failed_result.dart b/lib/app/features/contact/data/results/contact_failed_result.dart index 13bc4af6..58ba4d71 100644 --- a/lib/app/features/contact/data/results/contact_failed_result.dart +++ b/lib/app/features/contact/data/results/contact_failed_result.dart @@ -2,4 +2,5 @@ enum ContactFailedResult { tooManyRequests, unauthorized, unknown, + error, } diff --git a/lib/app/features/contact/domain/models/contact_answer.dart b/lib/app/features/contact/domain/models/contact_answer.dart index 5833be33..488a81c0 100644 --- a/lib/app/features/contact/domain/models/contact_answer.dart +++ b/lib/app/features/contact/domain/models/contact_answer.dart @@ -1,4 +1,5 @@ import 'package:dio/dio.dart'; +import 'package:site/app/core/shared/shared.dart'; import 'package:site/app/features/contact/domain/models/contact_model.dart'; class ContactAnswer extends ContactModel { @@ -11,8 +12,8 @@ class ContactAnswer extends ContactModel { email: contact.email, message: contact.message, subject: contact.subject, - status: response.statusMessage ?? '', - response: '${response.statusCode}', + statusCode: response.statusCode ?? AppValues.invalidValue, + responseMessage: '${(response.data as Map)['message']}', ); } @@ -21,10 +22,10 @@ class ContactAnswer extends ContactModel { required super.email, required super.message, required super.subject, - required this.status, - required this.response, + required this.statusCode, + required this.responseMessage, }); - final String status; - final String response; + final int statusCode; + final String responseMessage; } diff --git a/lib/app/features/contact/presentation/cubit/contact_cubit.dart b/lib/app/features/contact/presentation/cubit/contact_cubit.dart index cd02b866..6a3d3df1 100644 --- a/lib/app/features/contact/presentation/cubit/contact_cubit.dart +++ b/lib/app/features/contact/presentation/cubit/contact_cubit.dart @@ -1,4 +1,5 @@ import 'dart:developer'; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -42,7 +43,8 @@ class ContactCubit extends Cubit { ContactFailedResult.unauthorized => 'Unauthorized', ContactFailedResult.tooManyRequests => 'Too many requests, try again later', - ContactFailedResult.unknown => 'Error sending email', + ContactFailedResult.unknown => 'Unknown error', + ContactFailedResult.error => 'Error sending email', }, ), );