/// <summary> /// /// </summary> /// <param name="contract"></param> /// <returns></returns> #region PUT public async Task PutChartDataAsync(KpiChartContract contract) { var existing = await Context.KpiCharts.FindAsync(contract.Id); if (existing == null) { throw new CannotFindIdException("chart", contract.Id); } var updated = KPIDashboardMapper.ChartContractToEntity(contract); Context.KpiDataSets.RemoveRange(Context.KpiDataSets.Where(x => x.ChartId == existing.Id)); if (!Context.KpiAdditionalContents.Any(c => c.Id == updated.Id)) { Context.KpiAdditionalContents.Add(updated.AdditionalContent); } else { Context.Entry(updated.AdditionalContent).State = EntityState.Modified; } existing.DataSets = contract.DataSets.Select(KPIDashboardMapper.DataSetContractToEntity).ToList(); Context.Entry(existing).CurrentValues.SetValues(updated); Context.Entry(existing).State = EntityState.Modified; if (await Context.SaveChangesAsync() <= 0) { throw new CannotSaveToDatabaseException("chart"); } }
/// <summary> /// /// </summary> /// <param name="contract"></param> /// <returns></returns> #region POST public async Task <int> PostChartDataAsync(KpiChartContract contract) { if (Context.KpiCharts.Find(contract.Id) != null) { throw new EntityConflictException("chart", contract.Id); } var newChart = KPIDashboardMapper.ChartContractToEntity(contract); var category = Context.KpiCategories.Find(contract.CategoryId); if (category == null) { throw new CannotFindIdException("category", contract.CategoryId); } newChart.DisplayOrder = category.Charts.Count() + 1; Context.KpiCharts.Add(newChart); Context.Entry(newChart).State = EntityState.Added; var isSaved = await Context.SaveChangesAsync(); if (isSaved <= 0) { throw new CannotSaveToDatabaseException("chart"); } return(newChart.Id); }