예제 #1
0
        public static Books CreateBook(string title, string author, int price, int amount, string category)
        {
            using (var db = new WScontext())
            {
                var book = db.Books.
                           FirstOrDefault(
                    b => b.Title == title
                    );

                if (book == null)
                {
                    book = new Books
                    {
                        Title    = title,
                        Author   = author,
                        Price    = price,
                        Amount   = amount,
                        Category = db.BookCategories.FirstOrDefault(c => c.Name == category)
                    };
                    db.Books.Add(book);
                    db.SaveChanges();
                }
                return(book);
            }
        }
예제 #2
0
        /// <summary>
        /// Admin-Method, update book information.
        /// </summary>
        /// <returns>
        /// True if book is updated, else false.
        /// </returns>
        public bool UpdateBook(int adminID, int bookID, string title, string author, int price)
        {
            using (var db = new WScontext())
            {
                var hm = new HelperMethods();

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

                if (book != null && hm.AdminCheck(adminID))
                {
                    book.Title  = title;
                    book.Author = author;
                    book.Price  = price;

                    db.Books.Update(book);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
예제 #3
0
        /// <summary>
        /// Admin-Method, adds user.
        /// </summary>
        /// <returns>
        /// True if user is added, else false.
        /// </returns>
        public bool AddUser(int adminID, string name, string password)
        {
            var hm = new HelperMethods();

            using (var db = new WScontext())
            {
                var user = db.Users.
                           FirstOrDefault(
                    u => u.Name == name
                    );

                if (user == null && hm.AdminCheck(adminID))
                {
                    user = new Users
                    {
                        Name     = name,
                        Password = password,
                    };
                    db.Users.Add(user);
                    db.SaveChanges();
                    return(true);
                }
                return(false);
            }
        }
예제 #4
0
        /// <summary>
        /// Admin-Method, adds book to category.
        /// </summary>
        /// <returns>
        /// True if book is added, else false.
        /// </returns>
        public bool AddBookToCategory(int adminID, int bookID, int categoryID)
        {
            using (var db = new WScontext())
            {
                var hm = new HelperMethods();

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

                var category = db.BookCategories.
                               FirstOrDefault(
                    c => c.Id == categoryID
                    );

                if (category != null && book != null && hm.AdminCheck(adminID))
                {
                    book.Category = category;

                    db.Books.Update(book);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
예제 #5
0
        /// <summary>
        /// Admin-Method, adds book.
        /// </summary>
        /// <returns>
        /// True if book is added, else false.
        /// </returns>
        public bool AddBook(int adminID, string title, string author, int price, int amount)
        {
            using (var db = new WScontext())
            {
                var hm = new HelperMethods();

                var book = db.Books.
                           FirstOrDefault(
                    b => b.Title == title
                    );

                if (book == null && hm.AdminCheck(adminID) == true)
                {
                    book = new Books
                    {
                        Title  = title,
                        Author = author,
                        Amount = amount,
                        Price  = price
                    };
                    db.Books.Add(book);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
예제 #6
0
        public void SessionCheck(int userId)
        {
            using (var db = new WScontext())
            {
                var api = new WebbshopAPI();

                var user = db.Users.
                           FirstOrDefault(
                    u => u.Id == userId
                    );

                if (user != null)
                {
                    if (DateTime.Now > user.SessionTimer.AddMinutes(15))
                    {
                        api.Logout(userId);
                    }
                    else
                    {
                        user.SessionTimer = DateTime.Now;
                        db.Users.Update(user);
                        db.SaveChanges();
                    }
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Method to logout user.
        /// </summary>
        public void Logout(int userID)
        {
            using (var db = new WScontext())
            {
                var user = db.Users.
                           FirstOrDefault(
                    c => c.Id == userID
                    );

                if (user != null)
                {
                    user.SessionTimer = DateTime.MinValue;
                    user.LastLogin    = DateTime.Now;
                    user.IsActive     = false;
                    db.Users.Update(user);
                    db.SaveChanges();
                }
            }
        }
예제 #8
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);
            }
        }
예제 #9
0
        public static BookCategory CreateCategory(string name)
        {
            using (var db = new WScontext())
            {
                var category = db.BookCategories.
                               FirstOrDefault(
                    c => c.Name == name
                    );

                if (category == null)
                {
                    category = new BookCategory {
                        Name = name
                    };
                    db.BookCategories.Add(category);
                    db.SaveChanges();
                }
                return(category);
            }
        }
예제 #10
0
        /// <summary>
        /// Admin-Method, removes category from database.
        /// </summary>
        /// <returns>
        /// True if category is deleted, else false.
        /// </returns>
        public bool DeleteCategory(int adminID, int categoryID)
        {
            using (var db = new WScontext())
            {
                var hm = new HelperMethods();

                var category = db.BookCategories.
                               FirstOrDefault(
                    c => c.Id == categoryID
                    );

                if (category != null && hm.AdminCheck(adminID))
                {
                    db.BookCategories.Remove(category);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
예제 #11
0
        /// <summary>
        /// Admin-Method, remove book from database.
        /// </summary>
        /// <returns>
        /// True if book is deleted, else false.
        /// </returns>
        public bool DeleteBook(int adminID, int bookID)
        {
            using (var db = new WScontext())
            {
                var hm = new HelperMethods();

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

                if (book != null && hm.AdminCheck(adminID))
                {
                    db.Books.Remove(book);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
예제 #12
0
        /// <summary>
        /// Method to login user.
        /// </summary>
        /// <returns>
        /// User int.
        /// </returns>
        public int Login(string userName, string password)
        {
            using (var db = new WScontext())
            {
                var user = db.Users.
                           FirstOrDefault(
                    c => c.Name == userName
                    );

                if (user != null && user.Password == password)
                {
                    user.SessionTimer = DateTime.Now;
                    user.IsActive     = true;
                    db.Users.Update(user);
                    db.SaveChanges();
                    return(user.Id);
                }

                return(0);
            }
        }
예제 #13
0
        public static Users CreateUser(string name, string password, bool adminCheck)
        {
            using (var db = new WScontext())
            {
                var user = db.Users.
                           FirstOrDefault(
                    u => u.Name == name
                    );

                if (user == null)
                {
                    user = new Users {
                        Name     = name,
                        Password = password,
                        IsAdmin  = adminCheck
                    };
                    db.Users.Add(user);
                    db.SaveChanges();
                }
                return(user);
            }
        }
예제 #14
0
        /// <summary>
        /// Admin-Method, sets bookamount.
        /// </summary>
        public bool SetAmount(int adminID, int bookID, int amount)
        {
            using (var db = new WScontext())
            {
                var hm = new HelperMethods();

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

                if (book != null && hm.AdminCheck(adminID) == true)
                {
                    book.Amount = amount;
                    db.Books.Update(book);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
예제 #15
0
        /// <summary>
        /// Admin-Method, adds category.
        /// </summary>
        /// <returns>
        /// True if category is added, else false.
        /// </returns>
        public bool AddCategory(int adminID, string name)
        {
            using (var db = new WScontext())
            {
                var hm = new HelperMethods();

                var category = db.BookCategories.
                               FirstOrDefault(
                    c => c.Name == name
                    );

                if (category == null && hm.AdminCheck(adminID))
                {
                    category = new BookCategory {
                        Name = name
                    };
                    db.BookCategories.Add(category);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
예제 #16
0
        /// <summary>
        /// Method for user to register.
        /// </summary>
        /// <returns>
        /// True if user is added, else false.
        /// </returns>
        public bool Register(string name, string password, string passwordVerify)
        {
            using (var db = new WScontext())
            {
                var user = db.Users.
                           FirstOrDefault(
                    u => u.Name == name
                    );

                if (user == null && passwordVerify == password)
                {
                    user = new Users
                    {
                        Name     = name,
                        Password = password
                    };
                    db.Users.Add(user);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }