public async Task <BaseResponseModelPost> Create(MetricModel request) { BaseResponseModelPost response = new BaseResponseModelPost(); if (request.Validate()) { if (await CheckAspiceProcess(request.AspiceProcessId, response)) { if (await CheckMetricType(request.MetricTypeId, response)) { if (await CheckAffectedField(request.AffectedFieldId, response)) { if (await CheckMetricIdentificator(request.Identificator, response)) { if (CheckMetricColumns(request.Columns, response)) { Metric metric = new Metric { Name = request.Name, Description = request.Description, Identificator = request.Identificator, RequirementGroup = request.RequirementGroup, AspiceProcessId = request.AspiceProcessId, AffectedFieldId = request.AffectedFieldId, MetricTypeId = request.MetricTypeId, Public = CurrentUser.CompanyId.HasValue ? request.Public : true, CompanyId = CurrentUser.CompanyId }; for (int i = 0; i < request.Columns.Count; i++) { await CreateMetricColumn(metric, request.Columns[i]); } await Database.Metric.AddAsync(metric); await Database.SaveChangesAsync(); response.Id = metric.Id; response.Message = "Metric was successfully created!"; } } } } } } else { response.Success = false; response.Message = "Some of the required properties is not present!"; } return(response); }
public async Task <BaseResponseModel> Edit(MetricModel request) { BaseResponseModel response = new BaseResponseModel(); if (request.Validate()) { if (await CheckAspiceProcess(request.AspiceProcessId, response)) { if (await CheckMetricType(request.MetricTypeId, response)) { if (await CheckAffectedField(request.AffectedFieldId, response)) { Metric metric = await Load(request.Id, response); if (metric != null && CheckMetricForCrud(metric, response)) { if (await CheckMetricIdentificator(request.Identificator, response, metric.Id) && CheckMetricColumns(request.Columns, response)) { metric.Identificator = request.Identificator; metric.Name = request.Name; metric.Description = request.Description; metric.RequirementGroup = request.RequirementGroup; metric.AspiceProcessId = request.AspiceProcessId; metric.AffectedFieldId = request.AffectedFieldId; //metric.MetricTypeId = request.MetricTypeId; --> nelze menit z duvodu, ze by potom nesedely hodnoty columns - musi se mazat columns metric.Public = CurrentUser.CompanyId.HasValue ? request.Public : true; //smazane var deletedIds = metric.MetricColumn.Select(c => c.Id).Except(request.Columns.Select(c => c.Id)); Database.MetricColumn.RemoveRange(metric.MetricColumn.Where(c => deletedIds.Contains(c.Id))); //nove a editovane for (int i = 0; i < request.Columns.Count; i++) { if (request.Columns[i].Id == 0) { await CreateMetricColumn(metric, request.Columns[i]); } else { MetricColumn column = metric.MetricColumn.First(c => c.Id == request.Columns[i].Id); column.Value = request.Columns[i].Value ?? string.Empty; column.FieldName = request.Columns[i].FieldName; column.NumberFieldName = request.Columns[i].NumberFieldName; if (!string.IsNullOrEmpty(request.Columns[i].CoverageName)) { column.DivisorValue = request.Columns[i].DivisorValue ?? JazzService.ALL_VALUES; column.DivisorFieldName = request.Columns[i].DivisorFieldName ?? string.Empty; column.CoverageName = request.Columns[i].CoverageName; } } } await Database.SaveChangesAsync(); response.Message = "Metric was successfully edited!"; } } } } } } else { response.Success = false; response.Message = "Some of the required properties is not present!"; } return(response); }