TF-4178 Add datasource/repository/interactor for create new label
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/method/error_method_response.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/set_error.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/method/response/set_response.dart';
|
||||
@@ -76,4 +77,26 @@ mixin HandleSetErrorMixin {
|
||||
logWarning('HandleSetErrorMixin::handleSetResponse():remainedErrors: $remainedErrors');
|
||||
return remainedErrors;
|
||||
}
|
||||
|
||||
parseErrorForSetResponse(SetResponse? response, Id requestId) {
|
||||
final mapError =
|
||||
response?.notCreated ?? response?.notUpdated ?? response?.notDestroyed;
|
||||
if (mapError != null && mapError.containsKey(requestId)) {
|
||||
final setError = mapError[requestId];
|
||||
log('HandleSetErrorMixin::parseErrorForSetResponse():setError: $setError');
|
||||
if (setError?.type == ErrorMethodResponse.invalidArguments) {
|
||||
throw InvalidArgumentsMethodResponse(
|
||||
description: setError?.description,
|
||||
);
|
||||
} else if (setError?.type == ErrorMethodResponse.invalidResultReference) {
|
||||
throw InvalidResultReferenceMethodResponse(
|
||||
description: setError?.description,
|
||||
);
|
||||
} else {
|
||||
throw UnknownMethodResponse(description: setError?.description);
|
||||
}
|
||||
} else {
|
||||
throw UnknownMethodResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,6 @@ import 'package:labels/model/label.dart';
|
||||
|
||||
abstract class LabelDatasource {
|
||||
Future<List<Label>> getAllLabels(AccountId accountId);
|
||||
|
||||
Future<Label> createNewLabel(AccountId accountId, Label labelData);
|
||||
}
|
||||
|
||||
@@ -19,4 +19,14 @@ class LabelDatasourceImpl extends LabelDatasource {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Label> createNewLabel(AccountId accountId, Label labelData) {
|
||||
return Future.sync(() async {
|
||||
return await _labelApi.createNewLabel(accountId, labelData);
|
||||
}).catchError((error, stackTrace) async {
|
||||
await _exceptionThrower.throwException(error, stackTrace);
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
import 'package:jmap_dart_client/http/http_client.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/jmap_request.dart';
|
||||
import 'package:labels/labels.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/handle_error_mixin.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class LabelApi {
|
||||
class LabelApi with HandleSetErrorMixin {
|
||||
final HttpClient _httpClient;
|
||||
final Uuid _uuid;
|
||||
|
||||
LabelApi(this._httpClient);
|
||||
LabelApi(this._httpClient, this._uuid);
|
||||
|
||||
Future<List<Label>> getAllLabels(AccountId accountId) async {
|
||||
final builder = JmapRequestBuilder(_httpClient, ProcessingInvocation());
|
||||
@@ -23,4 +27,34 @@ class LabelApi {
|
||||
|
||||
return response?.list ?? [];
|
||||
}
|
||||
|
||||
Future<Label> createNewLabel(AccountId accountId, Label labelData) async {
|
||||
final generateCreateId = Id(_uuid.v1());
|
||||
|
||||
final method = SetLabelMethod(accountId)
|
||||
..addCreate(generateCreateId, labelData);
|
||||
|
||||
final builder = JmapRequestBuilder(_httpClient, ProcessingInvocation());
|
||||
final invocation = builder.invocation(method);
|
||||
|
||||
final result =
|
||||
await (builder..usings(method.requiredCapabilities)).build().execute();
|
||||
|
||||
final response = result.parse<SetLabelResponse>(
|
||||
invocation.methodCallId,
|
||||
SetLabelResponse.deserialize,
|
||||
);
|
||||
|
||||
final labelCreated = response?.created?[generateCreateId];
|
||||
|
||||
if (labelCreated != null) {
|
||||
final newLabelCreated = labelCreated.copyWith(
|
||||
displayName: labelData.displayName,
|
||||
color: labelData.color,
|
||||
);
|
||||
return newLabelCreated;
|
||||
} else {
|
||||
throw parseErrorForSetResponse(response, generateCreateId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,4 +12,9 @@ class LabelRepositoryImpl extends LabelRepository {
|
||||
Future<List<Label>> getAllLabels(AccountId accountId) {
|
||||
return _labelDatasource.getAllLabels(accountId);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Label> createNewLabel(AccountId accountId, Label labelData) {
|
||||
return _labelDatasource.createNewLabel(accountId, labelData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,6 @@ import 'package:labels/model/label.dart';
|
||||
|
||||
abstract class LabelRepository {
|
||||
Future<List<Label>> getAllLabels(AccountId accountId);
|
||||
|
||||
Future<Label> createNewLabel(AccountId accountId, Label labelData);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:labels/model/label.dart';
|
||||
|
||||
class CreatingNewLabel extends LoadingState {}
|
||||
|
||||
class CreateNewLabelSuccess extends UIState {
|
||||
final Label newLabel;
|
||||
|
||||
CreateNewLabelSuccess(this.newLabel);
|
||||
|
||||
@override
|
||||
List<Object> get props => [newLabel];
|
||||
}
|
||||
|
||||
class CreateNewLabelFailure extends FeatureFailure {
|
||||
CreateNewLabelFailure(dynamic exception) : super(exception: exception);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:labels/model/label.dart';
|
||||
import 'package:tmail_ui_user/features/labels/domain/repository/label_repository.dart';
|
||||
import 'package:tmail_ui_user/features/labels/domain/state/create_new_label_state.dart';
|
||||
|
||||
class CreateNewLabelInteractor {
|
||||
final LabelRepository _labelRepository;
|
||||
|
||||
CreateNewLabelInteractor(this._labelRepository);
|
||||
|
||||
Stream<Either<Failure, Success>> execute(
|
||||
AccountId accountId,
|
||||
Label labelData,
|
||||
) async* {
|
||||
try {
|
||||
yield Right(CreatingNewLabel());
|
||||
final newLabel = await _labelRepository.createNewLabel(
|
||||
accountId,
|
||||
labelData,
|
||||
);
|
||||
yield Right(CreateNewLabelSuccess(newLabel));
|
||||
} catch (e) {
|
||||
yield Left(CreateNewLabelFailure(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import 'package:tmail_ui_user/features/labels/domain/repository/label_repository
|
||||
import 'package:tmail_ui_user/features/labels/domain/usecases/get_all_label_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/usecases/verify_name_interactor.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class LabelInteractorBindings extends InteractorsBindings {
|
||||
@override
|
||||
@@ -18,7 +19,7 @@ class LabelInteractorBindings extends InteractorsBindings {
|
||||
|
||||
@override
|
||||
void bindingsDataSourceImpl() {
|
||||
Get.lazyPut(() => LabelApi(Get.find<HttpClient>()));
|
||||
Get.lazyPut(() => LabelApi(Get.find<HttpClient>(), Get.find<Uuid>()));
|
||||
Get.lazyPut(
|
||||
() => LabelDatasourceImpl(
|
||||
Get.find<LabelApi>(),
|
||||
|
||||
@@ -10,7 +10,6 @@ import 'package:jmap_dart_client/http/http_client.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/core_capability.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/method/error_method_response.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/error/set_error.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/patch_object.dart';
|
||||
@@ -197,24 +196,7 @@ class MailboxAPI with HandleSetErrorMixin, MailAPIMixin {
|
||||
parentId: request.parentId);
|
||||
return newMailboxCreated;
|
||||
} else {
|
||||
throw _parseErrorForSetMailboxResponse(setMailboxResponse, generateCreateId);
|
||||
}
|
||||
}
|
||||
|
||||
_parseErrorForSetMailboxResponse(SetMailboxResponse? response, Id requestId) {
|
||||
final mapError = response?.notCreated ?? response?.notUpdated ?? response?.notDestroyed;
|
||||
if (mapError != null && mapError.containsKey(requestId)) {
|
||||
final setError = mapError[requestId];
|
||||
log('MailboxAPI::_parseErrorForSetMailboxResponse():setError: $setError');
|
||||
if (setError?.type == ErrorMethodResponse.invalidArguments) {
|
||||
throw InvalidArgumentsMethodResponse(description: setError?.description);
|
||||
} else if (setError?.type == ErrorMethodResponse.invalidResultReference) {
|
||||
throw InvalidResultReferenceMethodResponse(description: setError?.description);
|
||||
} else {
|
||||
throw UnknownMethodResponse(description: setError?.description);
|
||||
}
|
||||
} else {
|
||||
throw UnknownMethodResponse();
|
||||
throw parseErrorForSetResponse(setMailboxResponse, generateCreateId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user