Controllers Update

This commit is contained in:
thermalthrottle
2021-03-18 23:56:49 +02:00
parent 128a243133
commit de738b7801
3 changed files with 277 additions and 115 deletions

View File

@@ -10,11 +10,12 @@ namespace Business.Business.Sales
{ {
class ProductController : IController<Product> class ProductController : IController<Product>
{ {
private LuminousContext context = new LuminousContext(); private LuminousContext context;
private User currentUser; private User currentUser;
public ProductController(User currenUser) public ProductController(User currenUser)
{ {
this.currentUser = currenUser; this.currentUser = currenUser;
context = new LuminousContext();
} }
public ICollection<Product> GetAll() public ICollection<Product> GetAll()
{ {

View File

@@ -10,23 +10,56 @@ namespace Business.Business.UserManagment.Controllers
{ {
class RoleController : IReadOnlyController<Role> class RoleController : IReadOnlyController<Role>
{ {
private LuminousContext context = new LuminousContext(); private LuminousContext context;
private User currentUser;
public RoleController(User currentUser)
{
this.context = new LuminousContext();
this.currentUser = currentUser;
}
public ICollection<Role> GetAll() public ICollection<Role> GetAll()
{ {
return context.Role.ToList(); if (currentUser != null || currentUser.RoleId == 3)
{
return context.Role.ToList();
}
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public Role Get(int id) public Role Get(int id)
{ {
return context.Role.Find(id); if (currentUser != null || currentUser.RoleId == 3)
{
return context.Role.Find(id);
}
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public Role Get(string name) public Role Get(string name)
{ {
return context.Role.FirstOrDefault(u => u.Name == name); if (currentUser != null || currentUser.RoleId == 3)
{
return context.Role.FirstOrDefault(u => u.Name == name);
}
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public ICollection<Role> GetByApproximateName(string name) public ICollection<Role> GetByApproximateName(string name)
{ {
return context.Role.Where(u => u.Name.Contains(name)).ToList(); if (currentUser != null || currentUser.RoleId == 3)
{
return context.Role.Where(u => u.Name.Contains(name)).ToList();
}
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
} }
} }

View File

@@ -9,26 +9,58 @@ namespace Business.Business.UserManagment
{ {
public class UserController : IController<User> public class UserController : IController<User>
{ {
private LuminousContext context = new LuminousContext(); private LuminousContext context;
private RoleController rolectrl = new RoleController(); private RoleController rolectrl;
private User currentUser;
public UserController()
{
this.context = new LuminousContext();
}
public UserController(User currentUser)
{
this.currentUser = currentUser;
this.context = new LuminousContext();
this.rolectrl = new RoleController(currentUser);
}
public ICollection<User> GetAll() public ICollection<User> GetAll()
{ {
return context.User.ToList(); if (currentUser != null || currentUser.RoleId == 3)
{
return context.User.ToList();
}
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public void CheckIfUserEverCreated() public void CheckIfUserEverCreated()
{ {
if (!GetAll().Any()) if (!context.User.ToList().Any())
{ {
throw new ArgumentException("No users in the database!"); throw new ArgumentException("No users in the database!");
} }
} }
public User Get(int id) public User Get(int id)
{ {
return context.User.Find(id); if (currentUser != null || currentUser.RoleId == 3)
{
return context.User.Find(id);
}
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public User Get(string name) public User Get(string name)
{ {
return context.User.FirstOrDefault(u => u.Name == name); if (currentUser != null || currentUser.RoleId == 3)
{
return context.User.FirstOrDefault(u => u.Name == name);
}
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public User ValidatePassword(string password) public User ValidatePassword(string password)
{ {
@@ -41,247 +73,343 @@ namespace Business.Business.UserManagment
} }
public ICollection<User> GetByApproximateName(string name) public ICollection<User> GetByApproximateName(string name)
{ {
return context.User.Where(u => u.Name.Contains(name)).ToList(); if (currentUser != null || currentUser.RoleId == 3)
}
public void RegisterItem(string name, string password, int roleId)
{
if (GetAll().Where(u => u.Name == name).Any())
{ {
throw new ArgumentException("The username is already taken!"); return context.User.Where(u => u.Name.Contains(name)).ToList();
}
else if (GetAll().Where(u => u.Password == password).Any())
{
throw new ArgumentException("The password is already taken"!);
} }
else else
{ {
var foundRole = rolectrl.Get(roleId); throw new ArgumentException("Insufficient Role!");
if (foundRole != null) }
}
public void RegisterItem(string name, string password)
{
var user = new User(name, password, 1);
context.User.Add(user);
context.SaveChanges();
}
public void RegisterItem(string name, string password, int roleId)
{
if (currentUser != null || currentUser.RoleId == 3)
{
if (GetAll().Where(u => u.Name == name).Any())
{ {
var user = new User(name, password, roleId); throw new ArgumentException("The username is already taken!");
context.User.Add(user); }
context.SaveChanges(); else if (GetAll().Where(u => u.Password == password).Any())
{
throw new ArgumentException("The password is already taken"!);
} }
else else
{ {
throw new ArgumentException("Role not found!"); var foundRole = rolectrl.Get(roleId);
if (foundRole != null)
{
var user = new User(name, password, roleId);
context.User.Add(user);
context.SaveChanges();
}
else
{
throw new ArgumentException("Role not found!");
}
} }
} }
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public void RegisterItem(string name, string password, string roleName) public void RegisterItem(string name, string password, string roleName)
{ {
if (GetAll().Where(u => u.Name == name).Any()) if (currentUser != null || currentUser.RoleId == 3)
{ {
throw new ArgumentException("The username is already taken!"); if (GetAll().Where(u => u.Name == name).Any())
}
else if (GetAll().Where(u => u.Password == password).Any())
{
throw new ArgumentException("The password is already taken"!);
}
else
{
var foundRole = rolectrl.Get(roleName);
if (foundRole != null)
{ {
var user = new User(name, password, foundRole.Id); throw new ArgumentException("The username is already taken!");
context.User.Add(user); }
context.SaveChanges(); else if (GetAll().Where(u => u.Password == password).Any())
{
throw new ArgumentException("The password is already taken"!);
} }
else else
{ {
throw new ArgumentException("Role not found!"); var foundRole = rolectrl.Get(roleName);
if (foundRole != null)
{
var user = new User(name, password, foundRole.Id);
context.User.Add(user);
context.SaveChanges();
}
else
{
throw new ArgumentException("Role not found!");
}
} }
} }
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public void UpdateName(int id, string newName) public void UpdateName(int id, string newName)
{ {
var user = Get(id); if (currentUser != null || currentUser.Id == 3)
if (user != null)
{ {
if (user.Name != newName) var user = Get(id);
if (user != null)
{ {
user.Name = newName; if (user.Name != newName)
context.SaveChanges(); {
user.Name = newName;
context.SaveChanges();
}
else
{
throw new ArgumentException("Usernames match. Please choose another username!");
}
} }
else else
{ {
throw new ArgumentException("Usernames match. Please choose another username!"); throw new ArgumentException("No user with such id");
} }
} }
else else
{ {
throw new ArgumentException("No user with such id"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void UpdateName(string oldName, string newName) public void UpdateName(string oldName, string newName)
{ {
if (oldName != newName) if (currentUser != null || currentUser.RoleId == 3)
{ {
var user = Get(oldName); if (oldName != newName)
if (user != null)
{ {
user.Name = newName; var user = Get(oldName);
context.SaveChanges(); if (user != null)
{
user.Name = newName;
context.SaveChanges();
}
else
{
throw new ArgumentException("No user with such name!");
}
} }
else else
{ {
throw new ArgumentException("No user with such name!"); throw new ArgumentException("Usernames match. Please use another username!");
} }
} }
else else
{ {
throw new ArgumentException("Usernames match. Please use another username!"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void UpdatePassword(int id, string newPassword) public void UpdatePassword(int id, string newPassword)
{ {
var user = Get(id); if (currentUser != null || currentUser.RoleId == 3)
if (user != null)
{ {
if (user.Password != newPassword) var user = Get(id);
if (user != null)
{ {
user.Password = newPassword; if (user.Password != newPassword)
context.SaveChanges(); {
user.Password = newPassword;
context.SaveChanges();
}
else
{
throw new ArgumentException("Passwords match! Please use another password!");
}
} }
else else
{ {
throw new ArgumentException("Passwords match! Please use another password!"); throw new ArgumentException("User not found");
} }
} }
else else
{ {
throw new ArgumentException("User not found"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void UpdatePassword(string name, string newPassword) public void UpdatePassword(string name, string newPassword)
{ {
var user = Get(name); if (currentUser != null || currentUser.RoleId == 3)
if (user != null)
{ {
if (user.Password != newPassword) var user = Get(name);
if (user != null)
{ {
user.Password = newPassword; if (user.Password != newPassword)
context.SaveChanges(); {
user.Password = newPassword;
context.SaveChanges();
}
else
{
throw new ArgumentException("Passwords match! Please use another password!");
}
} }
else else
{ {
throw new ArgumentException("Passwords match! Please use another password!"); throw new ArgumentException("User not found");
} }
} }
else else
{ {
throw new ArgumentException("User not found"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void UpdateRole(int id, int RoleId) public void UpdateRole(int id, int RoleId)
{ {
var user = Get(id); if (currentUser != null || currentUser.RoleId == 3)
if (user != null)
{ {
var foundRole = rolectrl.Get(RoleId); var user = Get(id);
if (foundRole != null) if (user != null)
{ {
user.RoleId = RoleId; var foundRole = rolectrl.Get(RoleId);
context.SaveChanges(); if (foundRole != null)
{
user.RoleId = RoleId;
context.SaveChanges();
}
else
{
throw new ArgumentException("Role not found!");
}
} }
else else
{ {
throw new ArgumentException("Role not found!"); throw new ArgumentException("User not found");
} }
} }
else else
{ {
throw new ArgumentException("User not found"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void UpdateRole(int id, string roleName) public void UpdateRole(int id, string roleName)
{ {
var user = Get(id); if (currentUser != null || currentUser.RoleId == 3)
if (user != null)
{ {
var foundRole = rolectrl.Get(roleName); var user = Get(id);
if (foundRole != null) if (user != null)
{ {
user.RoleId = foundRole.Id; var foundRole = rolectrl.Get(roleName);
context.SaveChanges(); if (foundRole != null)
{
user.RoleId = foundRole.Id;
context.SaveChanges();
}
else
{
throw new ArgumentException("Role not found!");
}
} }
else else
{ {
throw new ArgumentException("Role not found!"); throw new ArgumentException("User not found");
} }
} }
else else
{ {
throw new ArgumentException("User not found"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void UpdateRole(string name, int roleId) public void UpdateRole(string name, int roleId)
{ {
var user = Get(name); if (currentUser != null || currentUser.RoleId == 3)
if (user != null)
{ {
var foundRole = rolectrl.Get(roleId); var user = Get(name);
if (foundRole != null) if (user != null)
{ {
user.RoleId = roleId; var foundRole = rolectrl.Get(roleId);
context.SaveChanges(); if (foundRole != null)
{
user.RoleId = roleId;
context.SaveChanges();
}
else
{
throw new ArgumentException("Role not found!");
}
} }
else else
{ {
throw new ArgumentException("Role not found!"); throw new ArgumentException("User not found");
} }
} }
else else
{ {
throw new ArgumentException("User not found"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void UpdateRole(string name, string roleName) public void UpdateRole(string name, string roleName)
{ {
var user = Get(name); if (currentUser != null || currentUser.RoleId == 3)
if (user != null)
{ {
var foundRole = rolectrl.Get(roleName); var user = Get(name);
if (foundRole != null) if (user != null)
{ {
user.RoleId = foundRole.Id; var foundRole = rolectrl.Get(roleName);
context.SaveChanges(); if (foundRole != null)
{
user.RoleId = foundRole.Id;
context.SaveChanges();
}
else
{
throw new ArgumentException("Role not found!");
}
} }
else else
{ {
throw new ArgumentException("Role not found!"); throw new ArgumentException("User not found");
} }
} }
else else
{ {
throw new ArgumentException("User not found"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void Delete(int id) public void Delete(int id)
{ {
var user = Get(id); if (currentUser != null || currentUser.RoleId == 3)
if (user != null)
{ {
context.User.Remove(user); var user = Get(id);
context.SaveChanges(); if (user != null)
{
context.User.Remove(user);
context.SaveChanges();
}
else
{
throw new ArgumentException("User not found");
}
} }
else else
{ {
throw new ArgumentException("User not found"); throw new ArgumentException("Insufficient Role!");
} }
} }
public void Delete(string name) public void Delete(string name)
{ {
var user = Get(name); if (currentUser != null || currentUser.RoleId == 3)
if (user != null)
{ {
context.User.Remove(user); var user = Get(name);
context.SaveChanges(); if (user != null)
{
context.User.Remove(user);
context.SaveChanges();
}
else
{
throw new ArgumentException("User not found");
}
} }
else else
{ {
throw new ArgumentException("User not found"); throw new ArgumentException("Insufficient Role!");
} }
} }
} }