public async Task <RulesetDashboardLayout> Create(RulesetDashboardLayout item)
 {
     //item.IsDefaultComputer = false;
     //item.IsDefaultTablet = false;
     //item.IsDefaultMobile = false;
     return(await _repo.Add(item));
 }
        public void Create_sp(RulesetDashboardLayout model, string UserId, int old_RulesetDashboardLayoutId)
        {
            string consString = _configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;

            using (SqlConnection con = new SqlConnection(consString))
            {
                using (SqlCommand cmd = new SqlCommand("Ruleset_DuplicateLayout_And_Page"))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection  = con;
                    cmd.Parameters.AddWithValue("@RulesetLayoutID", model.RulesetDashboardLayoutId);
                    cmd.Parameters.AddWithValue("@OldRulesetDashboardLayoutId", old_RulesetDashboardLayoutId);
                    cmd.Parameters.AddWithValue("@RulesetId", model.RulesetId);
                    cmd.Parameters.AddWithValue("@UserId", UserId);
                    cmd.Parameters.AddWithValue("@IsDuplicatingLayout", true);
                    con.Open();
                    try
                    {
                        var a = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        con.Close();
                        throw ex;
                    }
                    con.Close();
                    //return true;
                }
            }
        }
        private void RemoveDefaultComputerDeviceFromOtherLayouts(RulesetDashboardLayout RDL)
        {
            var layouts = _context.RulesetDashboardLayouts.Where(x => x.RulesetDashboardLayoutId != RDL.RulesetDashboardLayoutId && x.RulesetId == RDL.RulesetId && x.IsDeleted != true).ToList();

            foreach (var item in layouts)
            {
                item.IsDefaultComputer = false;
            }
            _context.SaveChanges();
        }
        public RulesetDashboardLayout GetById(int?id)
        {
            RulesetDashboardLayout RulesetDashboardLayout = _context.RulesetDashboardLayouts
                                                            .Include(d => d.RulesetDashboardPages)
                                                            .Where(x => x.RulesetDashboardLayoutId == id && x.IsDeleted != true).SingleOrDefault();

            if (RulesetDashboardLayout == null)
            {
                return(RulesetDashboardLayout);
            }
            RulesetDashboardLayout.RulesetDashboardPages = RulesetDashboardLayout.RulesetDashboardPages.Where(p => p.IsDeleted != true).OrderBy(x => x.SortOrder).ToList();

            return(RulesetDashboardLayout);
        }
        public async Task <RulesetDashboardLayout> Update(RulesetDashboardLayout item)
        {
            var RulesetDashboardLayout = await _repo.Get(item.RulesetDashboardLayoutId);

            if (RulesetDashboardLayout == null)
            {
                return(RulesetDashboardLayout);
            }

            RulesetDashboardLayout.Name          = item.Name;
            RulesetDashboardLayout.DefaultPageId = item.DefaultPageId;
            RulesetDashboardLayout.LayoutHeight  = item.LayoutHeight;
            RulesetDashboardLayout.LayoutWidth   = item.LayoutWidth;

            RulesetDashboardLayout.IsDefaultComputer = item.IsDefaultComputer;
            RulesetDashboardLayout.IsDefaultTablet   = item.IsDefaultTablet;
            RulesetDashboardLayout.IsDefaultMobile   = item.IsDefaultMobile;

            try
            {
                await _repo.Update(RulesetDashboardLayout);

                if (RulesetDashboardLayout.IsDefaultComputer)
                {
                    RemoveDefaultComputerDeviceFromOtherLayouts(RulesetDashboardLayout);
                }
                if (RulesetDashboardLayout.IsDefaultTablet)
                {
                    RemoveDefaultTabletDeviceFromOtherLayouts(RulesetDashboardLayout);
                }
                if (RulesetDashboardLayout.IsDefaultMobile)
                {
                    RemoveDefaultMobileDeviceFromOtherLayouts(RulesetDashboardLayout);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(RulesetDashboardLayout);
        }