TF-4243 Add datasource/repository/interactor for delete a label feature
This commit is contained in:
@@ -8,4 +8,6 @@ abstract class LabelDatasource {
|
||||
Future<Label> createNewLabel(AccountId accountId, Label labelData);
|
||||
|
||||
Future<Label> editLabel(AccountId accountId, EditLabelRequest labelRequest);
|
||||
|
||||
Future<void> deleteLabel(AccountId accountId, Label label);
|
||||
}
|
||||
|
||||
@@ -31,4 +31,11 @@ class LabelDatasourceImpl extends LabelDatasource {
|
||||
return await _labelApi.editLabel(accountId, labelRequest);
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteLabel(AccountId accountId, Label label) {
|
||||
return Future.sync(() async {
|
||||
return await _labelApi.deleteLabel(accountId, label);
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ 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:tmail_ui_user/features/labels/domain/model/edit_label_request.dart';
|
||||
import 'package:tmail_ui_user/features/labels/domain/exceptions/label_exceptions.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class LabelApi with HandleSetErrorMixin {
|
||||
@@ -96,4 +97,29 @@ class LabelApi with HandleSetErrorMixin {
|
||||
throw parseErrorForSetResponse(response, labelId);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteLabel(AccountId accountId, Label label) async {
|
||||
final labelId = label.id;
|
||||
|
||||
if (labelId == null) {
|
||||
throw LabelIdIsNull();
|
||||
}
|
||||
|
||||
final method = SetLabelMethod(accountId)..addDestroy({labelId});
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
if (response?.destroyed?.contains(labelId) != true) {
|
||||
throw parseErrorForSetResponse(response, labelId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,9 @@ class LabelRepositoryImpl extends LabelRepository {
|
||||
Future<Label> editLabel(AccountId accountId, EditLabelRequest labelRequest) {
|
||||
return _labelDatasource.editLabel(accountId, labelRequest);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteLabel(AccountId accountId, Label label) {
|
||||
return _labelDatasource.deleteLabel(accountId, label);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,4 +8,6 @@ abstract class LabelRepository {
|
||||
Future<Label> createNewLabel(AccountId accountId, Label labelData);
|
||||
|
||||
Future<Label> editLabel(AccountId accountId, EditLabelRequest labelRequest);
|
||||
|
||||
Future<void> deleteLabel(AccountId accountId, Label label);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
|
||||
class DeletingALabel extends LoadingState {}
|
||||
|
||||
class DeleteALabelSuccess extends UIState {
|
||||
final String labelDisplayName;
|
||||
|
||||
DeleteALabelSuccess(this.labelDisplayName);
|
||||
|
||||
@override
|
||||
List<Object> get props => [labelDisplayName];
|
||||
}
|
||||
|
||||
class DeleteALabelFailure extends FeatureFailure {
|
||||
DeleteALabelFailure(dynamic exception) : super(exception: exception);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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/extensions/label_extension.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/delete_a_label_state.dart';
|
||||
|
||||
class DeleteALabelInteractor {
|
||||
final LabelRepository _labelRepository;
|
||||
|
||||
DeleteALabelInteractor(this._labelRepository);
|
||||
|
||||
Stream<Either<Failure, Success>> execute(
|
||||
AccountId accountId,
|
||||
Label label,
|
||||
) async* {
|
||||
try {
|
||||
yield Right(DeletingALabel());
|
||||
await _labelRepository.createNewLabel(accountId, label);
|
||||
yield Right(DeleteALabelSuccess(label.safeDisplayName));
|
||||
} catch (e) {
|
||||
yield Left(DeleteALabelFailure(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user