diff --git a/LuminousSales/Business/Business/Sales/ProductController.cs b/LuminousSales/Business/Business/Sales/ProductController.cs index 4673858..0b01c1a 100644 --- a/LuminousSales/Business/Business/Sales/ProductController.cs +++ b/LuminousSales/Business/Business/Sales/ProductController.cs @@ -390,22 +390,15 @@ namespace Business.Business.Sales public void RemoveAmount(int productId, double Amount) { - if (currentUser.RoleId > 1) + var product = Get(productId); + if (product != null) { - var product = Get(productId); - if (product != null) - { - product.AmountInStock -= Amount; - context.SaveChanges(); - } - else - { - throw new ArgumentException("Product id not valid!"); - } + product.AmountInStock -= Amount; + context.SaveChanges(); } else { - throw new ArgumentException("Insufficient Role!"); + throw new ArgumentException("Product id not valid!"); } } diff --git a/LuminousSales/Display/Views/AdminView.cs b/LuminousSales/Display/Views/AdminView.cs index 435380f..ff7bf48 100644 --- a/LuminousSales/Display/Views/AdminView.cs +++ b/LuminousSales/Display/Views/AdminView.cs @@ -10,6 +10,10 @@ namespace Display.Views public class AdminView : ManagerView { User currentUser; + + /// + /// Constructor that accepts a user object. + /// public AdminView(User currentUser) : base(currentUser) { this.currentUser = currentUser; @@ -18,7 +22,7 @@ namespace Display.Views /// Shows the avaliable to the user commands. /// /// - /// The main menu. + /// The main menu. Inherited from ManagerView and adds Admin-specific menu. /// public override void ShowAvaliableCommands() { @@ -28,6 +32,10 @@ namespace Display.Views /// /// Asks the user to choose which group of action to use. /// + /// + /// A choice is given by entering a number from the given list. + /// Inherited from ManagerView and expanded with the Admin menu. + /// public override void ActionHandle() { try @@ -207,8 +215,9 @@ namespace Display.Views Console.WriteLine(e.Message); } } + /// - /// Lists all users which match the search term from the database. + /// Lists all users who match the search term from the database. /// public void GetByApproximateName() { @@ -241,7 +250,8 @@ namespace Display.Views string username = Console.ReadLine(); Console.Write("Enter password: "); string password = Console.ReadLine(); - Console.Write("Enter role ID or name (default 1, Cashier): "); + Console.WriteLine("Avaliable roles: 1 - Cashier, 2 - Manager, 3 - Admin"); + Console.Write("Enter role ID or name: "); string role = Console.ReadLine(); bool result = int.TryParse(role, out int roleId); if (role == null) @@ -268,27 +278,38 @@ namespace Display.Views /// /// Changes the role given to a specific user. /// + /// + /// Asks for a user's ID or name and the new role ID or name. + /// public void UpdateRole() { try { UserController userctl = new UserController(currentUser); Console.WriteLine("Updating role..."); - Console.Write("Enter username: "); + Console.Write("Enter username or user ID: "); string username = Console.ReadLine(); Console.Write("Enter new role ID or name: "); string role = Console.ReadLine(); - bool result = int.TryParse(role, out int roleId); - if (result) + bool userResult = int.TryParse(username, out int userId); + bool roleResult = int.TryParse(role, out int roleId); + if (userResult && roleResult) + { + userctl.UpdateRole(userId, roleId); + } + else if (userResult && !roleResult) + { + userctl.UpdateRole(userId, role); + } + else if (!userResult && roleResult) { userctl.UpdateRole(username, roleId); - Console.WriteLine("Updated role successfully"); } else { userctl.UpdateRole(username, role); - Console.WriteLine("Updated role successfully"); } + Console.WriteLine("Updated role successfully"); } catch (Exception e) { @@ -384,7 +405,7 @@ namespace Display.Views } } /// - /// Add product to the database. + /// Adds a product to the database. /// public void AddItem() { diff --git a/LuminousSales/Display/Views/BaseView.cs b/LuminousSales/Display/Views/BaseView.cs index 02f3b04..5d8fc1d 100644 --- a/LuminousSales/Display/Views/BaseView.cs +++ b/LuminousSales/Display/Views/BaseView.cs @@ -12,11 +12,28 @@ namespace Display.Views internal ProductController productctrl; private DealController dealctrl; internal User currentUser; + + /// + /// Constructor that accepts a user object. + /// + /// + /// User object is used for stock and deal checking. + /// Initialises stock and deal controllers. + /// public BaseView(User currentUser) { this.currentUser = currentUser; this.dealctrl = new DealController(currentUser); + this.productctrl = new ProductController(currentUser); } + + /// + /// Shows all available commands. + /// + /// + /// Includes only the basic functions of the program. + /// The main menu. + /// public virtual void ShowAvaliableCommands() { Console.WriteLine(); @@ -25,6 +42,14 @@ namespace Display.Views Console.WriteLine(); Console.WriteLine("1. Sales"); } + + /// + /// Asks the user to choose which group of action to use. + /// + /// + /// A choice is given by entering a number from the given list. + /// It's expanded by its inheritors. + /// public virtual void ActionHandle() { try @@ -51,6 +76,10 @@ namespace Display.Views Console.WriteLine(e.Message); } } + + /// + /// Selection menu with base actions. + /// public void SaleHandle() { bool running = true; @@ -90,6 +119,10 @@ namespace Display.Views } } + + /// + /// Lists all products which match the search term from the database. + /// private void SearchItem() { try @@ -97,9 +130,10 @@ namespace Display.Views productctrl = new ProductController(currentUser); Console.Write("Search item: "); string search = Console.ReadLine(); + Console.WriteLine("Product ID - Name - Price - Amount"); foreach (var item in productctrl.GetByApproximateName(search).ToList()) { - Console.WriteLine($"{item.Id} {item.Name} {item.Price} {item.AmountInStock}"); + Console.WriteLine($"{item.Id} - {item.Name} - {item.Price} - {item.AmountInStock}"); } } catch (Exception e) @@ -108,10 +142,15 @@ namespace Display.Views Console.WriteLine(e.Message); } } + + /// + /// Sells products. Asks which product and the quantity sold. Prints a summary in the end. + /// private void SaleItem() { try { + productctrl = new ProductController(currentUser); List check = new List(); bool endTyped = false; while (!endTyped) @@ -141,14 +180,16 @@ namespace Display.Views else { endTyped = true; + Console.WriteLine(); Console.WriteLine("Check"); double sum = 0; + check.Reverse(); var lastdeals = dealctrl.GetAll().OrderByDescending(x => x.Id).ToArray(); - for (int i = 0; i < check.Count; i++) + for (int i = check.Count - 1; i >= 0; i--) { sum += check[i].Price * lastdeals[i].Amount; int rowNum = i + 1; - Console.WriteLine($"{rowNum} {check[i].Name} {check[i].Price}x{lastdeals[i].Amount}"); + Console.WriteLine($"{rowNum}. {check[i].Name} - {check[i].Price}x{lastdeals[i].Amount}"); } Console.WriteLine($"Total: {sum}"); } @@ -156,7 +197,6 @@ namespace Display.Views } catch (Exception e) { - Console.WriteLine(e.Message); } } diff --git a/LuminousSales/Display/Views/ManagerView.cs b/LuminousSales/Display/Views/ManagerView.cs index 91a3dfd..a430112 100644 --- a/LuminousSales/Display/Views/ManagerView.cs +++ b/LuminousSales/Display/Views/ManagerView.cs @@ -16,11 +16,8 @@ namespace Display.Views /// /// /// User object is used for stock and deal checking. - /// - /// /// Initialises stock and deal controllers. /// - public ManagerView(User currentUser):base(currentUser) { stockctrl = new StockController(currentUser); @@ -32,12 +29,8 @@ namespace Display.Views /// /// /// Inherits all available commands from the base view. - /// - /// /// The main menu. /// - - public override void ShowAvaliableCommands() { base.ShowAvaliableCommands(); @@ -48,15 +41,10 @@ namespace Display.Views /// Asks the user to choose which group of action to use. /// /// - /// If user inputs the digit 1, returns selling handles. - /// - /// - /// If user inputs the digit 2, returns managing handles. - /// - /// + /// If user inputs the digit 1, returns selling handles. + /// If user inputs the digit 2, returns managing handles. /// If user inputs something else, the operation is invalid. /// - public override void ActionHandle() { try @@ -93,7 +81,6 @@ namespace Display.Views /// /// Requires role level 2 (Manager). /// - public void ManageHandle() { bool running = true; @@ -104,11 +91,12 @@ namespace Display.Views Console.WriteLine("0. Back"); Console.WriteLine(); Console.WriteLine("Stock Managment"); - Console.WriteLine("1. GetAll"); - Console.WriteLine("2. Get"); - Console.WriteLine("3. GetByTime"); - Console.WriteLine("4. Add"); + Console.WriteLine("1. List all stocks"); + Console.WriteLine("2. Get stock by ID"); + Console.WriteLine("3. List stocks by time"); + Console.WriteLine("4. Add stock"); Console.WriteLine("5. Delete"); + Console.WriteLine("6. List deals by user"); Console.Write("> "); try { @@ -130,6 +118,9 @@ namespace Display.Views case 5: Delete(); break; + case 6: + GetByUser(); + break; case 0: running = false; break; @@ -149,7 +140,6 @@ namespace Display.Views /// /// Lists all information about stock from the database. /// - public void GetAll() { try @@ -171,8 +161,7 @@ namespace Display.Views /// /// Lists all registered information about stocks from the database. - /// - + /// public void Get() { @@ -197,8 +186,6 @@ namespace Display.Views /// /// /// Inputs start time and end time. - /// - /// /// Lists all information about stocks from the database in real time. /// public void GetByTime() @@ -228,14 +215,9 @@ namespace Display.Views /// /// /// Entering product name and amount. - /// - /// /// If the result is true, returns a stock with product id, amount and a real time. - /// - /// /// Else returns a stock with product name, amount and a real time. /// - public void Add() { try @@ -265,7 +247,6 @@ namespace Display.Views /// /// Deletes a stock from the database. /// - public void Delete() { try @@ -282,5 +263,41 @@ namespace Display.Views } } + /// + /// Gets deals by the user who made them. + /// + /// + /// Inputs username or user ID. + /// Lists all deals made by a user from the database. + /// + public void GetByUser() + { + try + { + Console.WriteLine("Getting stock by time..."); + Console.Write("Enter username or user ID: "); + string input = Console.ReadLine(); + int.TryParse(input, out int inputId); + ICollection output; + if (inputId != 0) + { + output = dealctrl.GetByUser(inputId); + } + else + { + output = dealctrl.GetByUser(input); + } + Console.WriteLine("Deal ID - Product ID - Amount - Time"); + foreach (var item in output) + { + Console.WriteLine($"{item.Id} - {item.ProductId} - {item.Amount} - {item.Time}"); + } + + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + } } }