示例#1
0
        public int AddUpdateDasboard(DashboardDetailDTO dto, int userId)
        {
            try
            {
                if (dto.Id == 0)
                {
                    List <DB_DashboardWidget> dbwidgets = new List <DB_DashboardWidget>();
                    foreach (var dbw in dto.DashboardWidgets)
                    {
                        var ent = new DB_DashboardWidget();
                        ent = _dashboardWidgetMapper.GetEntity(dbw, ent);
                        dbwidgets.Add(ent);
                    }
                    DB_Dashboard entdb = new DB_Dashboard();
                    entdb.Name             = dto.Name;
                    entdb.GlobalFilterId   = dto.GlobalFilterId;
                    entdb.DashboardWidgets = dbwidgets;
                    entdb.UserId           = userId;
                    entdb.CreatedOn        = DateTime.Now;
                    entdb.ModifiedOn       = DateTime.Now;

                    _dbcontext.DB_Dashboards.Add(entdb);
                    _dbcontext.SaveChanges();
                    return(entdb.Id);
                }
                else
                {
                    var dashboardWidgetIds = dto.DashboardWidgets.Where(o => o.Id != 0).Select(o => o.Id).ToList();
                    var deletewidgets      = _dbcontext.DB_DashboardWidgets.Include(o => o.DashboardWidgetSettings).Where(o => o.DashboardId == dto.Id && !dashboardWidgetIds.Contains(o.Id)).ToArray();
                    if (deletewidgets.Any())
                    {
                        foreach (var w in deletewidgets)
                        {
                            _dbcontext.DB_DashboardWidgetSettings.RemoveRange(w.DashboardWidgetSettings.ToArray());
                        }
                        _dbcontext.DB_DashboardWidgets.RemoveRange(deletewidgets);
                    }

                    var newwidgets = dto.DashboardWidgets.Where(o => o.Id == 0).ToList();
                    List <DB_DashboardWidget> dbwidgets = new List <DB_DashboardWidget>();
                    foreach (var dbw in newwidgets)
                    {
                        var ent = new DB_DashboardWidget();
                        ent             = _dashboardWidgetMapper.GetEntity(dbw, ent);
                        ent.DashboardId = dto.Id;
                        dbwidgets.Add(ent);
                    }
                    _dbcontext.DB_DashboardWidgets.AddRange(dbwidgets.ToArray());
                    _dbcontext.SaveChanges();

                    var oldwidgets = dto.DashboardWidgets.Where(o => o.Id != 0).ToList();
                    foreach (var dbw in oldwidgets)
                    {
                        var ent = _dbcontext.DB_DashboardWidgets.Single(o => o.Id == dbw.Id);
                        ent = _dashboardWidgetMapper.GetEntity(dbw, ent);
                    }
                    var dashboard = _dbcontext.DB_Dashboards.Single(o => o.Id == dto.Id);
                    dashboard.GlobalFilterId = dto.GlobalFilterId;
                    dashboard.ModifiedOn     = DateTime.Now;
                    dashboard.Name           = dto.Name;
                    _dbcontext.SaveChanges();
                    return(dto.Id);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }