Example #1
0
        private BaseResponse UpdateSpeedometer(SaveLayoutItemRequest request)
        {
            var response = new BaseResponse();
            try
            {
                var derLayoutItem = DataContext.DerLayoutItems
                    .Include(x => x.Artifact)
                    .Include(x => x.Artifact.Plots)
                    .Include(x => x.Artifact.CustomSerie)
                    .Include(x => x.Artifact.Series)
                    .Single(x => x.Id == request.Id);

                //DataContext.DerArtifacts.Remove(derLayoutItem.Artifact);

                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 derArtifact = new DerArtifact();
                derArtifact.GraphicType = request.Type;
                var plots = request.Artifact.Speedometer.PlotBands.Select(x => new DerArtifactPlot
                {
                    Color = x.Color,
                    From = x.From,
                    To = x.To
                }).ToList();

                derArtifact.Plots = plots;
                derArtifact.CustomSerie = DataContext.Kpis.FirstOrDefault(y => y.Id == request.Artifact.Speedometer.Series.KpiId);
                if (request.Artifact.Speedometer.LabelSeries != null) {
                    if (derArtifact.Series != null)
                    {
                        foreach (var serie in derArtifact.Series.ToList())
                        {
                            derArtifact.Series.Remove(serie);
                        }
                    }
                    else {
                        derArtifact.Series = new List<DerArtifactSerie>();
                    }
                    var labelSeries = new DerArtifactSerie
                    {
                        Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == request.Artifact.Speedometer.LabelSeries.KpiId),
                        Label = "tonnes/day",
                        Color = "#000"
                    };
                    derArtifact.Series.Add(labelSeries);
                }

                DataContext.DerArtifacts.Add(derArtifact);
                derLayoutItem.Artifact = derArtifact;
                //DataContext.DerLayoutItems.Add(derLayoutItem);

                var oldArtifact = new DerArtifact { Id = request.Artifact.Id };
                if (DataContext.DerArtifacts.Local.FirstOrDefault(x => x.Id == oldArtifact.Id) == null)
                {
                    DataContext.DerArtifacts.Attach(oldArtifact);
                }
                else
                {
                    oldArtifact = DataContext.DerArtifacts.Local.FirstOrDefault(x => x.Id == oldArtifact.Id);
                }

                foreach(var plot in oldArtifact.Plots.ToList())
                {
                    DataContext.DerArtifactPlots.Remove(plot);
                }

                DataContext.DerArtifacts.Remove(oldArtifact);

                DataContext.SaveChanges();
                response.IsSuccess = true;
            }
            catch (Exception exception)
            {
                response.Message = exception.Message;
            }

            return response;
        }
Example #2
0
        private BaseResponse SaveSpeedometer(SaveLayoutItemRequest request)
        {
            var response = new BaseResponse();
            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 derArtifact = new DerArtifact();
                derArtifact.GraphicType = request.Type;

                var plots = request.Artifact.Speedometer.PlotBands.Select(x => new DerArtifactPlot
                {
                    Color = x.Color,
                    From = x.From,
                    To = x.To
                }).ToList();
                derArtifact.Plots = plots;
                derArtifact.CustomSerie = DataContext.Kpis.FirstOrDefault(y => y.Id == request.Artifact.Speedometer.Series.KpiId);
                if (request.Artifact.Speedometer.LabelSeries != null)
                {
                    var labelSeries = new DerArtifactSerie
                    {
                        Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == request.Artifact.Speedometer.LabelSeries.KpiId),
                        Label = "ton/d",
                        Color = "#000"
                    };
                    derArtifact.Series = new List<DerArtifactSerie>();
                    derArtifact.Series.Add(labelSeries);
                }
                DataContext.DerArtifacts.Add(derArtifact);
                derLayoutItem.Artifact = derArtifact;
                DataContext.DerLayoutItems.Add(derLayoutItem);

                DataContext.SaveChanges();
                response.IsSuccess = true;
            }
            catch (Exception exception)
            {
                response.Message = exception.Message;
            }

            return response;
        }