request method

  1. @override
Future<A> request(
  1. A data
)
override

Implementation

@override
Future<A> request(A data) async {
  final dbStore = DbStoresWeebi().articles;
  final recordSnapshot = await dbStore.find(_database.db);
  if (recordSnapshot.isEmpty) {
    throw 'no articles';
  }
  final key = await dbStore.findKey(_database.db,
      finder: Finder(filter: Filter.equals('id', data.calibreId)));
  if (key == null) {
    throw 'cannot find calibreId ${data.calibreId} ${data.designation}';
  }
  final lineSnap = await dbStore.record(key).get(_database.db);
  if (lineSnap == null) {
    throw 'error lineSnap is null in updateArticle';
  }
  final _line = CalibreWeebi.fromMap(lineSnap, isProto: false);
  final _articleIndex = _line.articles.indexWhere((p) => p.id == data.id);
  _line.articles[_articleIndex] = data;

  final lineSnapUpdated = await dbStore
      .record(key)
      .update(_database.db, _line.toMap(isProto: false));

  if (lineSnapUpdated == null) {
    throw 'error lineSnapUpdated is null in updateArticle';
  }
  final CalibreWeebi line =
      CalibreWeebi.fromMap(lineSnapUpdated, isProto: false);
  final article = line.articles.firstWhere((element) =>
      element.calibreId == data.calibreId && element.id == data.id);
  //return data is Article ? article : article as ArticleBasket;
  return article as A;
}