public CreateSelectResponse Create(CreateSelectRequest request) { var response = new CreateSelectResponse(); try { var select = request.MapTo<Select>(); DataContext.Selects.Add(select); if (request.ParentId != 0) { var parent = new Select { Id = request.ParentId }; DataContext.Selects.Attach(parent); select.Parent = parent; } if (request.ParentOptionId != 0) { var parentOption = new SelectOption { Id = request.ParentOptionId }; DataContext.SelectOptions.Attach(parentOption); select.ParentOption = parentOption; } DataContext.SaveChanges(); response.IsSuccess = true; response.Message = "Select has been added successfully"; } catch (DbUpdateException dbUpdateException) { response.Message = dbUpdateException.Message; } return response; }
private BaseResponse SaveKpiInformations(SaveLayoutItemRequest request) { var response = new GetDerLayoutResponse(); try { var derLayoutItem = new DerLayoutItem(); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var kpiInformations = new List<DerKpiInformation>(); foreach (var item in request.KpiInformations) { if (item.KpiId > 0) { var kpi = new Kpi { Id = item.KpiId }; if (DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id) == null) { DataContext.Kpis.Attach(kpi); } else { kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id); } kpiInformations.Add(new DerKpiInformation { Kpi = kpi, Position = item.Position, IsOriginalData = item.IsOriginalData, ConfigType = item.ConfigType }); } else if (item.HighlightId > 0) { var selectOption = new SelectOption { Id = item.HighlightId }; if (DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == selectOption.Id) == null) { DataContext.SelectOptions.Attach(selectOption); } else { selectOption = DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == selectOption.Id); } kpiInformations.Add(new DerKpiInformation { SelectOption = selectOption, Position = item.Position, IsOriginalData = item.IsOriginalData, ConfigType = item.ConfigType }); } } derLayoutItem.KpiInformations = kpiInformations; DataContext.DerLayoutItems.Add(derLayoutItem); DataContext.SaveChanges(); response.IsSuccess = true; response.Message = "Changes has been saved"; } catch (Exception exception) { response.Message = exception.Message; } return response; }
public UpdateSelectResponse Update(UpdateSelectRequest request) { var response = new UpdateSelectResponse(); try { var select = DataContext.Selects.Where(p => p.Id == request.Id) .Include(p => p.Options) .Include(p => p.Parent) .Include(p => p.ParentOption) .Single(); DataContext.Entry(select).CurrentValues.SetValues(request); foreach (var option in select.Options.ToList()) { if (request.Options.All(c => c.Id != option.Id)) DataContext.SelectOptions.Remove(option); } foreach (var option in request.Options) { var existingOption = select.Options.SingleOrDefault(c => c.Id == option.Id); if (existingOption != null && option.Id != 0) { DataContext.Entry(existingOption).CurrentValues.SetValues(option); } else { var newOption = new SelectOption() { Text = option.Text, Value = option.Value }; select.Options.Add(newOption); } } if (request.ParentId != 0 && (select.Parent == null || select.Parent.Id != request.ParentId)) { var parent = new Select { Id = request.ParentId }; DataContext.Selects.Attach(parent); select.Parent = parent; var parentOption = new SelectOption { Id = request.ParentOptionId }; DataContext.SelectOptions.Attach(parentOption); select.ParentOption = parentOption; } else if (request.ParentId == 0) { select.Parent = null; select.ParentOption = null; } DataContext.SaveChanges(); response.IsSuccess = true; response.Message = "Select has been updated successfully"; } catch (DbUpdateException exception) { response.Message = exception.Message; } catch (ArgumentNullException exception) { response.Message = exception.Message; } catch (InvalidOperationException exception) { response.Message = exception.Message; } return response; }
private BaseResponse SaveHighlight(SaveLayoutItemRequest request) { var response = new GetDerLayoutResponse(); try { var derLayoutItem = new DerLayoutItem(); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var derHiglight = new DerHighlight(); var selectOption = new SelectOption { Id = request.Highlight.SelectOptionId }; DataContext.SelectOptions.Attach(selectOption); derHiglight.SelectOption = selectOption; derLayoutItem.Highlight = derHiglight; DataContext.DerHighlights.Add(derHiglight); DataContext.DerLayoutItems.Add(derLayoutItem); DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } return response; }
private BaseResponse UpdateHighlight(SaveLayoutItemRequest request) { var response = new GetDerLayoutResponse(); try { var derLayoutItem = DataContext.DerLayoutItems.Include(x => x.Highlight).Include(x => x.Highlight.SelectOption).Single(x => x.Id == request.Id); var selectOption = new SelectOption { Id = request.Highlight.SelectOptionId }; DataContext.SelectOptions.Attach(selectOption); derLayoutItem.Highlight.SelectOption = selectOption; DataContext.Entry(derLayoutItem).State = EntityState.Modified; DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } return response; }
private BaseResponse UpdateKpiInformations(SaveLayoutItemRequest request) { var response = new GetDerLayoutResponse(); try { var derLayoutItem = DataContext.DerLayoutItems.Include(x => x.KpiInformations).Single(x => x.Id == request.Id); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var kpiInformations = new List<DerKpiInformation>(); foreach (var item in request.KpiInformations) { var kpiInformation = DataContext.DerKpiInformations.SingleOrDefault(x => x.Id == item.Id); if (kpiInformation != null) { DataContext.DerKpiInformations.Remove(kpiInformation); } if (item.KpiId > 0) { var kpi = new Kpi { Id = item.KpiId }; if (DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id) == null) { DataContext.Kpis.Attach(kpi); } else { kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id); } var newKpiInformation = item.MapTo<DerKpiInformation>(); newKpiInformation.Kpi = kpi; kpiInformations.Add(newKpiInformation); } else if (item.HighlightId > 0) { var selectOption = new SelectOption { Id = item.HighlightId }; if (DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == selectOption.Id) == null) { DataContext.SelectOptions.Attach(selectOption); } else { selectOption = DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == selectOption.Id); } kpiInformations.Add(new DerKpiInformation { SelectOption = selectOption, Position = item.Position, ConfigType = item.ConfigType }); } /*if (kpiInformation != null) { DataContext.DerKpiInformations.Remove(kpiInformation); if (item.KpiId > 0) { var kpi = new Kpi { Id = item.KpiId }; if (DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id) == null) { DataContext.Kpis.Attach(kpi); } else { kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id); } var newKpiInformation = item.MapTo<DerKpiInformation>(); newKpiInformation.Kpi = kpi; kpiInformations.Add(newKpiInformation); } else if (item.HighlightId > 0) { var selectOption = new SelectOption { Id = item.HighlightId }; if (DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == selectOption.Id) == null) { DataContext.SelectOptions.Attach(selectOption); } else { selectOption = DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == selectOption.Id); } kpiInformations.Add(new DerKpiInformation { SelectOption = selectOption, Position = item.Position, ConfigType = item.ConfigType }); } } else { if (item.KpiId > 0) { var kpi = new Kpi { Id = item.KpiId }; if (DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id) == null) { DataContext.Kpis.Attach(kpi); } else { kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id); } var newKpiInformation = item.MapTo<DerKpiInformation>(); newKpiInformation.Kpi = kpi; kpiInformations.Add(newKpiInformation); //kpiInformations.Add(new DerKpiInformation { Kpi = kpi, Position = item.Position, ConfigType = item.ConfigType, KpiLabel = item.KpiLabel, KpiMeasurement = item.KpiMeasurement}); } else if (item.HighlightId > 0) { var selectOption = new SelectOption { Id = item.HighlightId }; if (DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == selectOption.Id) == null) { DataContext.SelectOptions.Attach(selectOption); } else { selectOption = DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == selectOption.Id); } kpiInformations.Add(new DerKpiInformation { SelectOption = selectOption, Position = item.Position, ConfigType = item.ConfigType }); } }*/ } derLayoutItem.KpiInformations = kpiInformations; //DataContext.DerLayoutItems.Add(derLayoutItem); DataContext.Entry(derLayoutItem).State = EntityState.Modified; DataContext.SaveChanges(); response.IsSuccess = true; response.Message = "Changes has been saved"; } catch (Exception exception) { response.Message = exception.Message; } return response; }
public SaveWeatherResponse SaveWeather(SaveWeatherRequest request) { try { if (request.Id != 0) { var weather = DataContext.Weathers.First(x => x.Id == request.Id); request.MapPropertiesToInstance<Weather>(weather); var value = new SelectOption { Id = request.ValueId }; DataContext.SelectOptions.Attach(value); weather.Value = value; } else { var weather = request.MapTo<Weather>(); var value = new SelectOption { Id = request.ValueId }; DataContext.SelectOptions.Attach(value); weather.Value = value; DataContext.Weathers.Add(weather); } DataContext.SaveChanges(); return new SaveWeatherResponse { IsSuccess = true, Message = "Weather data has been saved successfully" }; } catch (InvalidOperationException e) { return new SaveWeatherResponse { IsSuccess = false, Message = "An error occured while trying to save weather data" }; } }
public SaveWaveResponse SaveWave(SaveWaveRequest request) { try { var wave = request.MapTo<Wave>(); if (request.Id != 0) { wave = DataContext.Waves.First(x => x.Id == request.Id); request.MapPropertiesToInstance<Wave>(wave); if (request.ValueId != 0) { var value = DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == request.ValueId); if (value == null) { value = new SelectOption { Id = request.ValueId }; DataContext.SelectOptions.Attach(value); } wave.Value = value; } } else { if (request.ValueId != 0) { var value = DataContext.SelectOptions.Local.FirstOrDefault(x => x.Id == request.ValueId); if (value == null) { value = new SelectOption { Id = request.ValueId }; DataContext.SelectOptions.Attach(value); } } DataContext.Waves.Add(wave); } DataContext.SaveChanges(); return new SaveWaveResponse { IsSuccess = true, Message = "Wave data has been saved successfully", Id = wave.Id }; } catch (InvalidOperationException e) { return new SaveWaveResponse { IsSuccess = false, Message = "An error occured while trying to save weather data" }; } }