Ejemplo n.º 1
0
        //GET: Products/Verkoop
        public async Task <IActionResult> Verkoop(int?id)
        {
            if (id != null)
            {
                var customer = await _context.Customers.Include(c => c.Bags).Where(c => c.CustomerID == id).FirstOrDefaultAsync();

                Shoppingbag bag;
                if (customer.Bags.Count == 0)
                {
                    bag = new Shoppingbag()
                    {
                        Date = DateTime.Now, Customer = customer, CustomerID = customer.CustomerID
                    };
                    _context.Add(bag);
                    await _context.SaveChangesAsync();

                    //customer.Bags.Add(bag);
                }
                else
                {
                    bag = customer.Bags.FirstOrDefault(b => b.Closed == false);
                }
                ViewData["Customer"]    = customer;
                ViewData["Shoppingbag"] = bag;
            }
            return(View(await _context.Products.ToListAsync()));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 3
0
        public async Task <Order> CreateAsync(Order order)
        {
            _dbContext.Add(order);
            await _dbContext.SaveChangesAsync();

            return(order);
        }
        public async Task <IActionResult> PutCustomer(int id, Customer customer)
        {
            if (id != customer.Id)
            {
                return(BadRequest());
            }

            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 5
0
        public async Task <Cart> CreateAsync(Cart cart)
        {
            _dbContext.Add(cart);
            await _dbContext.SaveChangesAsync();

            return(cart);
        }
Ejemplo n.º 6
0
        public async Task <User> CreateAsync(User user)
        {
            _dbContext.Add(user);
            await _dbContext.SaveChangesAsync();

            return(user);
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.Id)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutOrderLine(int id, OrderLine orderLine)
        {
            if (id != orderLine.Id)
            {
                return(BadRequest());
            }

            _context.Entry(orderLine).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderLineExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 9
0
        public async Task <ActionResult> Post([FromBody] UserInformation newUserInfo)
        {
            if (userExists(newUserInfo.Email))
            {
                return(StatusCode(409, "Gebruiker bestaat al."));
            }
            User newUser = new User()
            {
                Email          = newUserInfo.Email,
                Password       = UserHelper.HashPassword(newUserInfo.Password),
                FirstName      = newUserInfo.FirstName,
                Prefix         = newUserInfo.Prefix,
                LastName       = newUserInfo.LastName,
                Street         = newUserInfo.Street,
                BuildingNumber = newUserInfo.BuildingNumber,
                PostalCode     = newUserInfo.PostalCode,
                Area           = newUserInfo.Area,
                DiscountPoints = 0,
                Admin          = false,
                Removed        = false
            };
            User currentUser = GetClaimUser();

            if (currentUser != null && currentUser.Admin)
            {
                newUser.Admin          = newUserInfo.Admin.HasValue ? newUserInfo.Admin.Value : false;
                newUser.DiscountPoints = newUserInfo.DiscountPoints.HasValue ? newUserInfo.DiscountPoints.Value : 0;
            }
            db.Users.Add(newUser);
            await db.SaveChangesAsync();

            return(StatusCode(201));
        }
Ejemplo n.º 10
0
        public async Task <Discount> CreateAsync(CreateDiscount createDiscount)
        {
            var discount = _mapper.Map <Discount>(createDiscount);

            _dbContext.Add(discount);
            await _dbContext.SaveChangesAsync();

            return(discount);
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> Create(Productsoort productsoort)
        {
            if (ModelState.IsValid)
            {
                _context.Productsoort.Add(productsoort);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(productsoort));
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> Create([Bind("ProductID,Name,Imagepath,Description,Price,Stock")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> Create([Bind("Id,Name,Email,BirthDate,CustomerGender")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(customer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
Ejemplo n.º 14
0
        public async Task <ActionResult> AddCountry([FromBody] Country newCountry)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            db.Country.Add(newCountry);
            await db.SaveChangesAsync();

            return(StatusCode(201));
        }
Ejemplo n.º 15
0
        public async Task <IActionResult> Create([Bind("Id,Code,UnitPrice")] Products products)
        {
            if (ModelState.IsValid)
            {
                _context.Add(products);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(products));
        }
Ejemplo n.º 16
0
        public async Task <ActionResult> AddBrand([FromBody] Brand newBrand)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            db.Brand.Add(newBrand);
            await db.SaveChangesAsync();

            return(StatusCode(201));
        }
Ejemplo n.º 17
0
        public async Task <ActionResult> AddProduct([FromBody] Product newProduct)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            await db.Product.AddAsync(newProduct);

            await db.SaveChangesAsync();

            return(CreatedAtAction("GetProductById", new { id = newProduct.Id }, newProduct));
        }
Ejemplo n.º 18
0
        public async Task <IActionResult> Create([Bind("Id,ClientId,ProductId,Quantity")] Order order)
        {
            if (ModelState.IsValid)
            {
                _context.Add(order);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View());
        }
        // 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));
        }
Ejemplo n.º 20
0
        public async Task <IActionResult> Create(CaffFileViewModel caff)
        {
            if (ModelState.IsValid)
            {
                CaffFile newCaffFile = new CaffFile
                {
                    UserId  = int.Parse(_userManager.GetUserId(User)),
                    Comment = caff.Comment
                };

                var isAuthorized = await _authorizationService.AuthorizeAsync(
                    User, newCaffFile,
                    CaffFileOperations.Create);

                if (!isAuthorized.Succeeded)
                {
                    return(Unauthorized());
                }

                if (caff.Content != null)
                {
                    // TODO: Add the file uploader function here that returns the path of the uploaded CAFF file
                    var verified = await VerifyAndUploadCaffFileAsync(caff.Content);


                    if (verified == null)
                    {
                        return(View("Error"));
                    }
                    var caffPath  = verified[0];
                    var imagePath = verified[2];

                    newCaffFile.Path      = caffPath;
                    newCaffFile.ImagePath = imagePath;
                    _context.CaffFiles.Add(newCaffFile);

                    await _context.SaveChangesAsync();

                    _logger.LogInformation("User : "******"  Action: Uploaded a new CAFF. CaffPath: " + caffPath);
                    _logger.LogInformation("User : "******"  Action: Uploaded a new CAFF. ImagePath: " + imagePath);
                }

                return(RedirectToAction(nameof(Index)));
            }
            _logger.LogInformation("User : "******"  Action: The model is invalid.");

            return(View(caff));
        }
Ejemplo n.º 21
0
        public async Task <Address> CreateAsync(Address address)
        {
            _dbContext.Add(address);
            await _dbContext.SaveChangesAsync();

            return(address);
        }
Ejemplo n.º 22
0
        public async Task <Payment> CreateAsync(Payment payment)
        {
            _dbContext.Add(payment);
            await _dbContext.SaveChangesAsync();

            return(payment);
        }
Ejemplo n.º 23
0
        public async Task <IEnumerable <OrderViewModel> > Checkout(OrderDto model, CancellationToken cancellationToken = default)
        {
            AppUser user = await _authService.GetUser(_user, cancellationToken);

            IQueryable <CartItem> cart = _context.CartItems
                                         .Include(ci => ci.Product)
                                         .ThenInclude(p => p.Images)
                                         .Where(ci => ci.UserId == user.UserId);

            IEnumerable <Order> orders = await cart
                                         .ProjectTo <Order>(_mapper.ConfigurationProvider)
                                         .ToListAsync(cancellationToken);

            PaymentMethod paymentMethod = await _context.PaymentMethods
                                          .SingleOrDefaultAsync(pm => pm.PaymentMethodId == model.PaymentMethodId, cancellationToken);

            foreach (Order order in orders)
            {
                order.PaymentMethodId = paymentMethod.PaymentMethodId;
            }

            await _context.Orders.AddRangeAsync(orders, cancellationToken);

            _context.RemoveRange(cart);

            await _context.SaveChangesAsync(cancellationToken);

            IEnumerable <OrderViewModel> viewModels = orders
                                                      .AsQueryable()
                                                      .ProjectTo <OrderViewModel>(_mapper.ConfigurationProvider)
                                                      .ToList();

            return(viewModels);
        }
Ejemplo n.º 24
0
        public async Task Upload(string file)
        {
            ProductImage image = await _context.ProductImages.SingleOrDefaultAsync(pi => pi.ProductImageId == 1);

            image.Image = file;

            _context.ProductImages.Update(image);
            await _context.SaveChangesAsync();
        }
        public async Task <IActionResult> Aanmaak([Bind("CustomerID,Name,Firstname")] Customer customer)
        {
            var loginuser = await _context.Customers.FindAsync(1);

            ViewData["Customer"] = loginuser;
            if (ModelState.IsValid)
            {
                var checklogin = await _context.Customers.Where(s => s.Firstname == customer.Firstname && s.Name == customer.Name).FirstOrDefaultAsync();

                if (checklogin == null)
                {
                    _context.Add(customer);
                    await _context.SaveChangesAsync();

                    checklogin = customer;
                }
                return(RedirectToAction("Verkoop", "Products", new { id = checklogin.CustomerID }));
            }
            return(View(customer));
        }
        public async Task <IActionResult> PutLogin(int id, Login login)
        {
            if (id != login.Id)
            {
                return(BadRequest());
            }

            // prevent updateing a Login and removing the attached role
            if (login.Role == null && login.RoleId < 1)
            {
                return(BadRequest());
            }

            _context.Entry(login).State = EntityState.Modified;


            if (login.Password != null)
            {
                login.Password = BC.HashPassword(login.Password);
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LoginExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 27
0
        public async Task <WishlistItemViewModel> AddWishlistItem(int productId, CancellationToken cancellationToken = default)
        {
            Product product = await _context.Products
                              .Include(p => p.Images)
                              .SingleOrDefaultAsync(p => p.ProductId == productId, cancellationToken);

            AppUser user = await _authService.GetUser(_user, cancellationToken);

            WishlistItem item = new WishlistItem
            {
                User    = user,
                Product = product,
            };

            await _context.WishlistItems.AddAsync(item, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            WishlistItemViewModel viewModel = _mapper.Map <WishlistItem, WishlistItemViewModel>(item);

            return(viewModel);
        }
Ejemplo n.º 28
0
        public async Task <IActionResult> PostCartItem([FromBody] PostCartItemArgs data)
        {
            long id        = data.id;
            long productid = data.productid;
            long pieces    = data.pieces;

            var cart = await _context.Carts.FindAsync(id).ConfigureAwait(false);

            if (cart == null)
            {
                return(NotFound());
            }

            var orderitemquery = from oi in _context.OrderItems
                                 where (oi.CartID == id && oi.ProductID == productid)
                                 select oi;

            var orderitem = await orderitemquery.FirstOrDefaultAsync().ConfigureAwait(false);

            if (orderitem == null)
            {
                _context.OrderItems.Add(new OrderItems {
                    CartID = id, Pieces = pieces, ProductID = productid
                });
                await _context.SaveChangesAsync();
            }
            else
            {
                orderitem.Pieces += pieces;
                if (orderitem.Pieces == 0)
                {
                    _context.OrderItems.Remove(orderitem);
                }
                await _context.SaveChangesAsync();
            }

            return(NoContent());
        }
Ejemplo n.º 29
0
        public async Task <ReviewViewModel> CreateReview(int productId, ReviewDto model, CancellationToken cancellationToken = default)
        {
            Product product = await _context.Products
                              .SingleOrDefaultAsync(p => p.ProductId == productId, cancellationToken);

            AppUser user = await _authService.GetUser(_user, cancellationToken);

            Review review = new Review
            {
                User      = user,
                Product   = product,
                Body      = model.Body,
                Stars     = model.Stars,
                CreatedAt = DateTime.Now,
            };

            await _context.Reviews.AddAsync(review, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            ReviewViewModel viewModel = _mapper.Map <Review, ReviewViewModel>(review);

            return(viewModel);
        }
Ejemplo n.º 30
0
        public async Task Register(RegisterDto model, CancellationToken cancellationToken = default)
        {
            AppUser user = _mapper.Map <RegisterDto, AppUser>(model);

            // Generate salt + password hash
            byte[] salt = _cryptoService.GenerateSalt();
            byte[] hash = _cryptoService.HashPassword(model.Password, salt);

            user.PasswordHash = hash;
            user.PasswordSalt = salt;
            user.CreatedAt    = DateTime.Now;

            // Save user to database
            await _context.Users.AddAsync(user, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);
        }