/// <summary> /// Checks if login succeeded /// </summary> /// <param name="userName"></param> /// <param name="userPassword"></param> /// <returns>integer, user id if successful, 0 if not</returns> public int CheckLoginStatus(string userName, string userPassword) { if (userName != "" && userPassword != "") { WebbShopAPI api = new WebbShopAPI(); var user = api.Login(userName, userPassword); if (user != 0) { AccountViews.LoginSuccess(); return(user); } } AccountViews.LoginFailed(); return(0); }
/// <summary> /// Logs the user into database. by Logindetails provided in the parameters of the method /// </summary> /// <param name="details"></param> /// <param name="userId"></param> /// <param name="errorMsg"></param> /// <returns>True if successful, else false. Also returns userId and string error message</returns> internal bool LoginUser(LoginDetails details, out int userId, out string errorMsg) { errorMsg = ""; //Convert from int? to int. userId = ConvertToInteger(webAPI.Login(details.UserName, details.Password)); if (userId != 0) { SetTimer(userId); return(true); } else { errorMsg = "Failed to login."; return(false); } }
static void Main(string[] args) { do { Login.ShowFirstMenu(); userId = WebbShopAPI.Login(Controllers.LoginController.username, Controllers.LoginController.password); if (userId > 0) { if (WebbShopAPI.user.IsAdmin) { Console.Clear(); Console.WriteLine("Du är inloggad som Administrator"); do { View.AdminMenu.ShowAdminMenu(); choise = Console.ReadLine(); Controllers.AdminControllers.AdminMenuController.AdminMenu(); } while (choise != "q"); } else { Console.WriteLine("Du är inloggad som user"); do { View.UserMenu.ShowUserMenu(); choise = Console.ReadLine(); Controllers.UserControllers.UserMenuController.ShowUserMenu(); } while (choise != "q"); } if (choise == "q") { choise = "n"; } } else { Console.WriteLine("Wrong username or password"); Console.Write("Try again (y/n): "); choise = Console.ReadLine(); } } while (choise != "n"); }
/// <summary> /// Runs the scenario /// </summary> public static void Run() { var api = new WebbShopAPI(); Console.Write("Logged in as: "); int userId = api.Login("Administrator", "CodicRulez"); Console.WriteLine(userId); //------------------------------------------------------------------------- Console.WriteLine("Adding new book... "); bool success = api.AddBook(userId, "Sagan om ringen", "J.R.R Tolkien", 300, 5); if (success) { Console.WriteLine("Added book: Sagan om ringen by J.R.R Tolkien"); } //------------------------------------------------------------------------- Console.WriteLine("Setting amount... "); success = api.SetAmount(userId, 2, 4); if (success) { Console.WriteLine("Successfully added new amount."); } //------------------------------------------------------------------------- Console.WriteLine("Searching for all users..."); var list = api.ListUsers(userId); foreach (var user in list) { Console.WriteLine(user.Name); } //------------------------------------------------------------------------- Console.WriteLine("Searching for all users matching keyword \"te\"... "); list = api.FindUser(userId, "te"); foreach (var user in list) { Console.WriteLine(user.Name); } //------------------------------------------------------------------------- var book = Helper.GetBookObject("Sagan om ringen"); if (book != null) { Console.WriteLine($"Updating {book.Title} with new price 350..."); success = api.UpdateBook(userId, book.ID, book.Title, book.Author, 350); if (success) { Console.WriteLine("Sucessfully updated book"); } } //------------------------------------------------------------------------- Console.WriteLine("Adding new category... "); success = api.AddCategory(userId, "Fantasy"); if (success) { Console.WriteLine("Added category: Fantasy"); } Console.WriteLine("Adding new category... "); success = api.AddCategory(userId, "Action"); if (success) { Console.WriteLine("Added category: Action"); } //------------------------------------------------------------------------- Console.WriteLine("Adding book to category... "); var bookId = Helper.GetBookID("Sagan om ringen"); var categoryId = Helper.GetCategoryId("Fantasy"); success = api.AddBookToCategory(userId, bookId, categoryId); if (success) { Console.WriteLine("Successfully added book Sagan om ringen to category Fantasy"); } //------------------------------------------------------------------------- Console.WriteLine("Updating category... "); categoryId = Helper.GetCategoryId("Action"); success = api.UpdateCategory(userId, categoryId, "ActionPower"); if (success) { Console.WriteLine("Successfully updated category genere Action to ActionPower."); } //------------------------------------------------------------------------- Console.WriteLine("Adding new user... "); success = api.AddUser(userId, "Legolas", "L3mb4sBr34d"); if (success) { Console.WriteLine("Successfully added user: Legolas"); } //------------------------------------------------------------------------- Console.WriteLine("Promoting \"Legolas\"... "); var customerId = Helper.GetUserID("Legolas"); success = api.Promote(userId, customerId); if (success) { Console.WriteLine("Sucessfully promoted Legolas to Admin"); } api.Logout(userId); //------------------------------------------------------------------------- Console.Write("Logged in as: "); userId = api.Login("Legolas", "L3mb4sBr34d"); Console.WriteLine(userId); Console.WriteLine("Purchasing \"Sagan om ringen\"... "); bookId = Helper.GetBookID("Sagan om ringen"); success = api.BuyBook(userId, bookId); if (success) { Console.WriteLine("Purchase successful."); } Console.WriteLine("Purchasing \"Sagan om ringen\"... "); success = api.BuyBook(userId, bookId); if (success) { Console.WriteLine("Purchase successful."); } Console.WriteLine("Purchasing \"Sagan om ringen\"... "); success = api.BuyBook(userId, bookId); if (success) { Console.WriteLine("Purchase successful."); } api.Logout(userId); //------------------------------------------------------------------------- Console.Write("Logged in as: "); userId = api.Login("Administrator", "CodicRulez"); Console.WriteLine(userId); Console.WriteLine("Searching for all sold items..."); var soldBooks = api.SoldItems(userId); if (soldBooks.Count > 0) { foreach (var item in soldBooks) { Console.WriteLine($"{item.Title} - {item.Author}"); } } Console.WriteLine("Searching for total price of all sold items... "); int moneyEarned = api.MoneyEarned(userId); if (moneyEarned > 0) { Console.WriteLine($"Money earned: {moneyEarned}"); } Console.WriteLine("Searching \"Best customer\"... "); int bestCustomer = api.BestCustomer(userId); if (bestCustomer > 0) { Console.WriteLine($"Best customer's ID: {bestCustomer}"); } Console.WriteLine("Deleting book \"I Robot\"... "); bookId = Helper.GetBookID("I Robot"); success = api.DeleteBook(userId, bookId); if (success) { Console.WriteLine("Successfully deleted book"); } Console.WriteLine("Deleting category \"Romance\"... "); categoryId = Helper.GetCategoryId("Romance"); success = api.DeleteCategory(userId, categoryId); if (success) { Console.WriteLine("Successfully deleted category"); } Console.WriteLine("Deleting category \"Fantasy\"... "); categoryId = Helper.GetCategoryId("Fantasy"); success = api.DeleteCategory(userId, categoryId); if (success) { Console.WriteLine("Successfully deleted category"); } api.Logout(userId); }
/// <summary> /// Runs a test-scenario /// </summary> public static void Run() { var api = new WebbShopAPI(); Console.Write("Logged in as: "); int userId = api.Login("TestCustomer", "Codic2021"); Console.WriteLine(userId); //--------------------------------------------------- Console.WriteLine("Searching all categories: "); var listOfCategories = api.GetCategories(); if (listOfCategories.Count > 0) { foreach (var category in listOfCategories) { Console.WriteLine(category.Genere); } } //--------------------------------------------------- Console.WriteLine("Searching all categories with \"or\"-keyword: "); listOfCategories = api.GetCategories("or"); if (listOfCategories.Count > 0) { foreach (var category in listOfCategories) { Console.WriteLine(category.Genere); } } //--------------------------------------------------- Console.WriteLine("Searching for all books with category \"Horror\": "); var listOfBooks = api.GetCategories(2); if (listOfBooks.Count > 0) { foreach (var book in listOfBooks) { Console.WriteLine(book.Title); } } var respons = api.Ping(userId); if (respons.Length > 0) { Console.WriteLine(respons); } //--------------------------------------------------- Console.WriteLine("Searching for all available books with category \"Horror\": "); var listOfAvailableBooks = api.GetAvailableBooks(2); foreach (var book in listOfAvailableBooks) { Console.WriteLine($"{book.Title}, Amount: {book.Amount}"); } //--------------------------------------------------- Console.WriteLine("Information around all books with genere \"Horror\""); var description = api.GetBook(4); Console.WriteLine(description); //--------------------------------------------------- Console.WriteLine("Searching for books matching search word \"shi\""); listOfBooks = api.GetBooks("shi"); foreach (var book in listOfBooks) { Console.WriteLine(book.Title); } respons = api.Ping(userId); if (respons.Length > 0) { Console.WriteLine(respons); } //--------------------------------------------------- Console.WriteLine("Searching for books matching Author \"Stephen King\""); listOfBooks = api.GetAuthor("Stephen King"); foreach (var book in listOfBooks) { Console.WriteLine(book.Title); } //--------------------------------------------------- Console.WriteLine("Selected book to purchase: \"Doctor Sleep\""); var succeed = api.BuyBook(userId, 2); if (succeed) { Console.WriteLine("Purchase made"); } //--------------------------------------------------- api.Logout(userId); }