Пример #1
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            var userConext = HttpContext.User;
            var userId     = userManager.GetUserId(userConext);

            if (!string.IsNullOrEmpty(userId))
            {
                var companyList = companyAppRepository.All().Where(e => e.ApplicationUserId == userId).ToList();
                if (companyList.Count > 0)
                {
                    var activeCompanies = companyList.Where(e => e.IsActive == true).ToList();
                    if (activeCompanies.Count > 1)
                    {
                        foreach (var activeCompany in activeCompanies)
                        {
                            activeCompany.IsActive = false;
                            companyAppRepository.Update(activeCompany);
                        }
                        await companyAppRepository.SaveChangesAsync();
                    }
                    var companyNavBars = companyList.Select(c => new CompanyNavBar()
                    {
                        Id = c.Id, Name = c.CompanyName, GUID = c.GUID, IsActive = c.IsActive
                    });
                    this.ViewBag.Companies = companyNavBars;
                    return(View());
                }
            }


            return(View());
        }
        public async Task <IActionResult> ChangeCompanyAsync(string guid)
        {
            this.Response.Cookies.Append("Company", guid, new CookieOptions()
            {
                MaxAge = new TimeSpan(30, 0, 0, 0)
            });
            var userConext = HttpContext.User;
            var userId     = userManager.GetUserId(userConext);

            if (!string.IsNullOrEmpty(userId))
            {
                var companyList     = companyAppRepository.All().Where(e => e.ApplicationUserId == userId);
                var activeCompanies = companyList.Where(e => e.IsActive == true).ToList();
                if (activeCompanies != null)
                {
                    if (activeCompanies.Count == 1)
                    {
                        var oldActive = activeCompanies.FirstOrDefault(e => e.IsActive == true);
                        oldActive.IsActive = false;
                        companyAppRepository.Update(oldActive);
                    }
                    else
                    {
                        foreach (var activeCompany in activeCompanies)
                        {
                            activeCompany.IsActive = false;
                            companyAppRepository.Update(activeCompany);
                        }
                    }
                }


                var newActive = companyList.Where(e => e.GUID == guid).FirstOrDefault();

                if (newActive == null)
                {
                    return(BadRequest());
                }

                newActive.IsActive = true;

                companyAppRepository.Update(newActive);
                await companyAppRepository.SaveChangesAsync();

                return(Redirect("/Home"));
            }
            return(BadRequest());
        }