예제 #1
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);
            }
        }
예제 #2
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);
            }
        }
예제 #3
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);
            }
        }
예제 #4
0
        /// <summary>
        /// Admin-Method, list of users where users name contains keyword.
        /// </summary>
        public void FindUser(int adminID, string keyword)
        {
            var hm = new HelperMethods();

            using (var db = new WScontext())
            {
                var users = db.Users.
                            Where(
                    u => u.Name.Contains(keyword)).ToArray(
                    );

                if (users != null && hm.AdminCheck(adminID))
                {
                    foreach (var user in users)
                    {
                        if (user.IsAdmin == false)
                        {
                            Console.WriteLine($"{user.Id}. {user.Name}");
                        }
                        else
                        {
                            Console.WriteLine($"{user.Id}. {user.Name} (Admin)");
                        }
                    }
                }
            }
        }
예제 #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
        /// <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);
            }
        }
예제 #7
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);
            }
        }
예제 #8
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);
            }
        }
예제 #9
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);
            }
        }
예제 #10
0
        /// <summary>
        /// Admin-Method, list of users.
        /// </summary>
        public void ListUsers(int adminID)
        {
            using (var db = new WScontext())
            {
                var hm = new HelperMethods();

                var users = db.Users.ToArray();

                if (users != null && hm.AdminCheck(adminID))
                {
                    foreach (var user in users)
                    {
                        if (user.IsAdmin == false)
                        {
                            Console.WriteLine($"{user.Id}. {user.Name}");
                        }
                        else
                        {
                            Console.WriteLine($"{user.Id}. {user.Name} (Admin)");
                        }
                    }
                }
            }
        }