added comments and rolecontroller updates

This commit is contained in:
thermalthrottle
2021-03-20 10:01:52 +02:00
parent 17fbd10d03
commit 2d44c67621
3 changed files with 446 additions and 17 deletions

View File

@@ -12,15 +12,58 @@ namespace Business.Business.Sales
{ {
private LuminousContext context; private LuminousContext context;
private User currentUser; private User currentUser;
/// <summary>
/// Constructor that accepts a user object
/// </summary>
/// <remarks>
/// User object is used for role checking
/// </remarks>
public ProductController(User currenUser) public ProductController(User currenUser)
{ {
this.currentUser = currenUser; this.currentUser = currenUser;
context = new LuminousContext(); this.context = new LuminousContext();
} }
/// <summary>
/// Constructor that accepts custom context and a user object
/// </summary>
/// <remarks>
/// Custom context is mainly used for Unit Testing
/// User object is used for role checking
/// </remarks>
public ProductController(LuminousContext context, User currenUser)
{
this.currentUser = currenUser;
this.context = context;
}
/// <summary>
/// Gets All Roles
/// </summary>
/// <remarks>
/// Requires no special roles
/// </remarks>
/// <returns>
/// Returns a ICollection of all roles.
/// </returns>
public ICollection<Product> GetAll() public ICollection<Product> GetAll()
{ {
return context.Product.ToList(); return context.Product.ToList();
} }
/// <summary>
/// Searches the role by given Id
/// </summary>
/// <returns>
/// Returns an object of the role with the given Id.
///
/// Requires no special roles
/// </returns>
public Product Get(int id) public Product Get(int id)
{ {
var item = context.Product.Find(id); var item = context.Product.Find(id);
@@ -33,6 +76,16 @@ namespace Business.Business.Sales
throw new ArgumentException("Product Id not found!"); throw new ArgumentException("Product Id not found!");
} }
} }
/// <summary>
/// Searches the role by given name
/// </summary>
/// <returns>
/// Returns an object of the role with the given name.
///
/// Requires no special roles
/// </returns>
public Product Get(string name) public Product Get(string name)
{ {
var item = context.Product.FirstOrDefault(p => p.Name == 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!"); throw new ArgumentException("Product name not found!");
} }
} }
/// <summary>
/// Searches the role by a given substring
/// </summary>
/// <returns>
/// Returns an ICollection of all roles that contain the given substring in their name.
///
/// Requires no special roles
/// </returns>
public ICollection<Product> GetByApproximateName(string name) public ICollection<Product> GetByApproximateName(string name)
{ {
var items = context.Product.Where(u => u.Name.Contains(name)).ToList(); 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!"); throw new ArgumentException("No products added in the database!");
} }
} }
/// <summary>
/// Adds an product in the database
/// </summary>
/// <remarks>
/// Accepts an item name and price.
///
/// Requires no special roles
/// </remarks>
public void AddItem(string name, double price) public void AddItem(string name, double price)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -84,6 +157,16 @@ namespace Business.Business.Sales
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the name of the given product
/// </summary>
/// <remarks>
/// Accepts the id for getting the product.
///
/// Requires Admin role
/// </remarks>
public void UpdateName(int id, string newName) public void UpdateName(int id, string newName)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -111,6 +194,16 @@ namespace Business.Business.Sales
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the name of the given product
/// </summary>
/// <remarks>
/// Accepts the current name for getting the product.
///
/// Requires Admin role
/// </remarks>
public void UpdateName(string oldName, string newName) public void UpdateName(string oldName, string newName)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -138,6 +231,16 @@ namespace Business.Business.Sales
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the price of the given product
/// </summary>
/// <remarks>
/// Accepts the id for getting the product.
///
/// Requires Admin role
/// </remarks>
public void UpdatePrice(int id, double price) public void UpdatePrice(int id, double price)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -165,6 +268,16 @@ namespace Business.Business.Sales
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the price of the given product
/// </summary>
/// <remarks>
/// Accepts the name for getting the product.
///
/// Requires Admin role
/// </remarks>
public void UpdatePrice(string name, double price) public void UpdatePrice(string name, double price)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -192,6 +305,16 @@ namespace Business.Business.Sales
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Deletes the given product
/// </summary>
/// <remarks>
/// Accepts an product for getting the product
///
/// Requires Admin role
/// </remarks>
public void Delete(int id) public void Delete(int id)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -212,6 +335,16 @@ namespace Business.Business.Sales
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Deletes the given product
/// </summary>
/// <remarks>
/// Accepts an name for getting the product
///
/// Requires Admin role
/// </remarks>
public void Delete(string name) public void Delete(string name)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)

View File

