User Controller updates
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using Models.Models;
|
using Models;
|
||||||
|
using Models.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -8,12 +9,12 @@ namespace Business.Business.UserManagment
|
|||||||
interface IController<T>
|
interface IController<T>
|
||||||
{
|
{
|
||||||
ICollection<T> GetAll();
|
ICollection<T> GetAll();
|
||||||
T SearchById(int id);
|
T Get(int id);
|
||||||
ICollection<T> SearchByApproximateName(string name);
|
T Get(string name);
|
||||||
T SearchByExactName(string name);
|
ICollection<T> GetByApproximateName(string name);
|
||||||
void UpdateNameById(int id, string newName);
|
void UpdateName(int id, string newName);
|
||||||
void UpdateNameByOldName(string oldName, string newName);
|
void UpdateName(string oldName, string newName);
|
||||||
void DeleteById(int id);
|
void Delete(int id);
|
||||||
void DeleteByName(string name);
|
void Delete(string name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Business.Business.Interfaces
|
||||||
|
{
|
||||||
|
interface IReadOnlyController<T>
|
||||||
|
{
|
||||||
|
ICollection<T> GetAll();
|
||||||
|
T Get(int id);
|
||||||
|
T Get(string name);
|
||||||
|
ICollection<T> GetByApproximateName(string name);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,32 @@
|
|||||||
|
using Business.Business.Interfaces;
|
||||||
|
using Models;
|
||||||
|
using Models.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Business.Business.UserManagment.Controllers
|
||||||
|
{
|
||||||
|
class RoleController : IReadOnlyController<Role>
|
||||||
|
{
|
||||||
|
private LuminousContext context = new LuminousContext();
|
||||||
|
|
||||||
|
public ICollection<Role> GetAll()
|
||||||
|
{
|
||||||
|
return context.Role.ToList();
|
||||||
|
}
|
||||||
|
public Role Get(int id)
|
||||||
|
{
|
||||||
|
return context.Role.Find(id);
|
||||||
|
}
|
||||||
|
public Role Get(string name)
|
||||||
|
{
|
||||||
|
return context.Role.FirstOrDefault(u => u.Name == name);
|
||||||
|
}
|
||||||
|
public ICollection<Role> GetByApproximateName(string name)
|
||||||
|
{
|
||||||
|
return context.Role.Where(u => u.Name.Contains(name)).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,52 +1,105 @@
|
|||||||
using Models;
|
using Business.Business.UserManagment.Controllers;
|
||||||
|
using Models;
|
||||||
using Models.Models;
|
using Models.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Business.Business.UserManagment
|
namespace Business.Business.UserManagment
|
||||||
{
|
{
|
||||||
public class UserController : IController<User>
|
public class UserController : IController<User>
|
||||||
{
|
{
|
||||||
private LuminousContext context = new LuminousContext();
|
private LuminousContext context = new LuminousContext();
|
||||||
|
private RoleController rolectrl = new RoleController();
|
||||||
public ICollection<User> GetAll()
|
public ICollection<User> GetAll()
|
||||||
{
|
{
|
||||||
return context.User.ToList();
|
return context.User.ToList();
|
||||||
}
|
}
|
||||||
public void DeleteById()
|
public void CheckIfUserEverCreated()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
if (!GetAll().Any())
|
||||||
|
{
|
||||||
|
throw new ArgumentException("No users in the database!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
public User Get(int id)
|
||||||
public void DeleteByName()
|
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
return context.User.Find(id);
|
||||||
}
|
}
|
||||||
public User SearchById(int id)
|
public User Get(string name)
|
||||||
{
|
|
||||||
return context.User.FirstOrDefault(u => u.Id == id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICollection<User> SearchByApproximateName(string name)
|
|
||||||
{
|
|
||||||
return context.User.Where(u => u.Name.Contains(name)).ToList();
|
|
||||||
}
|
|
||||||
public User SearchByExactName(string name)
|
|
||||||
{
|
{
|
||||||
return context.User.FirstOrDefault(u => u.Name == name);
|
return context.User.FirstOrDefault(u => u.Name == name);
|
||||||
}
|
}
|
||||||
|
public void ValidatePassword(string password)
|
||||||
public void UpdateNameById(int id, string newName)
|
|
||||||
{
|
{
|
||||||
var user = SearchById(id);
|
if (!GetAll().Where(u => u.Password == password).Any())
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid User!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ICollection<User> GetByApproximateName(string name)
|
||||||
|
{
|
||||||
|
return context.User.Where(u => u.Name.Contains(name)).ToList();
|
||||||
|
}
|
||||||
|
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!");
|
||||||
|
}
|
||||||
|
else if (GetAll().Where(u => u.Password == password).Any())
|
||||||
|
{
|
||||||
|
throw new ArgumentException("The password is already taken"!);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void RegisterItem(string name, string password, string roleName)
|
||||||
|
{
|
||||||
|
if (GetAll().Where(u => u.Name == name).Any())
|
||||||
|
{
|
||||||
|
throw new ArgumentException("The username is already taken!");
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
context.User.Add(user);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Role not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateName(int id, string newName)
|
||||||
|
{
|
||||||
|
var user = Get(id);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
if (user.Name != newName)
|
if (user.Name != newName)
|
||||||
{
|
{
|
||||||
user.Name = newName;
|
user.Name = newName;
|
||||||
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -58,15 +111,15 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("No user with such id");
|
throw new ArgumentException("No user with such id");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void UpdateName(string oldName, string newName)
|
||||||
public void UpdateNameByOldName(string oldName, string newName)
|
|
||||||
{
|
{
|
||||||
if (oldName != newName)
|
if (oldName != newName)
|
||||||
{
|
{
|
||||||
var user = SearchByExactName(oldName);
|
var user = Get(oldName);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
user.Name = newName;
|
user.Name = newName;
|
||||||
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -79,14 +132,15 @@ namespace Business.Business.UserManagment
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void UpdatePasswordById(int id, string newPassword)
|
public void UpdatePassword(int id, string newPassword)
|
||||||
{
|
{
|
||||||
var user = SearchById(id);
|
var user = Get(id);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
if (user.Password != newPassword)
|
if (user.Password != newPassword)
|
||||||
{
|
{
|
||||||
user.Password = newPassword;
|
user.Password = newPassword;
|
||||||
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -98,14 +152,15 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void UpdatePasswordByName(string name, string newPassword)
|
public void UpdatePassword(string name, string newPassword)
|
||||||
{
|
{
|
||||||
var user = SearchByExactName(name);
|
var user = Get(name);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
if (user.Password != newPassword)
|
if (user.Password != newPassword)
|
||||||
{
|
{
|
||||||
user.Password = newPassword;
|
user.Password = newPassword;
|
||||||
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -117,15 +172,115 @@ namespace Business.Business.UserManagment
|
|||||||
throw new ArgumentException("User not found");
|
throw new ArgumentException("User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void UpdateRole(int id, int RoleId)
|
||||||
public void DeleteById(int id)
|
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var user = Get(id);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
var foundRole = rolectrl.Get(RoleId);
|
||||||
|
if (foundRole != null)
|
||||||
|
{
|
||||||
|
user.RoleId = RoleId;
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Role not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("User not found");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
public void UpdateRole(int id, string roleName)
|
||||||
public void DeleteByName(string name)
|
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var user = Get(id);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
var foundRole = rolectrl.Get(roleName);
|
||||||
|
if (foundRole != null)
|
||||||
|
{
|
||||||
|
user.RoleId = foundRole.Id;
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Role not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("User not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateRole(string name, int roleId)
|
||||||
|
{
|
||||||
|
var user = Get(name);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
var foundRole = rolectrl.Get(roleId);
|
||||||
|
if (foundRole != null)
|
||||||
|
{
|
||||||
|
user.RoleId = roleId;
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Role not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("User not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateRole(string name, string roleName)
|
||||||
|
{
|
||||||
|
var user = Get(name);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
var foundRole = rolectrl.Get(roleName);
|
||||||
|
if (foundRole != null)
|
||||||
|
{
|
||||||
|
user.RoleId = foundRole.Id;
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Role not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("User not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void Delete(int id)
|
||||||
|
{
|
||||||
|
var user = Get(id);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
context.User.Remove(user);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("User not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void Delete(string name)
|
||||||
|
{
|
||||||
|
var user = Get(name);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
context.User.Remove(user);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("User not found");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -34,7 +34,7 @@ namespace Business.Business.UserManagment
|
|||||||
using (context = new LuminousContext())
|
using (context = new LuminousContext())
|
||||||
{
|
{
|
||||||
int roleToAttach = context.Role.FirstOrDefault(r => r.Name == "Admin").Id;
|
int roleToAttach = context.Role.FirstOrDefault(r => r.Name == "Admin").Id;
|
||||||
userctl.CreateUser(this.Username, this.Password, roleToAttach);
|
userctl.RegisterItem(this.Username, this.Password, roleToAttach);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +0,0 @@
|
|||||||
namespace Business.Business.UserManagment
|
|
||||||
{
|
|
||||||
public interface IController
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
@@ -10,13 +10,14 @@ namespace Display
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var val = new UserValidator();
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
val.CheckIfUserEverCreated();
|
var a = new UserController();
|
||||||
var InitialCreation = new CreateInitialUser("Admin", "pass123");
|
a.UpdateRole(1, "Cashier");
|
||||||
InitialCreation.CreateRoles();
|
foreach (var item in a.GetAll())
|
||||||
InitialCreation.CreateFirstUser();
|
{
|
||||||
|
Console.WriteLine($"{item.Name} {item.Role.Name}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user