Beispiel #1
0
        ///// <summary>
        ///// Modifies the amount of items in user's basket.
        ///// </summary>
        ///// <param name="BasketID">Specifies basket</param>
        ///// <param name="amount">Specifies amount</param>
        ///// <returns>True if could complete operation, false otherwise.</returns>
        //public async Task<bool> ModifyBasket(long BasketID, decimal amount)
        //{
        //    var basketEntity = await context.Baskets.FindAsync(BasketID);
        //    if (basketEntity == null)
        //        return false;
        //    basketEntity.Amount = amount;
        //    basketEntity.Date = DateTime.Now;
        //    context.Entry(basketEntity).Property(x => x.Amount).IsModified = true;
        //    await context.SaveChangesAsync();
        //    return true;
        //}

        /// <summary>
        /// Saves list of baskets, should be used to save users' buys in case
        /// hes logging out.
        /// </summary>
        /// <param name="basketsList">List of basket objects.</param>
        /// <returns>True if could save, false otherwise.</returns>
        public async Task <bool> SaveBaskets(List <Basket> basketsList)
        {
            foreach (Basket basket in basketsList)
            {
                var basketEntity = new Baskets();
                basketEntity.UserID    = basket.UserID;
                basketEntity.ProductID = basket.ProductID;
                basketEntity.Amount    = basket.Amount;
                basketEntity.Date      = basket.Date;

                context.Baskets.Add(basketEntity);
            }
            await context.SaveChangesAsync();

            return(true);
        }
Beispiel #2
0
        ///// <summary>
        ///// Saves a basket
        ///// </summary>
        ///// <param name="basket">Basket object.</param>
        ///// <returns>True if could save, false otherwise.</returns>
        //public async Task<Basket> AddBasket(Basket basket)
        //{
        //    var basketEntity = new Baskets();
        //    basketEntity.UserID = basket.UserID;
        //    basketEntity.ProductID = basket.ProductID;
        //    basketEntity.Amount = basket.Amount;
        //    basketEntity.Date = basket.Date;

        //    context.Baskets.Add(basketEntity);
        //    await context.SaveChangesAsync();
        //    basket.BasketID = basketEntity.BasketID;

        //    return basket;
        //}

        /// <summary>
        /// Moves the specified amount of specified product to the basket of specified user.
        /// </summary>
        /// <param name="UserID">Specifies user</param>
        /// <param name="ProductID">Specifies product</param>
        /// <param name="Amount">Specifies amount</param>
        /// <returns>True if could complete operation, false otherwise.</returns>
        public async Task <bool> MoveProductToBasket(long UserID, long ProductID, decimal Amount)
        {
            if (Amount <= 0)
            {
                return(false);
            }
            var product = await context.Products.FindAsync(ProductID);

            if (product == null)
            {
                return(false);
            }
            if (Amount > product.Amount)
            {
                return(false);
            }
            var basket = await context.Baskets.FirstOrDefaultAsync(x => x.UserID == UserID && x.ProductID == ProductID);

            if (basket == null)
            {
                basket           = new Baskets();
                basket.Amount    = Amount;
                basket.Date      = DateTime.Now;
                basket.ProductID = ProductID;
                basket.UserID    = UserID;
                context.Baskets.Add(basket);
            }
            else
            {
                basket.Amount += Amount;
                context.Entry(basket).Property(x => x.Amount).IsModified = true;
                basket.Date = DateTime.Now;
            }
            product.Amount -= Amount;
            context.Entry(product).Property(x => x.Amount).IsModified = true;
            // ok, problem jest taki, że zmieniamy basket i product, tylko, że zmieniamy lokalną zmienną chyba, a nie w samej kolekcji i to trzeba sfiksować
            await context.SaveChangesAsync();

            return(true);
        }