@@ -8,15 +8,54 @@ using System.Text;
namespace Business.Business.UserManagment.Controllers namespace Business.Business.UserManagment.Controllers
{ {
public class RoleController : IReadOnlyController<Role> public class RoleController : IReadOnlyController<Role>
{ {
private LuminousContext context; private LuminousContext context;
private User currentUser; private User currentUser;
/// <summary>
/// Empty Constructor
/// </summary>
/// <remarks>
/// Used for Initialiation of the roles in the database
/// </remarks>
public RoleController(){}
/// <summary>
/// Constructor that accepts a user object
/// </summary>
/// <remarks>
/// User object is used for role checking
/// </remarks>
public RoleController(User currentUser) public RoleController(User currentUser)
{ {
this.context = new LuminousContext(); this.context = new LuminousContext();
this.currentUser = currentUser; this.currentUser = currentUser;
} }
/// <summary>
/// Constructor that accepts custom context and a user object
/// </summary>
/// <remarks>
/// Custom context is mainly used for Unit Testing
/// User object is used for role checking
/// </remarks>
public RoleController(LuminousContext context, User currentUser)
{
this.context = context;
this.currentUser = currentUser;
}
/// <summary>
/// Creates the roles
/// </summary>
/// <remarks>
/// Almost every method of each class checks if the user has suffficient roles for the task
/// </remarks>
public void CreateInitialRoles() public void CreateInitialRoles()
{ {
var Admin = new Role("Admin"); var Admin = new Role("Admin");
@@ -25,6 +64,14 @@ namespace Business.Business.UserManagment.Controllers
context.Role.AddRange(Admin, Manager, Cashier); context.Role.AddRange(Admin, Manager, Cashier);
context.SaveChanges(); context.SaveChanges();
} }
/// <summary>
/// Gets All Roles
/// </summary>
/// <returns>
/// Returns a ICollection of all roles
/// </returns>
public ICollection<Role> GetAll() public ICollection<Role> GetAll()
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -36,6 +83,16 @@ namespace Business.Business.UserManagment.Controllers
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Searches the role by given Id
/// </summary>
/// <returns>
/// Returns an object of the role with the given Id
///
/// Requires Admin role.
/// </returns>
public Role Get(int id) public Role Get(int id)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -47,6 +104,16 @@ namespace Business.Business.UserManagment.Controllers
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Searches the role by given name
/// </summary>
/// <returns>
/// Returns an object of the role with the given name
///
/// Requires Admin role.
/// </returns>
public Role Get(string name) public Role Get(string name)
{ {
if (currentUser.RoleId == 3) if (currentUser.RoleId == 3)
@@ -58,11 +125,21 @@ namespace Business.Business.UserManagment.Controllers
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
public ICollection<Role> GetByApproximateName(string name)
/// <summary>
/// Searches the role by a given substring
/// </summary>
/// <returns>
/// Returns an ICollection of all roles that contain the given substring in their name.
///
/// Requires Admin role.
/// </returns>
public ICollection<Role> GetByApproximateName(string substring)
{ {
if (currentUser.RoleId == 3) 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 else
{ {

View File

@@ -12,16 +12,56 @@ namespace Business.Business.UserManagment
private LuminousContext context; private LuminousContext context;
private RoleController rolectrl; private RoleController rolectrl;
private User currentUser; private User currentUser;
/// <summary>
/// Empty Constructor
/// </summary>
/// <remarks>
/// Used for Initialiation of the roles in the database
/// </remarks>
public UserController() public UserController()
{ {
this.context = new LuminousContext(); this.context = new LuminousContext();
} }
/// <summary>
/// Constructor that accepts a user object
/// </summary>
/// <remarks>
/// User object is used for role checking
/// </remarks>
public UserController(User currentUser) public UserController(User currentUser)
{ {
this.currentUser = currentUser; this.currentUser = currentUser;
this.context = new LuminousContext(); this.context = new LuminousContext();
this.rolectrl = new RoleController(currentUser); this.rolectrl = new RoleController(currentUser);
} }
/// <summary>
/// Constructor that accepts custom context and a user object
/// </summary>
/// <remarks>
/// Custom context is mainly used for Unit Testing
/// User object is used for role checking
/// </remarks>
public UserController(User currentUser, LuminousContext context)
{
this.currentUser = currentUser;
this.context = context;
}
/// <summary>
/// Gets All Users
/// </summary>
/// <returns>
/// Returns a ICollection of all users.
///
/// Requires Admin role
/// </returns>
public ICollection<User> GetAll() public ICollection<User> GetAll()
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -33,14 +73,30 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Checks if there's a user in the database
/// </summary>
/// <remarks>
/// Can be used with an empty constructor
/// </remarks>
public bool CheckIfUserEverCreated() public bool CheckIfUserEverCreated()
{ {
if (context.User.ToList().Any()) if (context.User.ToList().Any())
{ {
return false; return true;
} }
return true; return false;
} }
/// <summary>
/// Searches the user by given Id
/// </summary>
/// <returns>
/// Returns an object of the user with the given Id
/// </returns>
public User Get(int id) public User Get(int id)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -52,6 +108,16 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Searches the user by given name
/// </summary>
/// <returns>
/// Returns an object of the user with the given name.
///
/// Requires Admin role
/// </returns>
public User Get(string name) public User Get(string name)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -63,6 +129,38 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Searches the user by a given substring
/// </summary>
/// <returns>
/// Returns an ICollection of all users that contain the given substring in their name.
///
/// Requires Admin role
/// </returns>
public ICollection<User> 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!");
}
}
/// <summary>
/// Checks if the password is valid
/// </summary>
/// <remarks>
/// Password is used to log in the user
/// </remarks>
/// <returns>
/// Returns an object of the found user
/// </returns>
public User ValidatePassword(string password) public User ValidatePassword(string password)
{ {
var user = context.User.FirstOrDefault(); var user = context.User.FirstOrDefault();
@@ -72,23 +170,30 @@ namespace Business.Business.UserManagment
} }
return user; return user;
} }
public ICollection<User> GetByApproximateName(string name)
{ /// <summary>
if (currentUser != null || currentUser.RoleId == 3) /// Registers an user
{ /// </summary>
return context.User.Where(u => u.Name.Contains(name)).ToList(); /// <remarks>
} /// Used for the creation of the initial user, so it assigns admin role by default
else /// </remarks>
{
throw new ArgumentException("Insufficient Role!");
}
}
public void RegisterItem(string name, string password) public void RegisterItem(string name, string password)
{ {
var user = new User(name, password, 1); var user = new User(name, password, 1);
context.User.Add(user); context.User.Add(user);
context.SaveChanges(); context.SaveChanges();
} }
/// <summary>
/// Registers an user
/// </summary>
/// <remarks>
/// Accepts an role id so it can assign a role to the user.
///
/// Requires Admin role
/// </remarks>
public void RegisterItem(string name, string password, int roleId) public void RegisterItem(string name, string password, int roleId)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -121,6 +226,16 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Registers an user
/// </summary>
/// <remarks>
/// Accepts an role name so it can assign a role to the user.
///
/// Requires Admin role
/// </remarks>
public void RegisterItem(string name, string password, string roleName) public void RegisterItem(string name, string password, string roleName)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -153,6 +268,16 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the username of the given user
/// </summary>
/// <remarks>
/// Accepts an id for getting the user.
///
/// Requires Admin role
/// </remarks>
public void UpdateName(int id, string newName) public void UpdateName(int id, string newName)
{ {
if (currentUser != null || currentUser.Id == 3) if (currentUser != null || currentUser.Id == 3)
@@ -180,6 +305,16 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the username of the given user
/// </summary>
/// <remarks>
/// Accepts the current name for getting the user.
///
/// Requires Admin role
/// </remarks>
public void UpdateName(string oldName, string newName) public void UpdateName(string oldName, string newName)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -207,6 +342,16 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the password of the given user
/// </summary>
/// <remarks>
/// Accepts an id for getting the user.
///
/// Requires Admin role
/// </remarks>
public void UpdatePassword(int id, string newPassword) public void UpdatePassword(int id, string newPassword)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -234,6 +379,16 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the password of the given user
/// </summary>
/// <remarks>
/// Accepts the name for getting the user.
///
/// Requires Admin role
/// </remarks>
public void UpdatePassword(string name, string newPassword) public void UpdatePassword(string name, string newPassword)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -261,6 +416,17 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the role of the given user
/// </summary>
/// <remarks>
/// Accepts an user id for getting the user.
/// Accepts an role id for getting the role.
///
/// Requires Admin role.
/// </remarks>
public void UpdateRole(int id, int RoleId) public void UpdateRole(int id, int RoleId)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -289,6 +455,17 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the role of the given user
/// </summary>
/// <remarks>
/// Accepts an user id for getting the user.
/// Accepts an role name for getting the role.
///
/// Requires Admin role.
/// </remarks>
public void UpdateRole(int id, string roleName) public void UpdateRole(int id, string roleName)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -317,6 +494,17 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the role of the given user
/// </summary>
/// <remarks>
/// Accepts an username for getting the user.
/// Accepts an role id for getting the role.
///
/// Requires Admin role.
/// </remarks>
public void UpdateRole(string name, int roleId) public void UpdateRole(string name, int roleId)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -345,6 +533,17 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Updates the role of the given user
/// </summary>
/// <remarks>
/// Accepts an username for getting the user.
/// Accepts an role name for getting the role.
///
/// Requires Admin role.
/// </remarks>
public void UpdateRole(string name, string roleName) public void UpdateRole(string name, string roleName)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -373,6 +572,16 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Deletes the given user
/// </summary>
/// <remarks>
/// Accepts an user id for getting the user.
///
/// Requires Admin role.
/// </remarks>
public void Delete(int id) public void Delete(int id)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)
@@ -393,6 +602,16 @@ namespace Business.Business.UserManagment
throw new ArgumentException("Insufficient Role!"); throw new ArgumentException("Insufficient Role!");
} }
} }
/// <summary>
/// Deletes the given user
/// </summary>
/// <remarks>
/// Accepts an username for getting the user.
///
/// Requires Admin role.
/// </remarks>
public void Delete(string name) public void Delete(string name)
{ {
if (currentUser != null || currentUser.RoleId == 3) if (currentUser != null || currentUser.RoleId == 3)