public AuthenticateResponse Authenticate(AuthenticateRequest model, string ipAddress)
        {
            var login = _context.Logins.Include(l => l.Role).SingleOrDefault(x => x.Email == model.Email);

            // return null if user not found
            if (login == null)
            {
                return(null);
            }

            if (!BCrypt.Net.BCrypt.Verify(model.Password, login.Password))
            {
                return(null);
            }
            // authentication successful so generate jwt and refresh tokens
            var jwtToken     = generateJwtToken(login);
            var refreshToken = generateRefreshToken(ipAddress);

            // save refresh token
            login.RefreshTokens.Add(refreshToken);
            _context.Update(login);
            _context.SaveChanges();

            return(new AuthenticateResponse(login, jwtToken, refreshToken.Token));
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("ShoppingitemID,Quantity,ProductID,ShoppingbagID")] Shoppingitem shoppingitem)
        {
            if (ModelState.IsValid)
            {
                if (shoppingitem.Quantity == 0)
                {
                    return(RedirectToAction("Overzicht", "Shoppingbags", new { id = shoppingitem.ShoppingbagID }));
                }
                var checkproduct = await _context.Shoppingitems.Include(i => i.Product).Where(i => i.ProductID == shoppingitem.ProductID && i.ShoppingbagID == shoppingitem.ShoppingbagID).FirstOrDefaultAsync();

                if (checkproduct == null)
                {
                    _context.Add(shoppingitem);
                    await _context.SaveChangesAsync();

                    checkproduct = shoppingitem;
                }
                else
                {
                    checkproduct.Quantity += shoppingitem.Quantity;
                    _context.Update(checkproduct);
                    await _context.SaveChangesAsync();
                }
                return(RedirectToAction("Overzicht", "Shoppingbags", new { id = checkproduct.ShoppingbagID }));

                //else quantity aanpassen
                //redirect naar shoppingbag overzicht (new view parameters=customerid,shoppingbagid
            }
            ViewData["ProductID"]     = new SelectList(_context.Products, "ProductID", "Name", shoppingitem.ProductID);
            ViewData["ShoppingbagID"] = new SelectList(_context.Shoppingbags, "ShoppingbagID", "ShoppingbagID", shoppingitem.ShoppingbagID);
            return(View(shoppingitem));
        }
예제 #3
0
        public ActionResult AdminEditCartItems([FromBody] CartViewModel _cartItem, int Id)
        {
            CartViewModelValidator validator = new CartViewModelValidator();
            ValidationResult       results   = validator.Validate(_cartItem);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    Errors.AddErrorToModelState(failure.PropertyName, failure.ErrorMessage, ModelState);
                }
            }

            if (!ModelState.IsValid || _cartItem.CartQuantity == 0)
            {
                return(BadRequest(ModelState));
            }

            if (_context.Users.Find(Id) != null)
            {
                var cartItem = _context.CartProducts
                               .Where(c => c.Cart.UserId == Id && c.ProductId == _cartItem.ProductId)
                               .Select(a => a).ToArray();

                if (cartItem.Length == 0)
                {
                    return(NotFound());
                }

                var oldQuantity = cartItem[0].CartQuantity;
                var stockid     = (_context.Stock.Where(s => s.Product.Id == _cartItem.ProductId).Select(p => p.Id)).ToArray().First();
                var stock       = _context.Stock.Find(stockid);

                if (stock.ProductQuantity + oldQuantity < _cartItem.CartQuantity)
                {
                    cartItem[0].CartQuantity = stock.ProductQuantity + oldQuantity;
                    stock.ProductQuantity    = 0;
                }

                else
                {
                    cartItem[0].CartQuantity = _cartItem.CartQuantity;
                    stock.ProductQuantity    = stock.ProductQuantity + oldQuantity - _cartItem.CartQuantity;
                }

                TotalPrice(cartItem[0].CartId);

                _context.Update(stock);
                _context.CartProducts.Update(cartItem[0]);
                _context.SaveChanges();

                return(Ok());
            }
            return(NotFound());
        }
        // GET: Shoppingbags
        public async Task <IActionResult> Index()
        {
            var webshopContext = await _context.Shoppingbags.Include(s => s.Customer).Include(s => s.Shoppingitems).ThenInclude(item => item.Product).ToListAsync();

            foreach (var item in webshopContext)
            {
                item.BerekenTotalPrice();
                _context.Update(item);
            }
            await _context.SaveChangesAsync();

            return(View(webshopContext));
        }
