Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove bloc and add change notifier with command handler #31

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion design_system/lib/src/drawer/drawer_ds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CustomDrawerDS extends StatelessWidget {
),
DrawerItemDS(
title: 'Sair',
onTap: onAccountTap ?? () {},
onTap: onLogoutTap ?? () {},
showLeading: false,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:developer';
import 'package:result_dart/result_dart.dart';

import '../../../../../core/client_http/app_response.dart';
import '../../../../../core/client_http/client_http.dart';
import '../../../../../core/errors/errors.dart';
import '../../../../../core/errors/unauthorized_exception.dart';
import '../../../../../core/typedefs/types.dart';
Expand All @@ -25,7 +26,7 @@ class AuthRepositoryImpl implements IAuthRepository {
}) : _authRemoteDatasource = authRemoteDatasource;

@override
Future<Output<AppResponse<AuthEntity>>> login(
Output<AppResponse<AuthEntity>> login(
LoginParams params,
) async {
try {
Expand All @@ -35,41 +36,31 @@ class AuthRepositoryImpl implements IAuthRepository {

log('Response: ${response.data}');

if (response.statusCode == 200) {
final appResponse = AppResponse<AuthEntity>.fromJson(
response.data,
(dynamic json) => AuthModel.fromMap(json as Map<String, dynamic>),
);

return Success(appResponse);
}

if (response.statusCode == 401) {
return Failure(
UnauthorizedException(
message: response.data['message'],
),
);
}
final appResponse = AppResponse<AuthEntity>.fromJson(
response.data,
(dynamic json) => AuthModel.fromMap(json as Map<String, dynamic>),
);

return Success(appResponse);
} on RestClientException catch (e) {
return Failure(
ServerException(
message: 'Error',
error: response.data['message'],
message: e.data['message'],
error: e.toString(),
),
);
} catch (e) {
return Failure(
ServerException(
message: 'Error',
message: 'Unexpected error',
error: e.toString(),
),
);
}
}

@override
Future<Output<AppResponse<UserEntity>>> signUp(RegisterParams params) async {
Output<AppResponse<UserEntity>> signUp(RegisterParams params) async {
try {
final response = await _authRemoteDatasource.register(RegisterModel(
photoUrl: 'url',
Expand All @@ -83,25 +74,23 @@ class AuthRepositoryImpl implements IAuthRepository {
password: params.password,
));

if (response.statusCode == 201) {
final appResponse = AppResponse<UserEntity>.fromJson(
response.data,
(dynamic json) => UserModel.fromMap(json as Map<String, dynamic>),
);

return Success(appResponse);
}
final appResponse = AppResponse<UserEntity>.fromJson(
response.data,
(dynamic json) => UserModel.fromMap(json as Map<String, dynamic>),
);

return Success(appResponse);
} on RestClientException catch (e) {
return Failure(
ServerException(
message: 'Error',
error: response.data['message'],
message: e.data['message'],
error: e.toString(),
),
);
} catch (e) {
return Failure(
ServerException(
message: 'Error',
message: 'Unexpected error',
error: e.toString(),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import '../entities/auth_entity.dart';
import '../entities/user_entity.dart';

abstract interface class IAuthRepository {
Future<Output<AppResponse<AuthEntity>>> login(LoginParams params);
Future<Output<AppResponse<UserEntity>>> signUp(RegisterParams params);
Output<AppResponse<AuthEntity>> login(LoginParams params);
Output<AppResponse<UserEntity>> signUp(RegisterParams params);
}
19 changes: 17 additions & 2 deletions lib/src/app/features/auth/domain/usecases/login_usecase.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import 'dart:developer';

import 'package:result_dart/result_dart.dart';

import '../../../../../core/client_http/app_response.dart';
import '../../../../../core/extensions/lucid_validator_extensions.dart';
import '../../../../../core/services/session_service.dart';
import '../../../../../core/typedefs/types.dart';
import '../../../../../core/usecase/usecase_interface.dart';
import '../dtos/login_params.dart';
import '../entities/auth_entity.dart';
import '../repositories/auth_repository_interface.dart';
import '../validators/login_params_validator.dart';

class LoginUsecase implements UseCase<AppResponse<AuthEntity>, LoginParams> {
final IAuthRepository _authRepository;
Expand All @@ -19,8 +23,19 @@ class LoginUsecase implements UseCase<AppResponse<AuthEntity>, LoginParams> {
_sessionService = sessionService;

@override
Future<Output<AppResponse<AuthEntity>>> call(LoginParams params) async {
final result = await _authRepository.login(params);
Output<AppResponse<AuthEntity>> call(LoginParams params) async {
final validator = LoginParamsValidator();

final result = await validator

/// valida o loginParams
.validateResult(params)

/// converte em um async Result: Future<Result<...>>
.toAsyncResult()

/// executa o respository
.flatMap(_authRepository.login);

final appResponse = result.getOrNull();

Expand Down
25 changes: 25 additions & 0 deletions lib/src/app/features/auth/domain/usecases/logout_usecase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:developer';

import 'package:result_dart/result_dart.dart';

import '../../../../../core/errors/default_exception.dart';
import '../../../../../core/services/session_service.dart';
import '../../../../../core/typedefs/types.dart';
import '../../../../../core/usecase/usecase_interface.dart';

class LogoutUsecase implements UseCase<Unit, Object?> {
final SessionService _sessionService;

LogoutUsecase({required SessionService sessionService})
: _sessionService = sessionService;

@override
Output<Unit> call([_]) async {
final removed = await _sessionService.removeToken();
if (removed) {
log('Remove Token');
return const Success(unit);
}
return const Failure(DefaultException(message: 'Not remove token'));
}
}
19 changes: 17 additions & 2 deletions lib/src/app/features/auth/domain/usecases/sign_up_usecase.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'package:result_dart/result_dart.dart';

import '../../../../../core/client_http/app_response.dart';
import '../../../../../core/extensions/lucid_validator_extensions.dart';
import '../../../../../core/typedefs/types.dart';
import '../../../../../core/usecase/usecase_interface.dart';
import '../dtos/register_params.dart';
import '../entities/user_entity.dart';
import '../repositories/auth_repository_interface.dart';
import '../validators/register_params_validator.dart';

class SignUpUsecase
implements UseCase<AppResponse<UserEntity>, RegisterParams> {
Expand All @@ -14,7 +18,18 @@ class SignUpUsecase
}) : _authRepository = authRepository;

@override
Future<Output<AppResponse<UserEntity>>> call(RegisterParams params) async {
return (await _authRepository.signUp(params));
Output<AppResponse<UserEntity>> call(RegisterParams params) async {
final validator = RegisterParamsValidator();

return await validator

/// valida o registerParams
.validateResult(params)

/// converte em um async Result: Future<Result<...>>
.toAsyncResult()

/// Executa o repository de Sign Up
.flatMap(_authRepository.signUp);
}
}
82 changes: 0 additions & 82 deletions lib/src/app/features/auth/presentation/bloc/auth_bloc.dart

This file was deleted.

30 changes: 0 additions & 30 deletions lib/src/app/features/auth/presentation/bloc/auth_event.dart

This file was deleted.

58 changes: 0 additions & 58 deletions lib/src/app/features/auth/presentation/bloc/auth_state.dart

This file was deleted.

Loading