/// <summary>
        /// Adds new product to database created from the Product data object
        /// </summary>
        /// <param name="newProduct">Product data object for creating new product.</param>
        /// <returns>True if it could add, false otherwise.</returns>
        public async Task <Product> AddProduct(Product newProduct)
        {
            string name     = newProduct.ProductName;
            var    products = await context.Products.Where(
                x => x.Name == name &&
                (x.State == 0 || x.State == 1)
                ).ToListAsync();

            if (products.Count != 0)
            {
                return(null);
            }
            Products dbProduct = new Products();

            dbProduct.Amount      = newProduct.Amount;
            dbProduct.CategoryID  = newProduct.CategoryID;
            dbProduct.Description = newProduct.Description;
            dbProduct.Discount    = newProduct.Discount;
            dbProduct.Name        = newProduct.ProductName;
            dbProduct.Picture     = (byte[])new ImageConverter().ConvertTo(newProduct.Picture, typeof(byte[]));
            dbProduct.Price       = newProduct.Price;
            dbProduct.State       = newProduct.State;
            dbProduct.Unit_type   = newProduct.UnitType;

            context.Products.Add(dbProduct);
            await context.SaveChangesAsync();

            newProduct.ProductID = dbProduct.ProductID;
            return(newProduct);
        }
        /// <summary>
        /// Attempts to log in using specified user name and password.
        /// Checks Active and Created users.
        /// If the state is Created changes state to Active.
        /// </summary>
        /// <param name="userName">Username to be checked.</param>
        /// <param name="password">Password to be checked.</param>
        /// <returns>If operation succeded returns new User object.
        /// Otherwise returns null</returns>
        public async Task <User> LogIn(string userName, string password)
        {
            var user = await context.Users.FirstOrDefaultAsync(
                x => x.Name == userName && // search by userName
                (x.State == 0 || x.State == 1));    //user 'created' or 'active'

            if (user == null)
            {
                return(null);
            }


            if (user.Password != Hash(password))
            {
                return(null);
            }
            if (user.State == 0)
            {
                user.State = 1;
                context.Users.Attach(user);
                var entry = context.Entry(user);
                entry.Property(e => e.State).IsModified = true;

                await context.SaveChangesAsync();
            }
            User loggedUserData = new User(
                user.Name,
                user.E_mail,
                user.Type,
                user.State);

            loggedUserData.UserID = user.UserID;
            return(loggedUserData);
        }
        /// <summary>
        /// Crates a brand new, ready for action Category.
        /// </summary>
        /// <param name="Name">Name of Category, like 'Vegetables' or 'Elder scrolls'</param>
        /// <param name="Description">Description of category.</param>
        /// <returns>Category object.</returns>
        public async Task <Category> AddCategory(Category newCategory)
        {
            string name = newCategory.Name;

            if ((await context.Categories.FirstOrDefaultAsync(x => x.Name == name)) != null)
            {
                return(null);
            }
            Categories dbCategory = new Categories();

            dbCategory.Name        = newCategory.Name;
            dbCategory.Description = newCategory.Description;

            context.Categories.Add(dbCategory);
            await context.SaveChangesAsync();

            newCategory.CategoryID = dbCategory.CategoryID;
            return(newCategory);
        }
        /// <summary>
        /// Sets Welcome Message in database.
        /// </summary>
        /// <param name="newMessage">A new message.</param>
        /// <returns>True if succeded, false otherwise.</returns>
        public async Task <bool> SetWelcomeMessage(string newMessage)
        {
            var message = await context.Other.FirstOrDefaultAsync();

            if (message == null)
            {
                Other dbOther = new Other();
                dbOther.Welcome_message = newMessage;
                dbOther.Contact_info    = "";
                context.Other.Add(dbOther);
                await context.SaveChangesAsync();
            }
            else
            {
                message.Welcome_message = newMessage;
                context.Entry(message).Property(e => e.Welcome_message).IsModified = true;
                await context.SaveChangesAsync();
            }
            return(true);
        }
        /// <summary>
        /// Finilaize users' transaction, saves new Sale_history object in
        /// database and Orders objects accordingly to baskets from argument.
        /// First it should save new Sale in database, then orders
        /// with proper foreign keys.
        /// Needed user info is in basketsList and should be consistent
        /// through the list. This method should also delete any user baskets
        /// in database.
        /// </summary>
        /// <param name="baskets">List of basket object</param>
        /// <returns>True if purchase succeded, false otherwise.</returns>
        public async Task <bool> CreateSale(long UserID)
        {
            var basketsList = await context.Baskets.Where(x => x.UserID == UserID).ToListAsync();

            if (basketsList.Count == 0)
            {
                return(false);
            }
            long userID = basketsList[0].UserID;

            Sale_history transaction = new Sale_history();

            transaction.UserID = userID;
            transaction.Date   = System.DateTime.Now;
            context.Sale_history.Add(transaction);
            await context.SaveChangesAsync();

            long saleID = transaction.SaleID;

            foreach (Baskets basket in basketsList)
            {
                Orders dbOrder = new Orders();
                dbOrder.SaleID    = saleID;
                dbOrder.ProductID = basket.ProductID;
                dbOrder.Price     = (await context.Products.FindAsync(basket.ProductID)).Price;
                dbOrder.Amount    = basket.Amount;

                context.Orders.Add(dbOrder);
            }

            var baskets = await context.Baskets.Where(x => x.UserID == UserID).ToListAsync();

            foreach (var basket in baskets)
            {
                context.Baskets.Remove(basket);
            }
            await context.SaveChangesAsync();

            return(true);
        }
示例#6
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);
        }