/// <summary> /// Deletes category from database by parameter id input. /// Also checks if user has been inactive for over 15 minutes /// </summary> /// <param name="userId"></param> /// <param name="catId"></param> /// <param name="userIsInactive"></param> /// <param name="errorMsg"></param> /// <returns>True if successful, else false</returns> internal bool DeleteCategor(int userId, int catId, out bool userIsInactive, out string errorMsg) { userIsInactive = false; errorMsg = ""; if (!IsSessionActive(userId, out string errorMsgIn)) { userIsInactive = true; errorMsg = errorMsgIn; return(false); } else { if (webAPI.DeleteCategory(userId, catId)) { return(true); } else { errorMsg = "Could not delete category"; Debug.WriteLine("webapi deleteCategory() returned false"); return(false); } } }
/// <summary> /// Allows admin user to delete book category /// </summary> /// <param name="adminId"></param> private void DeleteCategory(int adminId) { new Book().ViewAllCategories(); MessageViews.DisplaySelectMessage(); MessageViews.DisplayReturnMessage(); var categoryId = InputHelper.ParseInput(); if (categoryId == 0) { return; } WebbShopAPI api = new WebbShopAPI(); var result = api.DeleteCategory(adminId, categoryId); if (result) { MessageViews.DisplaySuccessMessage(); return; } MessageViews.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); }
public static void AdminMenu() { switch (Program.choise) { case "1": { Console.Write("Title: "); string title = Console.ReadLine(); Console.Write("Author: "); string author = Console.ReadLine(); Console.Write("Price: "); int price = Convert.ToInt32(Console.ReadLine()); Console.Write("Amount: "); int amount = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.AddBook(Program.userId, title, author, price, amount); break; } case "2": { View.AdminMenu.ShowBooks(); Console.Write("Choise the book (id): "); int bookId = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the actual amount: "); int amount = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.SetAmount(bookId, Program.userId, amount); break; } case "3": { var users = WebbShopAPI.ListUsers(Program.userId); Console.WriteLine(""); Console.WriteLine("==========================="); foreach (var user in users) { Console.WriteLine($"ID: {user.Id}. Username: {user.Name}"); } Console.WriteLine("==========================="); Console.WriteLine(""); Console.WriteLine("[Press any key to continue]"); Console.ReadKey(); break; } case "4": { Console.Write("Search: "); string keyword = Console.ReadLine(); var users = WebbShopAPI.FindUser(Program.userId, keyword); foreach (var user in users) { Console.WriteLine($"{user.Id}. {user.Name}"); } Console.WriteLine(""); Console.WriteLine("[Press any key to continue]"); Console.ReadKey(); break; } case "5": { View.AdminMenu.ShowBooks(); Console.Write("Choise the book (id): "); int bookId = Convert.ToInt32(Console.ReadLine()); Console.Write("New title: "); string title = Console.ReadLine(); Console.Write("New author: "); string author = Console.ReadLine(); Console.Write("New price: "); int price = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.UpdateBook(Program.userId, bookId, title, author, price); Console.WriteLine(""); Console.WriteLine("Success"); Console.WriteLine("[Press any key to continue]"); Console.ReadKey(); break; } case "6": { View.AdminMenu.ShowBooks(); Console.Write("Enter book id to delete: "); int bookId = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.DeleteBook(Program.userId, bookId); break; } case "7": { Console.Write("New category name: "); string catName = Console.ReadLine(); WebbShopAPI.AddCategory(Program.userId, catName); break; } case "8": { View.AdminMenu.ShowBooks(); Console.Write("Choise a book (id): "); int bookId = Convert.ToInt32(Console.ReadLine()); View.AdminMenu.ShowCategories(); Console.Write("Choise a category to add a book: "); int catId = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.AddBookToCategory(Program.userId, bookId, catId); break; } case "9": { View.AdminMenu.ShowCategories(); Console.Write("Choise a category: "); int catId = Convert.ToInt32(Console.ReadLine()); Console.Write("New name: "); string name = Console.ReadLine(); WebbShopAPI.UpdateCategory(Program.userId, catId, name); break; } case "10": { View.AdminMenu.ShowCategories(); Console.Write("Choise a category: "); int catId = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.DeleteCategory(Program.userId, catId); break; } case "11": { Console.Write("New username: "******"New password: "******"12": { var users = WebbShopAPI.ListUsers(Program.userId); Console.WriteLine(""); Console.WriteLine("==========================="); foreach (var user in users) { Console.WriteLine($"ID: {user.Id}. Username: {user.Name}"); } Console.WriteLine("==========================="); Console.Write("Choise a user to promote: "); int u = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.Promote(Program.userId, u); break; } case "13": { var users = WebbShopAPI.ListUsers(Program.userId); Console.WriteLine(""); Console.WriteLine("==========================="); foreach (var user in users) { Console.WriteLine($"ID: {user.Id}. Username: {user.Name}"); } Console.WriteLine("==========================="); Console.WriteLine("Choise a user to demote: "); int u = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.Demote(Program.userId, u); break; } case "14": { var users = WebbShopAPI.ListUsers(Program.userId); Console.WriteLine(""); Console.WriteLine("==========================="); foreach (var user in users) { Console.WriteLine($"ID: {user.Id}. Username: {user.Name}"); } Console.WriteLine("==========================="); Console.WriteLine("Choise a user to activate: "); int u = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.ActivateUser(Program.userId, u); break; } case "15": { var users = WebbShopAPI.ListUsers(Program.userId); Console.WriteLine(""); Console.WriteLine("==========================="); foreach (var user in users) { Console.WriteLine($"ID: {user.Id}. Username: {user.Name}"); } Console.WriteLine("==========================="); Console.WriteLine("Choise a user to inactivate: "); int u = Convert.ToInt32(Console.ReadLine()); WebbShopAPI.InactivateUser(Program.userId, u); break; } } }