public void AoAcessarACamadaDeAcessoADadosParaExcluirUmLivroNaoExistente_OMetodoDeveRetornarFalso()
        {
            var usuario = new User
            {
                District = "Centro",
                CellPhone = "3188888888",
                ZipCode = 30625130,
                City = "Belo Horizonte",
                State = "MG",
                Name = "Jô Soares",
                Email = "*****@*****.**",
                Number = 456,
                Address = "Afonso Pena",
                Password = "******",
                Phone = "313333333",
                UserType = "Cliente"
            };

            using (var livrariaContext = new LivrariaTDDContext())
            {
                usuario = livrariaContext.Users.Add(usuario);
                livrariaContext.SaveChanges();
            }

            var pedido = new Order
            {
                User = usuario,
                PaymentType = null,
                Products = new List<Product> { null, null },
                OrderValue = 0M,
                FreightValue = 10.00M,
                TotalValue = 0M + 10.00M
            };

            const int userId = -34;
            var listaIdProducts = new List<int> { -15, -10 };
            const int formaPagamentoId = -5;

            using (var auxContext = new LivrariaTDDContext())
            {
                var countAntes = auxContext.Orders.Count();
                Assert.AreEqual(0, countAntes);
            }

            using (var livrariaContext = new LivrariaTDDContext())
            {
                _repository = new PedidoRepository(livrariaContext);

                var result = _repository.SalvarPedido(pedido, userId, listaIdProducts, formaPagamentoId);

                Assert.False(result);
            }

            using (var auxContext = new LivrariaTDDContext())
            {
                var countDepois = auxContext.Orders.Count();
                Assert.AreEqual(0, countDepois);
            }
        }
 public void SetUp()
 {
     _repository = new Mock<IOrderRepository>();
     _business = new OrderBusiness(_repository.Object);
     _userId = 1;
     _listaIdProdutos = new List<int> { 1, 2 };
     _idFormaDePagamento = 1;
     _pedido = new Order {OrderValue = 100.0M, FreightValue = 10.0M, TotalValue = 110.0M};
 }
        public bool SalvarPedido(Order pedido, int userId, List<int> listaIdProducts, int formaPagamentoId)
        {
            try
            {
                pedido.Products = new List<Product>();

                foreach (var idProduto in listaIdProducts)
                {
                    var produto = _context.Products.FirstOrDefault(x => x.ProductId == idProduto);
                    if (produto != null)
                    {
                        pedido.Products.Add(produto);
                    }
                    else
                    {
                        return false;
                    }
                }

                var usuario = _context.Users.FirstOrDefault(x => x.UserId == userId);
                if(usuario != null)
                {
                    pedido.User = usuario;
                }
                else
                {
                    return false;
                }

                var formaDePagamento =
                    _context.PaymentTypes.FirstOrDefault(x => x.PaymentTypeId == formaPagamentoId);
                if(formaDePagamento != null)
                {
                    pedido.PaymentType = formaDePagamento;
                }
                else
                {
                    return false;
                }

                _context.Orders.Add(pedido);
                _context.SaveChanges();
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }
        public void AoAcessarACamadaDeAcessoADadosParaSalvarUmaCompra_ACompraDeSerSalvaEOMetodoDeveRetornarVerdadeiro()
        {
            var novoLivro1 = new Product
            {
                Name = "Torre Negra",
                Author = "Stephen King",
                Publishing = "Universal",
                Year = 1995,
                Category = Categories.LiteraturaEstrangeira,
                Stock = 5,
                Price = 150.0M,
                Photo = "",
                Status = ProductStatus.Active
            };

            var novoLivro2 = new Product
            {
                Name = "Torre Negra Segunda edição",
                Author = "Stephen King",
                Publishing = "Universal",
                Year = 1998,
                Category = Categories.LiteraturaEstrangeira,
                Stock = 8,
                Price = 170.0M,
                Photo = "",
                Status = ProductStatus.Active
            };

            var formaPagamento = new PaymentType
                {
                    PaymentTypeName = "Boleto Bancário",
                    Icon = ""
                };

            var usuario = new User
                {
                    District = "Centro",
                    CellPhone = "3188888888",
                    ZipCode = 30625130,
                    City = "Belo Horizonte",
                    State = "MG",
                    Name = "Jô Soares",
                    Email = "*****@*****.**",
                    Number = 456,
                    Address = "Afonso Pena",
                    Password = "******",
                    Phone = "313333333",
                    UserType = "Cliente"
                };

            using (var livrariaContext = new LivrariaTDDContext())
            {
                novoLivro1 = livrariaContext.Products.Add(novoLivro1);
                novoLivro2 = livrariaContext.Products.Add(novoLivro2);
                formaPagamento = livrariaContext.PaymentTypes.Add(formaPagamento);
                usuario = livrariaContext.Users.Add(usuario);
                livrariaContext.SaveChanges();
            }

            var pedido = new Order
                {
                    User = usuario,
                    PaymentType = formaPagamento,
                    Products = new List<Product> {novoLivro1, novoLivro2},
                    OrderValue = novoLivro1.Price + novoLivro2.Price,
                    FreightValue = 10.00M,
                    TotalValue = novoLivro1.Price + novoLivro2.Price + 10.00M
                };

            int userId = usuario.UserId;
            var listaIdProducts = new List<int>{ novoLivro1.ProductId, novoLivro2.ProductId };
            int formaPagamentoId = formaPagamento.PaymentTypeId;

            using (var auxContext = new LivrariaTDDContext())
            {
                var countAntes = auxContext.Orders.Count();
                Assert.AreEqual(0, countAntes);
            }

            using (var livrariaContext = new LivrariaTDDContext())
            {
                _repository = new PedidoRepository(livrariaContext);

                var result = _repository.SalvarPedido(pedido, userId, listaIdProducts, formaPagamentoId);

                Assert.True(result);
            }

            using (var auxContext = new LivrariaTDDContext())
            {
                var countDepois = auxContext.Orders.Count();
                Assert.AreEqual(1, countDepois);
            }
        }
        public ActionResult FinishOrder()
        {
            var paymentType = int.Parse(HttpContext.Request["paymentType"]);

            var accountService = DependencyResolver.Current.GetService<AccountBusiness>();

            var orderSerice = DependencyResolver.Current.GetService<OrderBusiness>();

            var user = accountService.GetByEmail(HttpContext.User.Identity.Name);

            var total = 0.0M;

            var shoppingCart = (Dictionary<Models.Product.Product, int>)Session["ShoppingCart"] ??
                               new Dictionary<Models.Product.Product, int>();

            var listaProdutos = new List<int>();

            foreach (var item in shoppingCart)
            {
                listaProdutos.Add(item.Key.ProductId);
                total += item.Value*item.Key.Price;
            }

            var order = new Order
                            {PaymentTypeId = paymentType, UserId = user.UserId, OrderValue = total, TotalValue = total};

            orderSerice.SalvarPedido(order, user.UserId, listaProdutos, paymentType);

            Session["ShoppingCart"] = new Dictionary<Models.Product.Product, int>();

            return View();
        }