Business update

This commit is contained in:
thermalthrottle
2021-03-18 14:57:00 +02:00
parent 7aac7f999f
commit 3c69124650
4 changed files with 156 additions and 24 deletions

View File

@@ -0,0 +1,19 @@
using Models.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace Business.Business.UserManagment
{
interface IController<T>
{
ICollection<T> GetAll();
T SearchById(int id);
ICollection<T> SearchByApproximateName(string name);
T SearchByExactName(string name);
void UpdateNameById(int id, string newName);
void UpdateNameByOldName(string oldName, string newName);
void DeleteById(int id);
void DeleteByName(string name);
}
}

View File

@@ -0,0 +1,131 @@
using Models;
using Models.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace Business.Business.UserManagment
{
public class UserController : IController<User>
{
private LuminousContext context = new LuminousContext();
public ICollection<User> GetAll()
{
return context.User.ToList();
}
public void DeleteById()
{
throw new NotImplementedException();
}
public void DeleteByName()
{
throw new NotImplementedException();
}
public User SearchById(int id)
{
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);
}
public void UpdateNameById(int id, string newName)
{
var user = SearchById(id);
if (user != null)
{
if (user.Name != newName)
{
user.Name = newName;
}
else
{
throw new ArgumentException("Usernames match. Please choose another username!");
}
}
else
{
throw new ArgumentException("No user with such id");
}
}
public void UpdateNameByOldName(string oldName, string newName)
{
if (oldName != newName)
{
var user = SearchByExactName(oldName);
if (user != null)
{
user.Name = newName;
}
else
{
throw new ArgumentException("No user with such name!");
}
}
else
{
throw new ArgumentException("Usernames match. Please use another username!");
}
}
public void UpdatePasswordById(int id, string newPassword)
{
var user = SearchById(id);
if (user != null)
{
if (user.Password != newPassword)
{
user.Password = newPassword;
}
else
{
throw new ArgumentException("Passwords match! Please use another password!");
}
}
else
{
throw new ArgumentException("User not found");
}
}
public void UpdatePasswordByName(string name, string newPassword)
{
var user = SearchByExactName(name);
if (user != null)
{
if (user.Password != newPassword)
{
user.Password = newPassword;
}
else
{
throw new ArgumentException("Passwords match! Please use another password!");
}
}
else
{
throw new ArgumentException("User not found");
}
}
public void DeleteById(int id)
{
throw new NotImplementedException();
}
public void DeleteByName(string name)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,24 +0,0 @@
using Models;
using Models.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace Business.Business.UserManagment
{
public class UserController
{
private LuminousContext context;
public void CreateUser(string Username, string Password, int RoleId)
{
using (context = new LuminousContext())
{
var user = new User(Username, Password, RoleId);
context.User.Add(user);
context.SaveChanges();
}
}
}
}

View File

@@ -0,0 +1,6 @@
namespace Business.Business.UserManagment
{
public interface IController
{
}
}