예제 #5
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Code,UnitPrice")] Products products)
        {
            if (id != products.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(products);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductsExists(products.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(products));
        }
예제 #6
0
        public Task UpdateAsync(UpdateDiscount updateDiscount)
        {
            var discount = _mapper.Map <Discount>(updateDiscount);

            _dbContext.Update(discount);
            return(_dbContext.SaveChangesAsync());
        }
예제 #7
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,ClientId,ProductId,Quantity")] Order order)
        {
            if (id != order.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(order);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrderExists(order.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(order));
        }
        public async Task <IActionResult> Edit(int id, [Bind("CustomerID,Name,Firstname")] Customer customer)
        {
            var loginuser = await _context.Customers.FindAsync(1);

            ViewData["Customer"] = loginuser;
            if (id != customer.CustomerID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(customer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.CustomerID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
예제 #9
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Email,BirthDate,CustomerGender")] Customer customer)
        {
            if (id != customer.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(customer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
예제 #10
0
        public async Task <IActionResult> Edit(long id, [Bind("ProductID,Name,Imagepath,Description,Price,Stock")] Product product)
        {
            if (id != product.ProductID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(product);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.ProductID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
예제 #11
0
        //Gets input from the body that is type Product (in Json)
        public void UpdateExistingProduct(int id, [FromBody] Product product)
        {
            //Find all products that has the given id in table Products
            Product p = _context.Products.Find(id);

            //Check if there is any input(value) for the attributes
            //If there is input, assign the new value to the attribute
            if (product.ProductNumber != null)
            {
                p.ProductNumber = product.ProductNumber;
            }
            if (product.ProductEAN != null)
            {
                p.ProductEAN = product.ProductEAN;
            }
            if (product.ProductInfo != null)
            {
                p.ProductInfo = product.ProductInfo;
            }
            if (product.ProductDescription != null)
            {
                p.ProductDescription = product.ProductDescription;
            }
            if (product.ProductSpecification != null)
            {
                p.ProductSpecification = product.ProductSpecification;
            }
            if (product.ProductPrice != 0)
            {
                p.ProductPrice = product.ProductPrice;
            }
            if (product.ProductColor != null)
            {
                p.ProductColor = product.ProductColor;
            }
            if (product._TypeId != 0)
            {
                p._TypeId = product._TypeId;
            }
            if (product.CategoryId != 0)
            {
                p.CategoryId = product.CategoryId;
            }
            if (product.CollectionId != 0)
            {
                p.CollectionId = product.CollectionId;
            }
            if (product.BrandId != 0)
            {
                p.BrandId = product.BrandId;
            }
            if (product.StockId != 0)
            {
                p.StockId = product.StockId;
            }
            //Update the changes to the table and save
            _context.Update(p);
            _context.SaveChanges();
        }
예제 #12
0
 public Task UpdateAsync(User user)
 {
     _dbContext.Update(user);
     return(_dbContext.SaveChangesAsync());
 }
예제 #13
0
        public void UpdateProduct(dynamic U_product, int id)
        {
            dynamic U_product_JSON = JsonConvert.DeserializeObject(U_product.ToString());
            Product p = _context.Products.Find(id);

            if (U_product_JSON.ProductName != null)
            {
                p.ProductName = U_product_JSON.ProductName;
            }
            if (U_product_JSON.ProductEAN != null)
            {
                p.ProductEAN = U_product_JSON.ProductEAN;
            }
            if (U_product_JSON.ProductNumber != null)
            {
                p.ProductNumber = U_product_JSON.ProductNumber;
            }
            if (U_product_JSON.ProductInfo != null)
            {
                p.ProductInfo = U_product_JSON.ProductInfo;
            }
            if (U_product_JSON.ProductDescription != null)
            {
                p.ProductDescription = U_product_JSON.ProductDescription;
            }
            if (U_product_JSON.ProductSpecification != null)
            {
                p.ProductSpecification = U_product_JSON.ProductSpecification;
            }
            if (U_product_JSON.ProductPrice != null)
            {
                p.ProductPrice = U_product_JSON.ProductPrice;
            }
            if (U_product_JSON.ProductColor != null)
            {
                p.ProductColor = U_product_JSON.ProductColor;
            }
            if (U_product_JSON._TypeId != null)
            {
                p._TypeId = U_product_JSON._TypeId;
            }
            if (U_product_JSON.CategoryId != null)
            {
                p.CategoryId = U_product_JSON.CategoryId;
            }
            if (U_product_JSON.CollectionId != null)
            {
                p.CollectionId = U_product_JSON.CollectionId;
            }
            if (U_product_JSON.BrandId != null)
            {
                p.BrandId = U_product_JSON.BrandId;
            }
            _context.Update(p);
            Stock s = _context.Stock.Find(p.StockId);

            if (U_product_JSON.ProductQuantity != null)
            {
                s.ProductQuantity = U_product_JSON.ProductQuantity;
            }
            _context.Update(s);
            if (U_product_JSON._TypeId != null)
            {
                int   TId = U_product_JSON._TypeId;
                _Type t   = _context.Types.Find(TId);
                if (U_product_JSON._TypeName != null)
                {
                    t._TypeName = U_product_JSON._TypeName;
                }
                _context.Update(t);
            }
            if (U_product_JSON.CollectionId != null)
            {
                int        CId = U_product_JSON.CollectionId;
                Collection c   = _context.Collections.Find(CId);
                if (U_product_JSON.CollectionName != null)
                {
                    c.CollectionName = U_product_JSON.CollectionName;
                }
                _context.Update(c);
            }
            if (U_product_JSON.CategoryId != null)
            {
                int      CatId = U_product_JSON.CategoryId;
                Category cat   = _context.Categories.Find(CatId);
                if (U_product_JSON.CategoryId != null)
                {
                    cat.CategoryName = U_product_JSON.CategoryName;
                }
                _context.Update(cat);
            }
            if (U_product_JSON.BrandId != null)
            {
                int   BId = U_product_JSON.BrandId;
                Brand b   = _context.Brands.Find(BId);
                if (U_product_JSON.BrandId != null)
                {
                    b.BrandName = U_product_JSON.BrandName;
                }
                _context.Update(b);
            }
            int          IId = _context.ProductImages.First(a => a.ProductId == id).Id;
            ProductImage i   = _context.ProductImages.Find(IId);

            if (U_product_JSON.ImageURL != null)
            {
                i.ImageURL = U_product_JSON.ImageURL;
            }
            _context.Update(i);
            _context.SaveChanges();
        }
예제 #14
0
 public Task UpdateProductAsync(CartProduct cartProduct)
 {
     _dbContext.Update(cartProduct);
     return(_dbContext.SaveChangesAsync());
 }
예제 #15
0
        public ActionResult Order(dynamic OrderDetailsJson)
        {
            dynamic OrderDetails = JsonConvert.DeserializeObject(OrderDetailsJson.ToString());

            int?   userId;
            int    addressId;
            double totalPrice;
            string orderPaymentMethod = OrderDetails.OrderPaymentMethod;
            int    availableOrderId   = 1;

            if (_context.Orders.Count() != 0)
            {
                availableOrderId = _context.Orders.Select(a => a.Id).Max() + 1;
            }

            if (_caller.HasClaim(claim => claim.Type == "id"))
            {
                userId = int.Parse((_caller.Claims.Single(claim => claim.Type == "id")).Value);

                addressId = (from item in _context.UserAddress
                             where item.UserId == userId
                             select item.AddressId).ToArray().FirstOrDefault();

                var cartItems = (from cart in _context.Carts
                                 where cart.UserId == userId
                                 let cartentry = (from entries in _context.CartProducts
                                                  where entries.CartId == cart.Id
                                                  select entries)
                                                 select cartentry).ToArray().First();

                totalPrice = (from cart in _context.Carts
                              where cart.UserId == userId
                              select cart.CartTotalPrice).ToArray().First();

                if (OrderDetails.Discount >= 0)
                {
                    double discounter = OrderDetails.Discount;
                    totalPrice = totalPrice - (totalPrice * (discounter / 100));
                }

                var o = new Order
                {
                    Id                 = availableOrderId,
                    UserId             = userId,
                    AddressId          = addressId,
                    OrderStatusId      = 1,
                    OrderTotalPrice    = totalPrice,
                    OrderPaymentMethod = orderPaymentMethod,
                    OrderDate          = DateTime.Now
                };
                _context.Orders.Add(o);

                foreach (var item in cartItems)
                {
                    int productItem      = item.ProductId;
                    var productSelection = _context.OrderProduct.Where(x => x.OrderId == o.Id && x.ProductId == productItem).Select(x => x);

                    if (productSelection.Count() == 1)
                    {
                        productSelection.FirstOrDefault().OrderQuantity = productSelection.FirstOrDefault().OrderQuantity + 1;
                        _context.Update(productSelection);
                    }

                    else
                    {
                        var orderproduct = new OrderProduct
                        {
                            OrderId       = o.Id,
                            ProductId     = item.ProductId,
                            OrderQuantity = item.CartQuantity
                        };
                        _context.OrderProduct.Add(orderproduct);
                        _context.CartProducts.Remove(item);
                    }
                }

                _context.SaveChanges();
                TotalPrice(_context.Users.Where(x => x.Id == userId).Select(x => x.Cart.Id).FirstOrDefault());
            }

            else
            {
                userId = null;
                int availableAddressId = 1;
                if (_context.Addresses.Count() != 0)
                {
                    availableAddressId = _context.Addresses.Select(a => a.Id).Max() + 1;
                }

                Address addressUser = new Address
                {
                    Id          = availableAddressId,
                    Street      = OrderDetails.Street,
                    City        = OrderDetails.City,
                    ZipCode     = OrderDetails.ZipCode,
                    HouseNumber = OrderDetails.HouseNumber
                };
                _context.Addresses.Add(addressUser);

                addressId  = addressUser.Id;
                totalPrice = OrderDetails.totalPrice;
                var cartItems = OrderDetails.cartItems;

                var o = new Order
                {
                    Id                 = availableOrderId,
                    UserId             = userId,
                    AddressId          = addressId,
                    OrderStatusId      = 1,
                    OrderTotalPrice    = totalPrice,
                    OrderDate          = DateTime.Now,
                    OrderPaymentMethod = orderPaymentMethod,
                };
                _context.Orders.Add(o);


                foreach (var item in cartItems)
                {
                    int productItem      = item.ProductId;
                    var productSelection = _context.OrderProduct.Where(x => x.OrderId == o.Id && x.ProductId == productItem).Select(x => x);

                    if (productSelection.Count() == 1)
                    {
                        productSelection.FirstOrDefault().OrderQuantity = productSelection.FirstOrDefault().OrderQuantity + 1;
                        _context.Update(productSelection);
                    }

                    else
                    {
                        var orderproduct = new OrderProduct
                        {
                            OrderId       = o.Id,
                            ProductId     = item.ProductId,
                            OrderQuantity = item.CartQuantity
                        };
                        _context.OrderProduct.Add(orderproduct);
                    }
                }

                _context.SaveChanges();
            }

            return(Ok());
        }
예제 #16
0
 public Task UpdateAsync(Membership membership)
 {
     _dbContext.Update(membership);
     return(_dbContext.SaveChangesAsync());
 }
예제 #17
0
        public async Task <ActionResult> Post([FromBody] OrderProducts requestOrder)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Order order = new Order();

            order.OrderStatus    = OrderStatusEnum.Ordered;
            order.OrderDate      = DateTime.Now;
            order.TaxPercentage  = 21;
            order.Email          = requestOrder.Email;
            order.FirstName      = requestOrder.FirstName;
            order.Prefix         = requestOrder.Prefix;
            order.LastName       = requestOrder.LastName;
            order.Street         = requestOrder.Street;
            order.BuildingNumber = requestOrder.BuildingNumber;
            order.PostalCode     = requestOrder.PostalCode;
            order.Area           = requestOrder.Area;
            order.OrderProducts  = new List <OrderProduct>();

            var productsAmount = (
                from p in db.Product
                from p_a in requestOrder.orderProductAmount
                where p_a.ProductId == p.Id
                select new {
                product = p,
                amount = p_a.Amount
            }
                ).ToArray();


            foreach (var productAmount in productsAmount)
            {
                order.OrderProducts.Add(new OrderProduct()
                {
                    Product = productAmount.product,
                    Price   = productAmount.product.Price,
                    Amount  = productAmount.amount
                });
                productAmount.product.Inventory -= productAmount.amount;
                db.Update(productAmount.product);
            }

            User user = GetClaimUser();

            if (user != null)
            {
                order.User = user;
                if (requestOrder.SaveAddress)
                {
                    user.Street         = requestOrder.Street;
                    user.BuildingNumber = requestOrder.BuildingNumber;
                    user.PostalCode     = requestOrder.PostalCode;
                    user.Area           = requestOrder.Area;
                }
                if (user.DiscountPoints == 10)
                {
                    order.DiscountPercentage = this.discountPercentage;
                    user.DiscountPoints      = 0;
                }
                else
                {
                    user.DiscountPoints++;
                }
                db.Update(user);
            }

            db.Add(order);
            await db.SaveChangesAsync();

            return(CreatedAtAction("GetOrder", new { id = order.Id }, order.Id));
        }
예제 #18
0
 public Task UpdateAsync(Product product)
 {
     _dbContext.Update(product);
     return(_dbContext.SaveChangesAsync());
 }