Controllers Update
This commit is contained in:
@@ -10,11 +10,12 @@ namespace Business.Business.Sales
|
||||
{
|
||||
class ProductController : IController<Product>
|
||||
{
|
||||
private LuminousContext context = new LuminousContext();
|
||||
private LuminousContext context;
|
||||
private User currentUser;
|
||||
public ProductController(User currenUser)
|
||||
{
|
||||
this.currentUser = currenUser;
|
||||
context = new LuminousContext();
|
||||
}
|
||||
public ICollection<Product> GetAll()
|
||||
{
|
||||
|
||||
@@ -10,23 +10,56 @@ namespace Business.Business.UserManagment.Controllers
|
||||
{
|
||||
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()
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
return context.Role.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public Role Get(int id)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
return context.Role.Find(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public Role Get(string 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)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
return context.Role.Where(u => u.Name.Contains(name)).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,27 +9,59 @@ namespace Business.Business.UserManagment
|
||||
{
|
||||
public class UserController : IController<User>
|
||||
{
|
||||
private LuminousContext context = new LuminousContext();
|
||||
private RoleController rolectrl = new RoleController();
|
||||
private LuminousContext context;
|
||||
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()
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
return context.User.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void CheckIfUserEverCreated()
|
||||
{
|
||||
if (!GetAll().Any())
|
||||
if (!context.User.ToList().Any())
|
||||
{
|
||||
throw new ArgumentException("No users in the database!");
|
||||
}
|
||||
}
|
||||
public User Get(int id)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
return context.User.Find(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public User Get(string 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)
|
||||
{
|
||||
var user = context.User.FirstOrDefault();
|
||||
@@ -40,10 +72,25 @@ namespace Business.Business.UserManagment
|
||||
return user;
|
||||
}
|
||||
public ICollection<User> 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!");
|
||||
}
|
||||
}
|
||||
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())
|
||||
{
|
||||
@@ -68,7 +115,14 @@ namespace Business.Business.UserManagment
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void RegisterItem(string name, string password, string roleName)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
if (GetAll().Where(u => u.Name == name).Any())
|
||||
{
|
||||
@@ -93,7 +147,14 @@ namespace Business.Business.UserManagment
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void UpdateName(int id, string newName)
|
||||
{
|
||||
if (currentUser != null || currentUser.Id == 3)
|
||||
{
|
||||
var user = Get(id);
|
||||
if (user != null)
|
||||
@@ -113,7 +174,14 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("No user with such id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void UpdateName(string oldName, string newName)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
if (oldName != newName)
|
||||
{
|
||||
@@ -132,9 +200,15 @@ namespace Business.Business.UserManagment
|
||||
{
|
||||
throw new ArgumentException("Usernames match. Please use another username!");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void UpdatePassword(int id, string newPassword)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
var user = Get(id);
|
||||
if (user != null)
|
||||
@@ -154,7 +228,14 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void UpdatePassword(string name, string newPassword)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
var user = Get(name);
|
||||
if (user != null)
|
||||
@@ -174,7 +255,14 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void UpdateRole(int id, int RoleId)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
var user = Get(id);
|
||||
if (user != null)
|
||||
@@ -195,7 +283,14 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void UpdateRole(int id, string roleName)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
var user = Get(id);
|
||||
if (user != null)
|
||||
@@ -216,7 +311,14 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void UpdateRole(string name, int roleId)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
var user = Get(name);
|
||||
if (user != null)
|
||||
@@ -237,7 +339,14 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void UpdateRole(string name, string roleName)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
var user = Get(name);
|
||||
if (user != null)
|
||||
@@ -258,7 +367,14 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void Delete(int id)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
var user = Get(id);
|
||||
if (user != null)
|
||||
@@ -271,7 +387,14 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
public void Delete(string name)
|
||||
{
|
||||
if (currentUser != null || currentUser.RoleId == 3)
|
||||
{
|
||||
var user = Get(name);
|
||||
if (user != null)
|
||||
@@ -284,5 +407,10 @@ namespace Business.Business.UserManagment
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient Role!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user