TF-4178 Add datasource/repository/interactor for create new label

This commit is contained in:
dab246
2025-11-26 16:32:53 +07:00
committed by Dat H. Pham
parent 8f1da20e54
commit 38c47ca373
10 changed files with 128 additions and 22 deletions
@@ -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);
}
}