public void Patch(DatasetEntity entity, DatasetPatch datasetPatch) { // Dataset if (!string.IsNullOrWhiteSpace(datasetPatch.Label)) { entity.Label = datasetPatch.Label; } if (datasetPatch.Private.HasValue) { entity.Private = datasetPatch.Private.Value; } // Category if (datasetPatch?.CategoryId != entity?.CategoryId) { entity.CategoryId = datasetPatch.CategoryId; } // Series Group if (datasetPatch.Groups != null) { foreach (var groupPatch in datasetPatch.Groups) { var group = entity.SeriesGroups.Single(z => z.Id == groupPatch.Id); _seriesGroupMapper.Patch(group, groupPatch); } } // Series if (datasetPatch.Series != null) { var newSeries = datasetPatch.Series.Where(z => !z.Id.HasValue); foreach (var series in newSeries) { var seriesCreation = _autoMapper.Map <SeriesCreation>(series); var seriesEntity = _seriesMapper.Create(seriesCreation); entity.Series.Add(seriesEntity); } var updatedSeries = datasetPatch.Series.Where(z => z.Id.HasValue); foreach (var seriesPatch in updatedSeries) { var series = entity.Series.Single(z => z.Id == seriesPatch.Id); _seriesMapper.Patch(series, seriesPatch); } } }
public async Task <ActionResult> PatchAsync(int id, [FromBody] DatasetPatch datasetPatch) { try { await _datasetService.PatchAsync(id, datasetPatch); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok()); }
/// <summary> /// Patch dataset /// </summary> /// <param name="datasetId"></param> /// <param name="datasetPatch"></param> public async Task PatchAsync(int datasetId, DatasetPatch datasetPatch) { var userId = await GetUserIdAsync(); await InvokeGuard(() => _guard.AgainstInvalidDatasetPatchAsync(datasetId, datasetPatch, userId)); var entity = await _repository.GetDatasetAsync(datasetId); // TODO: maybe this shouldn't all // happen in a mapper? _mapper.Patch(entity, datasetPatch); await _unitOfWork.SaveChangesAsync(); }
public async Task <ValidationResult> AgainstInvalidDatasetPatchAsync(int datasetId, DatasetPatch dataset, int?userId) { var result = new ValidationResult(); await _datasetValidator.DatasetExistsAndIsActiveAsync(datasetId, result); if (result.IsValid && userId.HasValue) { await _userValidator.UserOwnsDatasetAsync(userId.Value, datasetId, result); } if (dataset.CategoryId.HasValue) { await _categoryValidator.CategoryExistsAsync(dataset.CategoryId.Value, result); } if (dataset.Groups != null) { foreach (var seriesGroup in dataset.Groups) { _seriesGroupValidator.SeriesGroupHasValidOrder(seriesGroup.Order, result); var seriesOrderIdentifiers = seriesGroup.Series .Where(z => z.Order.HasValue) .Select(z => new OrderIdentifier { Id = z.Id.HasValue ? z.Id.Value : 0, Order = z.Order.Value }); await _datasetValidator.DatasetSeriesCanIntroduceOrderAsync(datasetId, seriesOrderIdentifiers, result); } var seriesGroupIds = dataset.Groups.Select(z => z.Id); await _datasetValidator.DatasetContainsSeriesGroupsAsync(datasetId, seriesGroupIds, result); var orderIdentifiers = dataset.Groups .Where(z => z.Order.HasValue) .Select(z => new OrderIdentifier { Id = z.Id, Order = z.Order.Value }); await _seriesGroupValidator.SeriesGroupsHaveValidOrderAsync(datasetId, orderIdentifiers, result); } if (dataset.Series != null) { // New series foreach (var newSeries in dataset.Series.Where(z => !z.Id.HasValue)) { _seriesValidator.SeriesHasValidTypeId(newSeries.Label, newSeries.Type, result); } // Existing series foreach (var existingSeries in dataset.Series.Where(z => z.Id.HasValue)) { await _datasetValidator.DatasetContainsSeriesAsync(datasetId, existingSeries.Id.Value, result); if (existingSeries.Type.HasValue) { _seriesValidator.SeriesCanChangeType(existingSeries.Label, existingSeries.Type, result); } } foreach (var series in dataset.Series) { if (!string.IsNullOrWhiteSpace(series.Color)) { _seriesValidator.SeriesHasValidColor(series.Label, series.Color, result); } if (series.Order.HasValue) { _seriesGroupValidator.SeriesGroupHasValidOrder(series.Order.Value, result); } } } return(result); }