feature(core):save
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:remever/common/functions.dart';
|
||||
import 'package:remever/common/services/api_client.dart';
|
||||
import 'package:remever/common/typedef.dart';
|
||||
import 'package:remever/database/database.dart';
|
||||
import 'package:remever/inject.dart';
|
||||
import 'package:remever/models/crud_collection_dto.dart';
|
||||
@@ -19,6 +21,10 @@ final class CollectionsService implements CollectionsInterface {
|
||||
|
||||
@override
|
||||
Future<void> createCollection(CrudCollectionDto dto) async {
|
||||
try {
|
||||
createCollectionApi(dto);
|
||||
} catch (_) {}
|
||||
|
||||
return await getIt<AppDatabase>().collectionsDao.createCollection(dto);
|
||||
}
|
||||
|
||||
@@ -41,33 +47,70 @@ final class CollectionsService implements CollectionsInterface {
|
||||
@override
|
||||
Future<void> getCollectionsFromApi() async {
|
||||
try {
|
||||
final List<Json> items = [];
|
||||
|
||||
final perPage = 20;
|
||||
|
||||
final Response<dynamic> response = await apiClient.get(
|
||||
'/collections',
|
||||
queryParameters: <String, dynamic>{'perPage': 20, 'page': 1},
|
||||
queryParameters: <String, dynamic>{'perPage': perPage, 'page': 9999},
|
||||
);
|
||||
|
||||
print('data');
|
||||
} catch (e) {
|
||||
if (response.data['success'] != true) {
|
||||
logger.logInfo('В методе getCollectionsFromApi success false');
|
||||
throw Exception('Не удалось получить коллекции');
|
||||
}
|
||||
|
||||
int totalCount = response.data['result']['totalCount'];
|
||||
|
||||
if (totalCount == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int pagesCount = (totalCount / perPage).ceil();
|
||||
int currentPage = 0;
|
||||
|
||||
while (pagesCount != currentPage) {
|
||||
final Response<dynamic> response = await apiClient.get(
|
||||
'/collections',
|
||||
queryParameters: <String, dynamic>{
|
||||
'perPage': perPage,
|
||||
'page': currentPage + 1,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.data['success'] != true) {
|
||||
throw Exception('Не удалось получить $currentPage страницу журнала');
|
||||
}
|
||||
|
||||
items.addAll(
|
||||
(response.data['result']['items'] as List)
|
||||
.cast<Map<String, dynamic>>(),
|
||||
);
|
||||
|
||||
currentPage = currentPage + 1;
|
||||
}
|
||||
|
||||
await getIt<AppDatabase>().collectionsDao.syncCollectionFromApi(items);
|
||||
} catch (e, st) {
|
||||
print('Response error $e');
|
||||
logger.logError('Ошибка в методе getCollectionsFromApi', e, st);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createCollectionApi() async {
|
||||
Future<void> createCollectionApi(CrudCollectionDto dto) async {
|
||||
try {
|
||||
final Response<dynamic> response = await apiClient.post(
|
||||
'/collections',
|
||||
data: {
|
||||
"title": "Основы программирования для утюгов",
|
||||
"description":
|
||||
"Коллекция карточек по основам программирования для начинающих",
|
||||
"is_public": true,
|
||||
"title": dto.title,
|
||||
"description": dto.desc,
|
||||
"is_public": dto.isPublic,
|
||||
},
|
||||
);
|
||||
|
||||
print('data');
|
||||
} catch (e) {
|
||||
} catch (e, st) {
|
||||
print('Response error $e');
|
||||
logger.logError('Ошибка в методе createCollectionApi', e, st);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user