public IActionResult AddToCart(ProductDetailsViewModel viewModel)
        {
            var    item   = _repository.GetProductById(viewModel.ProductId);
            var    user   = _userManager.GetUserAsync(User).Result;
            string userId = user.Id;

            var cart = _repository.GetCartByUserId(userId);

            if (ModelState.IsValid)
            {
                if (cart == null)
                {
                    var cartModel = new Cart();
                    cartModel.User     = user;
                    cartModel.CartItem = new List <CartItem>()
                    {
                        new CartItem()
                        {
                            Product  = item,
                            Quantity = viewModel.Quantity,
                            Size     = viewModel.Size,
                            CartId   = cartModel.Id
                        }
                    };
                    cartModel.TotalPrice += item.Price * viewModel.Quantity;


                    _repository.AddEntity(cartModel);


                    if (_repository.SaveAll())
                    {
                        //var result = new { Success = "true", message = "Uspešno ste dodali artikal u korpu. " };
                        //return Json(result);
                        FlashMessage.Info("Uspešno ste dodali artikal u korpu. ");
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        throw new Exception("Nije uspelo");
                    }
                }
                else
                {
                    item = _repository.GetProductById(viewModel.ProductId);
                    viewModel.Product = item;

                    if (_repository.IsThereSameProductInTheCart(cart.Id, item.Id) != false)
                    {
                        //var result = new { Success = "true", message = "Već ste dodali ovaj artikal u korpu. " };
                        //return Json(result);


                        ModelState.AddModelError("Product", "Već ste dodali ovaj artikal u korpu. ");
                        return(View("../Home/Details", viewModel));
                    }
                    else
                    {
                        cart.CartItem.Append(
                            new CartItem()
                        {
                            Product  = item,
                            Quantity = viewModel.Quantity,
                            Size     = viewModel.Size,
                            CartId   = cart.Id
                        }
                            );



                        cart.TotalPrice += item.Price * viewModel.Quantity;



                        _repository.AddEntity(new CartItem()
                        {
                            Product  = item,
                            Quantity = viewModel.Quantity,
                            Size     = viewModel.Size,
                            CartId   = cart.Id
                        });

                        _repository.UpdateCartWithNewCartItem(cart);

                        if (_repository.SaveAll())
                        {
                            //return Json(new { Success = true });
                            FlashMessage.Info("Uspešno ste dodali artikal u korpu. ");
                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            //var result = new { Success = "true", message = "Uspešno ste dodali artikal u korpu. " };
                            //return Json(result);

                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                }
            }
            else
            {
                viewModel.Sizes   = _repository.GetSizesById(item.Id);
                viewModel.Product = item;
                return(View("../Home/Details", viewModel));
            }
        }
        public IActionResult AddToWishList(Product viewModel)
        {
            if (User.Identity.IsAuthenticated)
            {
                var    item   = _repository.GetProductById(viewModel.Id);
                var    user   = _userManager.GetUserAsync(User).Result;
                string userId = user.Id;

                var wishList = _repository.GetWishListByUserId(userId);
                if (ModelState.IsValid)
                {
                    if (wishList == null)
                    {
                        var wishListModel = new WishList();
                        wishListModel.User          = user;
                        wishListModel.WishListItems = new List <WishListItem>()
                        {
                            new WishListItem()
                            {
                                Product    = item,
                                WishListId = wishListModel.Id
                            }
                        };


                        _repository.AddEntity(wishListModel);


                        if (_repository.SaveAll())
                        {
                            //var result = new { Success = "true", message = "Uspešno ste dodali artikal u korpu. " };
                            //return Json(result);
                            FlashMessage.Info("Uspešno ste dodali artikal u listu želja. ");
                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            throw new Exception("Nije uspelo");
                        }
                    }
                    else
                    {
                        item      = _repository.GetProductById(viewModel.Id);
                        viewModel = item;

                        if (_repository.IsThereSameProductInTheWishList(wishList.Id, item.Id) != false)
                        {
                            //var result = new { Success = "true", message = "Već ste dodali ovaj artikal u korpu. " };
                            //return Json(result);


                            ModelState.AddModelError("Product", "Već ste dodali ovaj artikal u listu želja. ");
                            return(View("../Home/Details", viewModel));
                        }
                        else
                        {
                            wishList.WishListItems.Append(
                                new WishListItem()
                            {
                                Product    = item,
                                WishListId = wishList.Id
                            });


                            _repository.AddEntity(new WishListItem()
                            {
                                Product    = item,
                                WishListId = wishList.Id
                            });

                            _repository.UpdateEntity(wishList);

                            if (_repository.SaveAll())
                            {
                                //return Json(new { Success = true });
                                FlashMessage.Info("Uspešno ste dodali artikal u listu želja. ");
                                return(RedirectToAction("Index", "Home"));
                            }
                            else
                            {
                                //var result = new { Success = "true", message = "Uspešno ste dodali artikal u korpu. " };
                                //return Json(result);

                                return(RedirectToAction("Index", "Home"));
                            }
                        }
                    }
                }
                else
                {
                    viewModel = item;
                    return(View("../Home/Details", viewModel));
                }
            }
            else
            {
                return(RedirectToAction("Login", "Account"));
            }
        }
示例#3
0
        public async Task <IActionResult> ConfirmOrderAsync(CheckoutViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                var cart      = _repository.GetCartByUserId(user.Id);
                var cartItem  = _repository.GetCartItemByCartId(cart.Id);
                var countries = _repository.GetCountries();
                model.Country = countries;
                cart.CartItem = cartItem;

                MimeMessage message = new MimeMessage();

                MailboxAddress from = new MailboxAddress("DevitoAnonymo porudžbina",
                                                         _configuration["from"]);
                message.From.Add(from);

                InternetAddressList list = new InternetAddressList();
                list.Add(new MailboxAddress(user.FirstName + " " + user.LastName, user.Email)); //user email
                list.Add(new MailboxAddress("MKOClothing", "*****@*****.**"));       //MKOClothing email
                list.Add(new MailboxAddress("DevitoAnonytmo office", "*****@*****.**"));    //davidov email
                message.To.AddRange(list);
                //--------------------------------------------
                message.Subject = "Nova porudžbina";

                var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);


                var order = new Order
                {
                    Orderdate     = DateTime.Now,
                    User          = user,
                    PaymentMethod = model.PaymentMethod,
                    OrderNumber   = cart.Id + user.PhoneNumber + cart.CartItem.FirstOrDefault().Id + Math.Round((DateTime.Now.ToUniversalTime() - epoch).TotalSeconds)
                };

                var mailHtmlBody = "<h1>Porudžbina - DevitoAnonymo.com</h1><br/><hr/><table><tbody>" +
                                   "<tr><th>Broj porudžbine</th><td style='width: 200px'>" + order.OrderNumber + "</td></tr>" +
                                   "<tr><th>Ime</th><td style='width: 200px'>" + model.FirstName + "</td></tr>" +
                                   "<tr><th>Prezime</th><td style='width: 200px'>" + model.LastName + "</td></tr>" +
                                   "<tr><th>Adresa</th><td style='width: 200px'>" + model.Address + "</td></tr>" +
                                   "<tr><th>Poštanski broj</th><td style='width: 200px'>" + model.PostalNumber + "</td></tr>" +
                                   "<tr><th>Grad</th><td style='width: 200px'>" + model.City + "</td></tr>" +
                                   "<tr><th>Država</th><td style='width: 200px'>" + model.Country.SingleOrDefault(c => c.Id == model.CountryId).Title + "</td></tr>" +
                                   "<tr><th>Broj telefona</th><td style='width: 200px'>" + model.PhoneNumber + "</td></tr>";


                if (model.PaymentMethod == PaymentMethod.Pouzećem)
                {
                    mailHtmlBody += "<tr><th>Ukupna cena za naplatu: </th><td style='width: 200px'>" + cart.TotalPrice + " RSD</td></tr>";
                }
                else
                {
                    if (model.PaymentMethod == PaymentMethod.Paypal)
                    {
                        mailHtmlBody += "<tr><th>Ukupna cena za naplatu: </th><td style='width: 400px;color:green'>" + cart.TotalPrice + " RSD<strong> - PLAĆENO PUTEM PAYPAL-A</strong></td></tr>";
                    }
                }

                var orderItems = new List <OrderItem>();

                mailHtmlBody +=
                    "</tbody></table><hr/>" +
                    "<h3>Naručeni artikli: </h3><hr/>";
                foreach (var c in cart.CartItem)
                {
                    mailHtmlBody += "<table><tbody>" +
                                    "<tr><th>Broj artikla: </th><td style='width: 200px'>" + c.Product.ItemNumber + "</td></tr>" +
                                    "<tr><th>Naziv: </th><td style='width: 200px'>" + c.Product.Title + "</td></tr>" +
                                    "<tr><th>Veličina: </th><td style='width: 200px'>" + c.Size + "</td></tr>" +
                                    "<tr><th>Količina: </th><td style='width: 200px'>" + c.Quantity + "</td></tr>" +
                                    "</tbody></table><hr/>";



                    var orderItem = new OrderItem
                    {
                        Product   = c.Product,
                        Quantity  = c.Quantity,
                        Size      = c.Size,
                        UnitPrice = c.Product.Price
                    };

                    orderItems.Add(orderItem);
                }

                mailHtmlBody += "<br/><p><strong>Ukoliko postoji problem sa porudžbinom, javite se na email: [email protected]</strong></p>";

                order.OrderItems = orderItems;


                BodyBuilder bodyBuilder = new BodyBuilder();
                bodyBuilder.HtmlBody = mailHtmlBody;

                message.Body = bodyBuilder.ToMessageBody();

                SmtpClient client = new SmtpClient();
                await client.ConnectAsync(_configuration["mailHost"], 465, true);

                await client.AuthenticateAsync(_configuration["from"], _configuration["appPass"]);

                await client.SendAsync(message);

                await client.DisconnectAsync(true);

                client.Dispose();

                _repository.AddEntity(order);

                _repository.RemoveEntity(cart);

                if (_repository.SaveAll())
                {
                    _flashMessage.Confirmation("Uspešno ste poručili artikle. Na email vam je poslata priznanica sa detaljima porudžbine. ");
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    _flashMessage.Warning("Greška pri obradi vašeg zahteva. ");
                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                return(View(model));
            }
        }