create method

  1. @override
FutureOr<void> create(
  1. ServiceModel entity, {
  2. bool createOpLog = true,
  3. DataOperation dataOperation = DataOperation.singleCreate,
})
override

The create method creates a new entity.

Implementation

@override
FutureOr<void> create(
  ServiceModel entity, {
  bool createOpLog = true,
  DataOperation dataOperation = DataOperation.singleCreate,
}) async {
  return retryLocalCallOperation(() async {
    final serviceCompanion = entity.companion;
    final attributes = entity.attributes;
    await sql.batch((batch) {
      batch.insert(
        sql.service,
        serviceCompanion,
        mode: InsertMode.insertOrReplace,
      );
      if (attributes != null) {
        final attributesCompanions = attributes.map((e) {
          return e.companion;
        }).toList();

        batch.insertAll(
          sql.serviceAttributes,
          attributesCompanions,
          mode: InsertMode.insertOrReplace,
        );
      }
    });

    final newEntity = ServiceModel(
      id: entity.id,
      clientId: entity.clientId,
      serviceDefId: entity.serviceDefId,
      isActive: entity.isActive,
      accountId: entity.accountId,
      additionalDetails: entity.additionalDetails,
      tenantId: entity.tenantId,
      isDeleted: entity.isDeleted,
      rowVersion: entity.rowVersion,
      additionalFields: ServiceAdditionalFields(
        version: 1,
        fields: [
          AdditionalField(
            'clientCreatedTime',
            DateTime.now().millisecondsSinceEpoch.toString(),
          ),
          AdditionalField(
            'clientCreatedBy',
            entity.auditDetails?.createdBy,
          ),
        ],
      ),
      auditDetails: entity.auditDetails,
      clientAuditDetails: entity.clientAuditDetails,
      attributes: entity.attributes?.map((e) {
        return e.dataType == 'Number'
            ? e.copyWith(value: int.tryParse(e.value))
            : e.dataType == 'MultiValueList'
                ? e.copyWith(
                    value: e.value.toString().split('.'),
                    additionalDetails: e.additionalDetails != null
                        ? {"value": e.additionalDetails}
                        : null,
                  )
                : e.dataType == 'SingleValueList'
                    ? e.copyWith(
                        additionalDetails: e.additionalDetails != null
                            ? {"value": e.additionalDetails}
                            : null,
                      )
                    : e;
      }).toList(),
    );

    await super.create(
      newEntity,
      dataOperation: DataOperation.singleCreate,
      createOpLog: true,
    );
  });
}