Init login feature add domain layer
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
|
||||||
|
abstract class AuthenticationException extends Equatable {
|
||||||
|
static final wrongCredential = 'Credential is wrong';
|
||||||
|
|
||||||
|
AuthenticationException(String message);
|
||||||
|
}
|
||||||
|
|
||||||
|
class BadCredentials extends AuthenticationException {
|
||||||
|
BadCredentials() : super(AuthenticationException.wrongCredential);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
//
|
||||||
|
|
||||||
|
extension UriExtension on Uri {
|
||||||
|
bool isBaseUrlValid() => origin.isNotEmpty;
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import 'package:model/model.dart';
|
||||||
|
|
||||||
|
abstract class AuthenticationRepository {
|
||||||
|
Future<User> authenticationUser(Uri baseUrl, UserName userName, Password password);
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import 'package:model/model.dart';
|
||||||
|
|
||||||
|
abstract class CredentialRepository {
|
||||||
|
Future saveBaseUrl(Uri baseUrl);
|
||||||
|
|
||||||
|
Future removeBaseUrl();
|
||||||
|
|
||||||
|
Future<Uri> getBaseUrl();
|
||||||
|
|
||||||
|
Future saveUserName(UserName userName);
|
||||||
|
|
||||||
|
Future removeUserName();
|
||||||
|
|
||||||
|
Future<UserName> getUserName();
|
||||||
|
|
||||||
|
Future savePassword(Password password);
|
||||||
|
|
||||||
|
Future removePassword();
|
||||||
|
|
||||||
|
Future<Password> getPassword();
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import 'package:core/core.dart';
|
||||||
|
import 'package:model/model.dart';
|
||||||
|
|
||||||
|
class AuthenticationUserViewState extends ViewState {
|
||||||
|
final User user;
|
||||||
|
|
||||||
|
AuthenticationUserViewState(this.user);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [user];
|
||||||
|
}
|
||||||
|
|
||||||
|
class AuthenticationUserFailure extends FeatureFailure {
|
||||||
|
final exception;
|
||||||
|
|
||||||
|
AuthenticationUserFailure(this.exception);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [exception];
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import 'package:core/core.dart';
|
||||||
|
import 'package:core/presentation/state/failure.dart';
|
||||||
|
import 'package:model/account/password.dart';
|
||||||
|
import 'package:model/account/user_name.dart';
|
||||||
|
|
||||||
|
class GetCredentialViewState extends ViewState {
|
||||||
|
final Uri baseUrl;
|
||||||
|
final UserName userName;
|
||||||
|
final Password password;
|
||||||
|
|
||||||
|
GetCredentialViewState(this.baseUrl, this.userName, this.password);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [baseUrl, this.userName, this.password];
|
||||||
|
}
|
||||||
|
|
||||||
|
class GetCredentialFailure extends FeatureFailure {
|
||||||
|
final exception;
|
||||||
|
|
||||||
|
GetCredentialFailure(this.exception);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [exception];
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import 'package:core/core.dart';
|
||||||
|
import 'package:dartz/dartz.dart';
|
||||||
|
import 'package:model/model.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/repository/authentication_repository.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/state/authentication_user_state.dart';
|
||||||
|
|
||||||
|
class AuthenticationInteractor {
|
||||||
|
final AuthenticationRepository authenticationRepository;
|
||||||
|
final CredentialRepository credentialRepository;
|
||||||
|
|
||||||
|
AuthenticationInteractor(this.authenticationRepository, this.credentialRepository);
|
||||||
|
|
||||||
|
Future<Either<Failure, Success>> execute(Uri baseUrl, UserName userName, Password password) async {
|
||||||
|
try {
|
||||||
|
final user = await authenticationRepository.authenticationUser(baseUrl, userName, password);
|
||||||
|
await Future.wait([
|
||||||
|
credentialRepository.saveBaseUrl(baseUrl),
|
||||||
|
credentialRepository.saveUserName(userName),
|
||||||
|
credentialRepository.savePassword(password)
|
||||||
|
]);
|
||||||
|
return Right(AuthenticationUserViewState(user));
|
||||||
|
} catch (e) {
|
||||||
|
return Left(AuthenticationUserFailure(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import 'package:core/core.dart';
|
||||||
|
import 'package:dartz/dartz.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
||||||
|
import 'dart:core';
|
||||||
|
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/extensions/uri_extension.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
|
||||||
|
|
||||||
|
class GetCredentialInteractor {
|
||||||
|
final CredentialRepository credentialRepository;
|
||||||
|
|
||||||
|
GetCredentialInteractor(this.credentialRepository);
|
||||||
|
|
||||||
|
Future<Either<Failure, Success>> execute() async {
|
||||||
|
try {
|
||||||
|
final baseUrl = await credentialRepository.getBaseUrl();
|
||||||
|
final userName = await credentialRepository.getUserName();
|
||||||
|
final password = await credentialRepository.getPassword();
|
||||||
|
if (isCredentialValid(baseUrl)) {
|
||||||
|
return Right(GetCredentialViewState(baseUrl, userName, password));
|
||||||
|
} else {
|
||||||
|
return Left(GetCredentialFailure(BadCredentials()));
|
||||||
|
}
|
||||||
|
} catch (exception) {
|
||||||
|
return Left(GetCredentialFailure(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isCredentialValid(Uri baseUrl) => baseUrl.isBaseUrlValid();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user