update method

  1. @override
FutureOr<void> update(
  1. TaskModel entity, {
  2. bool createOpLog = true,
})
override

The update method updates an existing entity.

Implementation

@override
FutureOr<void> update(
  TaskModel entity, {
  bool createOpLog = true,
}) async {
  return retryLocalCallOperation(() async {
    final taskCompanion = entity.companion;

    final addressCompanion = entity.address
        ?.copyWith(
          relatedClientReferenceId: entity.clientReferenceId,
          auditDetails: entity.auditDetails,
          clientAuditDetails: entity.clientAuditDetails,
        )
        .companion;

    final resourcesCompanions = entity.resources?.map((e) {
          return e
              .copyWith(
                clientReferenceId: e.clientReferenceId,
                taskclientReferenceId: entity.clientReferenceId,
              )
              .companion;
        }).toList() ??
        [];

    await sql.batch((batch) {
      batch.update(
        sql.task,
        taskCompanion,
        where: (table) => table.clientReferenceId.equals(
          entity.clientReferenceId,
        ),
      );

      if (addressCompanion != null) {
        batch.update(
          sql.address,
          addressCompanion,
          where: (table) => table.relatedClientReferenceId.equals(
            addressCompanion.relatedClientReferenceId.value!,
          ),
        );
      }

      batch.insertAllOnConflictUpdate(sql.taskResource, resourcesCompanions);
    });

    await super.update(entity, createOpLog: createOpLog);
  });
}