public async Task <IActionResult> Add([Bind("Name")] Product product)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                product.UserId = _context.Users.FirstOrDefault(x => x.Email == user.Email).Id;
                _context.Products.Add(product);
                _context.SaveChanges();
            }

            return(RedirectToAction("Product", "Product", new { title = product.Title }));
        }
Example #2
0
        public async Task <IActionResult> Register(RegistrationModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.EmailAdress, Email = model.EmailAdress
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var internalUser = new User
                    {
                        Username  = model.Username,
                        FirstName = model.FirstName,
                        LastName  = model.LastName,
                        Email     = model.EmailAdress
                    };
                    _context.Add(internalUser);
                    _context.SaveChanges();

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public void AddItem(Product product, int amount)
        {
            var cartItem = _dbContext.Items.SingleOrDefault(x => x.Product.Id == product.Id && x.CartId == CartId);

            if (cartItem == null)
            {
                cartItem = new Item
                {
                    CartId   = CartId,
                    Product  = product,
                    Quantity = 1
                };
                _dbContext.Items.Add(cartItem);
            }

            else
            {
                cartItem.Quantity++;
            }

            _dbContext.SaveChanges();
        }