Skip to content

Commit

Permalink
feat: update bloc and repository layers for better control
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecastrosales committed May 12, 2024
1 parent ff4e89d commit 98869c0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/app/core/shared/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export 'app_assets.dart';
export 'app_datas.dart';
export 'app_keys.dart';
export 'app_urls.dart';
export 'app_values.dart';
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:developer';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:site/app/core/result/result.dart';
Expand All @@ -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,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ enum ContactFailedResult {
tooManyRequests,
unauthorized,
unknown,
error,
}
13 changes: 7 additions & 6 deletions lib/app/features/contact/domain/models/contact_answer.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<String, String>)['message']}',
);
}

Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand Down Expand Up @@ -42,7 +43,8 @@ class ContactCubit extends Cubit<ContactState> {
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',
},
),
);
Expand Down

0 comments on commit 98869c0

Please sign in to comment.