From 2d44c67621196c92528872aee0a9ecc897d4a55d Mon Sep 17 00:00:00 2001 From: thermalthrottle Date: Sat, 20 Mar 2021 10:01:52 +0200 Subject: [PATCH] added comments and rolecontroller updates --- .../Business/Sales/ProductController.cs | 135 +++++++++- .../Controllers/RoleController.cs | 83 +++++- .../Controllers/UserController.cs | 245 +++++++++++++++++- 3 files changed, 446 insertions(+), 17 deletions(-) diff --git a/LuminousSales/Business/Business/Sales/ProductController.cs b/LuminousSales/Business/Business/Sales/ProductController.cs index 76501b3..c3228d7 100644 --- a/LuminousSales/Business/Business/Sales/ProductController.cs +++ b/LuminousSales/Business/Business/Sales/ProductController.cs @@ -12,15 +12,58 @@ namespace Business.Business.Sales { private LuminousContext context; private User currentUser; + + /// + /// Constructor that accepts a user object + /// + /// + /// User object is used for role checking + /// + public ProductController(User currenUser) { this.currentUser = currenUser; - context = new LuminousContext(); + this.context = new LuminousContext(); } + + /// + /// Constructor that accepts custom context and a user object + /// + /// + /// Custom context is mainly used for Unit Testing + /// User object is used for role checking + /// + + public ProductController(LuminousContext context, User currenUser) + { + this.currentUser = currenUser; + this.context = context; + } + + /// + /// Gets All Roles + /// + /// + /// Requires no special roles + /// + /// + /// Returns a ICollection of all roles. + /// + public ICollection GetAll() { return context.Product.ToList(); } + + /// + /// Searches the role by given Id + /// + /// + /// Returns an object of the role with the given Id. + /// + /// Requires no special roles + /// + public Product Get(int id) { var item = context.Product.Find(id); @@ -33,6 +76,16 @@ namespace Business.Business.Sales throw new ArgumentException("Product Id not found!"); } } + + /// + /// Searches the role by given name + /// + /// + /// Returns an object of the role with the given name. + /// + /// Requires no special roles + /// + public Product Get(string name) { var item = context.Product.FirstOrDefault(p => p.Name == name); @@ -45,6 +98,16 @@ namespace Business.Business.Sales throw new ArgumentException("Product name not found!"); } } + + /// + /// Searches the role by a given substring + /// + /// + /// Returns an ICollection of all roles that contain the given substring in their name. + /// + /// Requires no special roles + /// + public ICollection GetByApproximateName(string name) { var items = context.Product.Where(u => u.Name.Contains(name)).ToList(); @@ -57,6 +120,16 @@ namespace Business.Business.Sales throw new ArgumentException("No products added in the database!"); } } + + /// + /// Adds an product in the database + /// + /// + /// Accepts an item name and price. + /// + /// Requires no special roles + /// + public void AddItem(string name, double price) { if (currentUser.RoleId == 3) @@ -84,6 +157,16 @@ namespace Business.Business.Sales throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the name of the given product + /// + /// + /// Accepts the id for getting the product. + /// + /// Requires Admin role + /// + public void UpdateName(int id, string newName) { if (currentUser.RoleId == 3) @@ -111,6 +194,16 @@ namespace Business.Business.Sales throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the name of the given product + /// + /// + /// Accepts the current name for getting the product. + /// + /// Requires Admin role + /// + public void UpdateName(string oldName, string newName) { if (currentUser.RoleId == 3) @@ -138,6 +231,16 @@ namespace Business.Business.Sales throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the price of the given product + /// + /// + /// Accepts the id for getting the product. + /// + /// Requires Admin role + /// + public void UpdatePrice(int id, double price) { if (currentUser.RoleId == 3) @@ -165,6 +268,16 @@ namespace Business.Business.Sales throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the price of the given product + /// + /// + /// Accepts the name for getting the product. + /// + /// Requires Admin role + /// + public void UpdatePrice(string name, double price) { if (currentUser.RoleId == 3) @@ -192,6 +305,16 @@ namespace Business.Business.Sales throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Deletes the given product + /// + /// + /// Accepts an product for getting the product + /// + /// Requires Admin role + /// + public void Delete(int id) { if (currentUser.RoleId == 3) @@ -212,6 +335,16 @@ namespace Business.Business.Sales throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Deletes the given product + /// + /// + /// Accepts an name for getting the product + /// + /// Requires Admin role + /// + public void Delete(string name) { if (currentUser.RoleId == 3) diff --git a/LuminousSales/Business/Business/UserManagment/Controllers/RoleController.cs b/LuminousSales/Business/Business/UserManagment/Controllers/RoleController.cs index d6af104..60cec1c 100644 --- a/LuminousSales/Business/Business/UserManagment/Controllers/RoleController.cs +++ b/LuminousSales/Business/Business/UserManagment/Controllers/RoleController.cs @@ -8,15 +8,54 @@ using System.Text; namespace Business.Business.UserManagment.Controllers { - public class RoleController : IReadOnlyController + public class RoleController : IReadOnlyController { private LuminousContext context; private User currentUser; + + /// + /// Empty Constructor + /// + /// + /// Used for Initialiation of the roles in the database + /// + + public RoleController(){} + + /// + /// Constructor that accepts a user object + /// + /// + /// User object is used for role checking + /// + public RoleController(User currentUser) { this.context = new LuminousContext(); this.currentUser = currentUser; } + + /// + /// Constructor that accepts custom context and a user object + /// + /// + /// Custom context is mainly used for Unit Testing + /// User object is used for role checking + /// + + public RoleController(LuminousContext context, User currentUser) + { + this.context = context; + this.currentUser = currentUser; + } + + /// + /// Creates the roles + /// + /// + /// Almost every method of each class checks if the user has suffficient roles for the task + /// + public void CreateInitialRoles() { var Admin = new Role("Admin"); @@ -25,6 +64,14 @@ namespace Business.Business.UserManagment.Controllers context.Role.AddRange(Admin, Manager, Cashier); context.SaveChanges(); } + + /// + /// Gets All Roles + /// + /// + /// Returns a ICollection of all roles + /// + public ICollection GetAll() { if (currentUser.RoleId == 3) @@ -36,6 +83,16 @@ namespace Business.Business.UserManagment.Controllers throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Searches the role by given Id + /// + /// + /// Returns an object of the role with the given Id + /// + /// Requires Admin role. + /// + public Role Get(int id) { if (currentUser.RoleId == 3) @@ -47,6 +104,16 @@ namespace Business.Business.UserManagment.Controllers throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Searches the role by given name + /// + /// + /// Returns an object of the role with the given name + /// + /// Requires Admin role. + /// + public Role Get(string name) { if (currentUser.RoleId == 3) @@ -58,11 +125,21 @@ namespace Business.Business.UserManagment.Controllers throw new ArgumentException("Insufficient Role!"); } } - public ICollection GetByApproximateName(string name) + + /// + /// Searches the role by a given substring + /// + /// + /// Returns an ICollection of all roles that contain the given substring in their name. + /// + /// Requires Admin role. + /// + + public ICollection GetByApproximateName(string substring) { if (currentUser.RoleId == 3) { - return context.Role.Where(u => u.Name.Contains(name)).ToList(); + return context.Role.Where(u => u.Name.Contains(substring)).ToList(); } else { diff --git a/LuminousSales/Business/Business/UserManagment/Controllers/UserController.cs b/LuminousSales/Business/Business/UserManagment/Controllers/UserController.cs index 01d9157..0bc23db 100644 --- a/LuminousSales/Business/Business/UserManagment/Controllers/UserController.cs +++ b/LuminousSales/Business/Business/UserManagment/Controllers/UserController.cs @@ -12,16 +12,56 @@ namespace Business.Business.UserManagment private LuminousContext context; private RoleController rolectrl; private User currentUser; + + /// + /// Empty Constructor + /// + /// + /// Used for Initialiation of the roles in the database + /// + public UserController() { this.context = new LuminousContext(); } + + /// + /// Constructor that accepts a user object + /// + /// + /// User object is used for role checking + /// + public UserController(User currentUser) { this.currentUser = currentUser; this.context = new LuminousContext(); this.rolectrl = new RoleController(currentUser); } + + /// + /// Constructor that accepts custom context and a user object + /// + /// + /// Custom context is mainly used for Unit Testing + /// User object is used for role checking + /// + + public UserController(User currentUser, LuminousContext context) + { + this.currentUser = currentUser; + this.context = context; + } + + /// + /// Gets All Users + /// + /// + /// Returns a ICollection of all users. + /// + /// Requires Admin role + /// + public ICollection GetAll() { if (currentUser != null || currentUser.RoleId == 3) @@ -33,14 +73,30 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Checks if there's a user in the database + /// + /// + /// Can be used with an empty constructor + /// + public bool CheckIfUserEverCreated() { if (context.User.ToList().Any()) { - return false; + return true; } - return true; + return false; } + + /// + /// Searches the user by given Id + /// + /// + /// Returns an object of the user with the given Id + /// + public User Get(int id) { if (currentUser != null || currentUser.RoleId == 3) @@ -52,6 +108,16 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Searches the user by given name + /// + /// + /// Returns an object of the user with the given name. + /// + /// Requires Admin role + /// + public User Get(string name) { if (currentUser != null || currentUser.RoleId == 3) @@ -63,6 +129,38 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Searches the user by a given substring + /// + /// + /// Returns an ICollection of all users that contain the given substring in their name. + /// + /// Requires Admin role + /// + + public ICollection GetByApproximateName(string substring) + { + if (currentUser != null || currentUser.RoleId == 3) + { + return context.User.Where(u => u.Name.Contains(substring)).ToList(); + } + else + { + throw new ArgumentException("Insufficient Role!"); + } + } + + /// + /// Checks if the password is valid + /// + /// + /// Password is used to log in the user + /// + /// + /// Returns an object of the found user + /// + public User ValidatePassword(string password) { var user = context.User.FirstOrDefault(); @@ -72,23 +170,30 @@ namespace Business.Business.UserManagment } return user; } - public ICollection GetByApproximateName(string name) - { - if (currentUser != null || currentUser.RoleId == 3) - { - return context.User.Where(u => u.Name.Contains(name)).ToList(); - } - else - { - throw new ArgumentException("Insufficient Role!"); - } - } + + /// + /// Registers an user + /// + /// + /// Used for the creation of the initial user, so it assigns admin role by default + /// + public void RegisterItem(string name, string password) { var user = new User(name, password, 1); context.User.Add(user); context.SaveChanges(); } + + /// + /// Registers an user + /// + /// + /// Accepts an role id so it can assign a role to the user. + /// + /// Requires Admin role + /// + public void RegisterItem(string name, string password, int roleId) { if (currentUser != null || currentUser.RoleId == 3) @@ -121,6 +226,16 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Registers an user + /// + /// + /// Accepts an role name so it can assign a role to the user. + /// + /// Requires Admin role + /// + public void RegisterItem(string name, string password, string roleName) { if (currentUser != null || currentUser.RoleId == 3) @@ -153,6 +268,16 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the username of the given user + /// + /// + /// Accepts an id for getting the user. + /// + /// Requires Admin role + /// + public void UpdateName(int id, string newName) { if (currentUser != null || currentUser.Id == 3) @@ -180,6 +305,16 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the username of the given user + /// + /// + /// Accepts the current name for getting the user. + /// + /// Requires Admin role + /// + public void UpdateName(string oldName, string newName) { if (currentUser != null || currentUser.RoleId == 3) @@ -207,6 +342,16 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the password of the given user + /// + /// + /// Accepts an id for getting the user. + /// + /// Requires Admin role + /// + public void UpdatePassword(int id, string newPassword) { if (currentUser != null || currentUser.RoleId == 3) @@ -234,6 +379,16 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the password of the given user + /// + /// + /// Accepts the name for getting the user. + /// + /// Requires Admin role + /// + public void UpdatePassword(string name, string newPassword) { if (currentUser != null || currentUser.RoleId == 3) @@ -261,6 +416,17 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the role of the given user + /// + /// + /// Accepts an user id for getting the user. + /// Accepts an role id for getting the role. + /// + /// Requires Admin role. + /// + public void UpdateRole(int id, int RoleId) { if (currentUser != null || currentUser.RoleId == 3) @@ -289,6 +455,17 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the role of the given user + /// + /// + /// Accepts an user id for getting the user. + /// Accepts an role name for getting the role. + /// + /// Requires Admin role. + /// + public void UpdateRole(int id, string roleName) { if (currentUser != null || currentUser.RoleId == 3) @@ -317,6 +494,17 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the role of the given user + /// + /// + /// Accepts an username for getting the user. + /// Accepts an role id for getting the role. + /// + /// Requires Admin role. + /// + public void UpdateRole(string name, int roleId) { if (currentUser != null || currentUser.RoleId == 3) @@ -345,6 +533,17 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Updates the role of the given user + /// + /// + /// Accepts an username for getting the user. + /// Accepts an role name for getting the role. + /// + /// Requires Admin role. + /// + public void UpdateRole(string name, string roleName) { if (currentUser != null || currentUser.RoleId == 3) @@ -373,6 +572,16 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Deletes the given user + /// + /// + /// Accepts an user id for getting the user. + /// + /// Requires Admin role. + /// + public void Delete(int id) { if (currentUser != null || currentUser.RoleId == 3) @@ -393,6 +602,16 @@ namespace Business.Business.UserManagment throw new ArgumentException("Insufficient Role!"); } } + + /// + /// Deletes the given user + /// + /// + /// Accepts an username for getting the user. + /// + /// Requires Admin role. + /// + public void Delete(string name) { if (currentUser != null || currentUser.RoleId == 3)