64 lines
2.4 KiB
Dart
64 lines
2.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aman_kassa_flutter/core/base/base_service.dart';
|
|
import 'package:aman_kassa_flutter/core/entity/Category.dart';
|
|
import 'package:aman_kassa_flutter/core/entity/Goods.dart';
|
|
import 'package:aman_kassa_flutter/core/entity/Service.dart';
|
|
import 'package:aman_kassa_flutter/core/locator.dart';
|
|
import 'package:aman_kassa_flutter/core/models/Response.dart';
|
|
import 'package:aman_kassa_flutter/core/services/DbService.dart';
|
|
|
|
import 'ApiService.dart';
|
|
|
|
class DataService extends BaseService {
|
|
final ApiService _api = locator<ApiService>();
|
|
final DbService _db = locator<DbService>();
|
|
|
|
Future<List<Category>> getCategoriesByParentId ( {int parentId} ) async {
|
|
List<Map<String, dynamic>> list = await _db.queryRowsWithWhere(Category_tableName, '$Category_columnParentIn = ?', [parentId ?? 0 ]);
|
|
return list.map((e) => Category.fromMap(e)).toList();
|
|
}
|
|
|
|
Future<List<Good>> getGoodsByCategoryId ( {int categoryId} ) async {
|
|
List<Map<String, dynamic>> list = await _db.queryRowsWithWhere(Goog_tableName, '$Goog_columnCategoryId = ?', [categoryId ?? 0]);
|
|
return list.map((e) => Good.fromMap(e)).toList();
|
|
}
|
|
|
|
Future<bool> getDataFromServer(String token) async {
|
|
try {
|
|
|
|
Response<dynamic> goods = await _api.getGoodsFromServer(token);
|
|
Response<dynamic> categories = await _api.getCategoryFromServer(token);
|
|
Response<dynamic> services = await _api.getServiceFromServer(token);
|
|
await _db.deleteAll(Goog_tableName);
|
|
await _db.deleteAll(Category_tableName);
|
|
await _db.deleteAll(Service_tableName);
|
|
log.i('All tables cleaned');
|
|
for (var key in goods.body.keys) {
|
|
print(goods.body[key]);
|
|
Good row = Good.fromMap(goods.body[key]);
|
|
_db.insert(Goog_tableName,row.toMap());
|
|
}
|
|
log.i('Inserted ${goods.body.length} to table $Goog_tableName');
|
|
|
|
for (var el in categories.body) {
|
|
print(el);
|
|
Category row = Category.fromMap(el);
|
|
_db.insert(Category_tableName,row.toMap());
|
|
}
|
|
log.i('Inserted ${categories.body.length} to table $Category_tableName');
|
|
|
|
for (var key in services.body.keys) {
|
|
print(services.body[key]);
|
|
Service row = Service.fromMap(services.body[key]);
|
|
_db.insert(Service_tableName,row.toMap());
|
|
}
|
|
log.i('Inserted ${services.body.length} to table $Service_tableName');
|
|
|
|
return true;
|
|
} catch (e) {
|
|
print(e);
|
|
return false;
|
|
}
|
|
}
|
|
} |