Example #1
0
        public async Task <IActionResult> Select(Guid item, string button)
        {
            if (button == "delete")
            {
                var h = _db.Products.Get(item);
                _db.Products.Delete(h.Id);
                _db.Save();
                return(View("ProductList", _db.Products.GetAllList().ToList()));
            }
            if (button == "change")
            {
                return(View("Shift", item));
            }
            if (button == "buy")
            {
                //  User user = await _userManager.FindByIdAsync(Convert.ToString(item));
                // if ((await _userManager.IsInRoleAsync(user, "Client") == true) && (user!=null))
                if (User.IsInRole("Client"))
                {
                    var user = await _userManager.FindByNameAsync(User.Identity.Name);

                    var h = _db.Clients.Get(Guid.Parse(user.Id));
                    if (h == null)
                    {
                        Client client = new Client
                        {
                            Name = User.Identity.Name,
                            Code = GenerateCodeClient.Generate(),
                            Id   = Guid.Parse(user.Id)
                        };
                        _db.Clients.Create(client);
                        _db.Save();

                        BuyingProduct.AddToCan(_db, client, _db.Products.Get(item));

                        return(View("Buy", _db));
                    }
                    else
                    {
                        BuyingProduct.AddToCan(_db, h, _db.Products.Get(item));

                        return(View("Buy", _db));
                    }
                }
                return(RedirectToAction("Index", "Home"));
            }
            return(View("ProductList", _db.Products.GetAllList().ToList()));
        }
Example #2
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = new User {
                    Email = model.Email, UserName = model.Email
                };

                // добавляем пользователя
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(user, "Client");

                    Client client = new Client
                    {
                        Name = user.UserName,
                        Code = GenerateCodeClient.Generate(),
                        Id   = Guid.Parse(user.Id)
                    };
                    _db.Clients.Create(client);
                    _db.Save();


                    // установка куки
                    await _signInManager.SignInAsync(user, false);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }
            return(View(model));
        }