public async Task <LastSuccessfulSyncEntity> FindByTableIdAndBackendId(string tableId, string backendId = "default") { var item = await SqLiteConnection.FindWithQueryAsync <LastSuccessfulSyncEntity>( "SELECT lss.* FROM last_successful_sync lss WHERE lss.table_id = ? AND lss.backend_id = ?", tableId, backendId ); if (item != default) { return(item); } var errorCode = DataErrorCodes.ItemNotFound; throw new GrException(errorCode.Message, errorCode: errorCode); }
/// <inheritdoc/> public async Task <TEntity> Save(TEntity entity, bool updateTimestamp = true) { var now = DateTimeOffset.Now; var entityId = entity.GetPropertyValueForAttribute <TId, PrimaryKeyAttribute>(); var hasId = entityId != null && !entityId.Equals(default(TId)); if (updateTimestamp) { if (!hasId) { entity.SetValueForAttribute <CreatedAtColumnAttribute>(now); } entity.SetValueForAttribute <UpdatedAtColumnAttribute>(now); } var isUpdate = hasId; if (hasId) { var tableName = await FetchTableName(); var idColName = await FetchColumnNameFromAttribute <PrimaryKeyAttribute>(); var oldRecord = await SqLiteConnection.FindWithQueryAsync <TEntity>( $"SELECT * FROM {tableName} WHERE {idColName} = ?", entityId); if (oldRecord == default) { isUpdate = false; } } var saveTask = isUpdate ? SqLiteConnection.UpdateAsync(entity) : SqLiteConnection.InsertAsync(entity); await saveTask; return(entity); }