/// <summary> /// Updates the database that a user has bought a book by parameter input /// Also checks if the user's session is active. Then also returns out bool if user is inactve, and string error message /// </summary> /// <param name="bookId"></param> /// <param name="userId"></param> /// <param name="userIsInactive"></param> /// <param name="errorMsgOut"></param> /// <returns>True if successfull</returns> internal bool UserBuysBook(int bookId, int userId, out bool userIsInactive, out string errorMsgOut) { userIsInactive = false; errorMsgOut = ""; if (!IsSessionActive(userId, out string errorMsgIn)) { userIsInactive = true; errorMsgOut = errorMsgIn; return(false); } else { //Returns False if amount of book is 0. A public int GetAmount(int bookId){} of WebbShopAPI should be implemented. if (webAPI.BuyBook(userId, bookId)) { return(true); } else { errorMsgOut = "user did not buy book"; Debug.WriteLine("BuyBook from webbshopapi returned false"); return(false); } } }
/// <summary> /// Allows you to purchase chosen book /// </summary> /// <param name="userId"></param> /// <param name="bookId"></param> public void PurchaseBook(int userId, int bookId) { WebbShopAPI api = new WebbShopAPI(); if (userId != 0 && bookId != 0) { var result = api.BuyBook(userId, bookId); if (result) { MessageViews.DisplaySuccessMessage(); return; } } PurchaseViews.DisplayErrorMessage(); }
/// <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); }