/// <summary>
        /// Sets the amount of books specified by the user.
        /// </summary>
        /// <param name="book">Takes a book to change amount on</param>
        /// <param name="admin">Takes a user with admin priviliges</param>
        private static void SetAmount(Book book, User admin)
        {
            AdminView.SetAmount();
            var input = SharedController.GetAndValidateInput();

            if (input.validatedInput != 0)
            {
                WebShopApi api = new WebShopApi();
                if (api.SetAmount(admin.Id, book.Id, input.validatedInput))
                {
                    if (book.Amount <= 0)
                    {
                        book.Amount = 0;
                    }
                    SharedError.Success();
                    BookView.ChangedNumberOfBooks(book);
                }
                else
                {
                    SharedError.Failed();
                    BookView.ChangedNumberOfBooks(book);
                }
            }
            else
            {
                SharedError.PrintWrongMenuInput();
            }
        }
        /// <summary>
        /// Deletes the number of books typed in when asked.
        /// </summary>
        /// <param name="book">Takes a book to delete from</param>
        /// <param name="admin">Takes a user with admin priviliges</param>
        private static void DeleteBook(Book book, User admin)
        {
            bool continueLoop = true;

            do
            {
                Console.Clear();
                AdminView.DeleteBook(book);
                var input = SharedController.GetAndValidateInput();
                if (input.validatedInput > 0)
                {
                    var originalBookAmount = book.Amount;
                    if (api.DeleteBook(admin.Id, book.Id, input.validatedInput))
                    {
                        SharedError.Success();
                        if (book.Amount == originalBookAmount &&
                            book.Amount - input.validatedInput <= 0)
                        {
                            book.Amount = 0;
                        }
                    }
                    else
                    {
                        SharedError.Failed();
                    }
                    continueLoop = false;
                }
                else
                {
                    SharedError.PrintWrongInput();
                }
            } while (continueLoop);
        }
        /// <summary>
        /// Method for adding a category to database
        /// </summary>
        /// <param name="admin">Takes a user with admin priviliges</param>
        private static void AddCategory(User admin)
        {
            var continueLoop = true;

            do
            {
                Console.Clear();
                AdminView.AddCategory();
                var input = SharedController.GetSearchInput();
                if (SharedController.CheckIfNullOrEmptyOrWhiteSpace(input))
                {
                    SharedError.PrintWrongInput();
                    continueLoop = true;
                }
                else
                {
                    if (api.AddCategory(admin.Id, input))
                    {
                        SharedError.Success();
                    }
                    else
                    {
                        SharedError.Failed();
                    }
                    continueLoop = false;
                }
            } while (continueLoop);
        }
        /// <summary>
        /// Method for editing the user. Promote, Demote, activate or deactivate
        /// </summary>
        /// <param name="admin">Takes a user with admin priviliges</param>
        /// <param name="editUser">Takes a user to edit</param>
        private static void EditUser(User admin, User editUser)
        {
            var continueLoop = true;

            do
            {
                AdminView.EditUser(editUser);
                var input = SharedController.GetAndValidateInput();
                switch (input.validatedInput)
                {
                case 1:
                    if (api.Promote(admin.Id, editUser.Id))
                    {
                        SharedError.Success();
                        break;
                    }
                    SharedError.Failed();
                    break;

                case 2:
                    if (api.Demote(admin.Id, editUser.Id))
                    {
                        SharedError.Success();
                        break;
                    }
                    SharedError.Failed();
                    break;

                case 3:
                    if (api.ActivateUser(admin.Id, editUser.Id))
                    {
                        SharedError.Success();
                        break;
                    }
                    SharedError.Failed();
                    break;

                case 4:
                    if (api.InactivateUser(admin.Id, editUser.Id))
                    {
                        SharedError.Success();
                        break;
                    }
                    SharedError.Failed();
                    break;

                case 0:
                    continueLoop = SharedController.GoBackIf_X_IsPressedOrPrintErrorMsg(input.menuInput);
                    break;

                default:
                    SharedError.PrintWrongMenuInput();
                    break;
                }
            } while (continueLoop);
        }
 /// <summary>
 /// Method for buying a book
 /// </summary>
 /// <param name="user">Takes a user to be connected with the purchase</param>
 /// <param name="book">Takes a book to be bought</param>
 internal static void BuyBook(User user, Book book)
 {
     if (api.BuyBook(user.Id, book.Id))
     {
         SharedError.Success();
     }
     else
     {
         SharedError.Failed();
     }
 }
        /// <summary>
        /// Method for adding a user to database
        /// </summary>
        /// <param name="admin">Takes a user with admin priviliges</param>
        private static void AddUser(User admin)
        {
            var userCredentials = AdminView.AddUser();

            if (api.AddUser(admin.Id, userCredentials.userName, userCredentials.password))
            {
                SharedError.Success();
            }
            else
            {
                SharedError.Failed();
            }
        }
        /// <summary>
        /// Method for updating the category name of specified category
        /// </summary>
        /// <param name="admin">Takes a user with admin priviliges</param>
        /// <param name="bookCategory">Takes a book category to be updated</param>
        private static void UpdateCategory(User admin, BookCategory bookCategory)
        {
            AdminView.UpdateCategory(bookCategory.Name);
            var input = SharedController.GetSearchInput();

            if (SharedController.CheckIfNullOrEmptyOrWhiteSpace(input))
            {
                SharedError.UnChanged();
            }
            else
            {
                if (api.UpdateCategory(admin.Id, bookCategory.Id, input))
                {
                    SharedError.Success();
                }
                else
                {
                    SharedError.Failed();
                }
            }
        }
        /// <summary>
        /// Delete category (if empty)
        /// </summary>
        /// <param name="admin">Takes a user with admin priviliges</param>
        private static void DeleteCategory(User admin)
        {
            var continueLoop = true;

            do
            {
                Console.Clear();
                AdminView.DeleteCategory();
                var categories = api.GetCategories();
                SharedView.ListCategories(categories);
                var input = SharedController.GetAndValidateInput();
                if (input.validatedInput > 0 &&
                    input.validatedInput <= categories.Count)
                {
                    if (api.DeleteCategory(
                            admin.Id,
                            categories[input.validatedInput - 1].Id))
                    {
                        SharedError.Success();
                    }
                    else
                    {
                        var books = api.GetBooksInCategory(categories[input.validatedInput - 1].Id);
                        SharedError.BooksStillInCategory(books.Count);
                    }
                }
                else
                {
                    if (input.validatedInput == 0 &&
                        input.menuInput.ToLower() == "x")
                    {
                        continueLoop = false;
                    }
                    else
                    {
                        SharedError.PrintWrongInput();
                    }
                }
            } while (continueLoop);
        }
        /// <summary>
        /// Method for changing the category that a book is connected to.
        /// </summary>
        /// <param name="book">Takes abook to change</param>
        /// <param name="admin">Takes a user with admin priviliges</param>
        private static void ChangeCategory(Book book, User admin)
        {
            var categories   = api.GetCategories();
            var continueLoop = true;

            do
            {
                Console.Clear();
                AdminView.ChangeCategory(book, categories);
                if (categories.Count() > 0)
                {
                    var input = SharedController.GetAndValidateInput();
                    if (input.validatedInput > 0 &&
                        input.validatedInput <= categories.Count())
                    {
                        var success = api.AddBookToCategory(
                            admin.Id,
                            book.Id,
                            categories[input.validatedInput - 1].Id);
                        continueLoop = false;
                        if (success)
                        {
                            SharedError.Success();
                        }
                        else
                        {
                            SharedError.Failed();
                        }
                    }
                    else
                    {
                        SharedError.PrintWrongInput();
                    }
                }
            } while (continueLoop);
        }
