示例#1
0
        public RentViewModel NewRent()
        {
            List <ShoppingCartItem> items = shoppingCartService.GetShoppingCartItems();
            int totalPrice = shoppingCartService.GetShoppingCartTotal();

            RentViewModel rent = new RentViewModel {
                ShoppingCartItems = items, TotalPrice = totalPrice
            };

            return(rent);
        }
示例#2
0
        public void SaveRentForProducts(List <Product> products, RentViewModel rent)
        {
            Rent lastRent = context.Rents.LastOrDefault();

            for (int i = 0; i < products.Count; i++)
            {
                context.RentProductConnections.Add(new RentProductConnection
                {
                    RentId = lastRent.Id,
                    ProductModellNumber = products[i].ModellNumber,
                    CountProduct        = rent.ShoppingCartItems[i].Quantity
                });
            }
            context.SaveChanges();
        }
示例#3
0
        public Boolean SaveRentAsync(String userName, RentViewModel rent)
        {
            // ellenőrizzük az annotációkat
            if (!Validator.TryValidateObject(rent, new ValidationContext(rent, null, null), null))
            {
                return(false);
            }

            // a felhasználót a név alapján betöltjük
            Customer customer = context.Users.FirstOrDefault(c => c.UserName == userName);


            if (customer == null)
            {
                return(false);
            }

            List <Product> products = new List <Product>();

            for (int i = 0; i < rent.ShoppingCartItems.Count; i++)
            {
                Product currentProduct = context.Products.FirstOrDefault(p => p.ModellNumber == rent.ShoppingCartItems[i].ProductModellNumber);
                products.Add(currentProduct);
            }
            int totalPrice = shoppingCartService.GetShoppingCartTotal();

            context.Rents.Add(new Rent
            {
                CustomerId = customer.Id,
                TotalPrice = totalPrice,
                Performed  = false
            });

            try
            {
                context.SaveChanges();
                SaveRentForProducts(products, rent);
            }
            catch (Exception)
            {
                // mentéskor lehet hiba
                return(false);
            }

            // ha idáig eljuttottunk, minden sikeres volt
            return(true);
        }