コード例 #1
0
        /// <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();
            }
        }
コード例 #2
0
        /// <summary>
        /// Logs out the user if X was pressed.
        /// </summary>
        /// <param name="user">Takes the user to log out</param>
        /// <param name="menuInput">takes the menu choice of the user</param>
        /// <param name="validatedInput">takes the validated input from the user</param>
        /// <returns>Returns true if user was logged out. false otherwise</returns>
        public static bool LogoutIf_X_IsPressed(User user, string menuInput, int validatedInput)
        {
            var logoutUser = false;

            if (validatedInput == 0 && menuInput.ToLower() == "x")
            {
                WebShopApi api = new WebShopApi();
                api.Logout(user.Id);
                logoutUser = true;
                Console.Clear();
            }

            return(logoutUser);
        }
コード例 #3
0
        /// <summary>
        /// Search book from specific author
        /// </summary>
        /// <returns>Returns a abort message if aborted during search.
        /// Always returns a list with books, empty or not.</returns>
        internal static (string message, List <Book> listWithBooks) SearchBooksFromAuthor()
        {
            List <Book> listOfBooksFromAuthor = new List <Book>();
            WebShopApi  api = new WebShopApi();

            Console.Clear();
            BookView.SearchBooksFromAuthor();
            var searchKeyword = SharedController.GetSearchInput();

            if (searchKeyword.ToLower() == "x")
            {
                return("Avbrutet", listOfBooksFromAuthor);
            }
            listOfBooksFromAuthor = api.GetBooksByAuthor(searchKeyword);
            return("Sökresultat", listOfBooksFromAuthor);
        }
コード例 #4
0
        /// <summary>
        /// Method for getting input and searching for a book
        /// </summary>
        /// <returns>A specific book</returns>
        public static Book SearchForBook()
        {
            WebShopApi api = new WebShopApi();

            Console.Clear();
            BookView.SearchForBook();
            var searchKeyword = SharedController.GetSearchInput();

            if (searchKeyword.ToLower() == "x")
            {
                return(null);
            }
            var listWithMatchingBooks = api.GetBooks(searchKeyword);

            if (listWithMatchingBooks.Count > 0)
            {
                Console.Clear();
                BookView.ListAllBooks(listWithMatchingBooks);
                var input = SharedController.GetAndValidateInput();
                if (input.validatedInput != 0 &&
                    input.validatedInput <= listWithMatchingBooks.Count)
                {
                    return(api.GetBook(listWithMatchingBooks[input.validatedInput - 1].Id));
                }
                else
                {
                    SharedError.PrintWrongInput();
                    return(null);
                }
            }
            else
            {
                SharedError.NothingFound();
                return(null);
            }
        }
コード例 #5
0
ファイル: Menu.cs プロジェクト: Potatismoose/Webbshop
        /// <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");
        }