Beispiel #10
0
        /// <summary>
        /// Method for printing the main menu
        /// </summary>
        public static void PrintMainMenu()
        {
            int    userMenuChoice;
            string menuInput = default;

            do
            {
                Console.Clear();
                StartView.Print();
                menuInput      = InputHelper.AskForMenuInput();
                userMenuChoice = InputHelper.ValidateMenuInput(menuInput);

                if (userMenuChoice == 0 && menuInput.ToLower() != "q")
                {
                    SharedError.PrintWrongMenuInput();
                }
                else
                {
                    var api = new WebShopApi();
                    switch (userMenuChoice)
                    {
                    case 1:
                        var userCredentials = LoginView.PrintLoginPage();
                        var user            = api.Login(userCredentials.userName, userCredentials.password);

                        if (UserController.UserIsNull(user) &&
                            !userCredentials.userName.Contains(abortChar))
                        {
                            SharedError.PrintWrongCredentials(user);

                            continue;
                        }
                        else if (userCredentials.userName.Contains(abortChar))
                        {
                            break;
                        }
                        else if (user.IsAdmin)
                        {
                            AdminController.PrintAdminSelectionMenu(user);
                        }
                        else
                        {
                            UserController.UserSelectionMenu(user);
                        }
                        break;

                    case 2:
                        var registererUserCredentials = UserView.Register();
                        if (!api.Register(registererUserCredentials.username,
                                          registererUserCredentials.password,
                                          registererUserCredentials.verifyPassword))
                        {
                            SharedError.Failed();
                            break;
                        }
                        SharedError.Success();
                        break;

                    case 0:
                        Environment.Exit(1);
                        break;

                    default:
                        SharedError.PrintWrongMenuInput();
                        break;
                    }
                }
            } while (menuInput.ToLower() != "q");
        }