/// <summary>
        /// Allows you to buy a book if the user exists, is active and if the book is available in stock
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="bookID"></param>
        /// <returns>boolean true if successful, false if not</returns>
        public bool BuyBook(int userID, int bookID)
        {
            using (var db = new WebbshopContext())
            {
                try
                {
                    var user = db.Users.FirstOrDefault(u => u.ID == userID);
                    if (user == null)
                    {
                        Console.WriteLine("User does not exist");
                        return(false);
                    }

                    bool isActive = CheckSessionTimer(user);
                    if (!isActive)
                    {
                        Console.WriteLine("You have to login to proceed");
                        return(false);
                    }

                    var book = db.Books.Include(b => b.BookCategory).FirstOrDefault(b => b.ID == bookID);
                    if (book != null)
                    {
                        if (book.Amount == 0)
                        {
                            Console.WriteLine("Book not available in stock.");
                            return(false);
                        }
                        var soldBook = new SoldBooks
                        {
                            Title        = book.Title,
                            Author       = book.Author,
                            Price        = book.Price,
                            Amount       = book.Amount,
                            BookCategory = book.BookCategory,
                            UsersId      = userID,
                            PurchaseDate = DateTime.Now
                        };
                        db.SoldBooks.Add(soldBook);
                        book.Amount -= 1;
                        db.SaveChanges();
                        return(true);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Exception was thrown: {e.Message}");
                }
                return(false);
            }
        }
예제 #2
0
        /// <summary>
        /// Method to check if user is active and copies book-data to SoldBook.
        /// </summary>
        public bool BuyBook(int userID, int bookID)
        {
            using (var db = new WScontext())
            {
                var user = db.Users.
                           FirstOrDefault(
                    u => u.Id == userID &&
                    DateTime.Now <= u.SessionTimer.AddMinutes(15)
                    );

                var book = db.Books.Include(
                    b => b.Category).
                           FirstOrDefault(
                    b => b.Id == bookID
                    );

                var soldBook = db.SoldBooks.
                               FirstOrDefault(
                    s => s.Title == book.Title
                    );

                if (user != null && book != null && book.Amount > 0)
                {
                    soldBook = new SoldBooks {
                        Title         = book.Title,
                        Author        = book.Author,
                        CategoryId    = book.Category.Id,
                        Price         = book.Price,
                        PurchasedDate = DateTime.Now,
                        UserId        = userID
                    };

                    book.Amount--;

                    db.SoldBooks.Add(soldBook);
                    db.Users.Update(user);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }