* Maintain DataModel for User, Role, Function and UserRole
This commit is contained in:
95
OrdersManagementDataModel/Services/FunctionService.cs
Normal file
95
OrdersManagementDataModel/Services/FunctionService.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
using OrdersManagementDataModel.Entities;
|
||||
|
||||
namespace OrdersManagementDataModel.Services;
|
||||
|
||||
public class FunctionService(OrdersManagementDbContext context, IMapper mapper) : IFunctionService
|
||||
{
|
||||
public async Task<IEnumerable<FunctionDto>> GetFunctions()
|
||||
{
|
||||
IList<FunctionDto> functions = await context.Functions.Select(x => mapper.Map<FunctionDto>(x)).ToListAsync();
|
||||
|
||||
return functions;
|
||||
}
|
||||
|
||||
public async Task<FunctionDto?> GetFunctionById(Guid id)
|
||||
{
|
||||
FunctionDto? function = await context.Functions.Where(x => x.RowPointer == id)
|
||||
.Select(x => mapper.Map<FunctionDto>(x)).FirstOrDefaultAsync();
|
||||
|
||||
return function;
|
||||
}
|
||||
|
||||
public async Task<FunctionDto?> GetFunctionByName(string name)
|
||||
{
|
||||
FunctionDto? function = await context.Functions.Where(x => x.Name == name)
|
||||
.Select(x => mapper.Map<FunctionDto>(x)).FirstOrDefaultAsync();
|
||||
|
||||
return function;
|
||||
}
|
||||
|
||||
public async Task<int> AddFunction(FunctionDto functionDto)
|
||||
{
|
||||
Function function = new Function
|
||||
{
|
||||
Name = functionDto.Name,
|
||||
RowPointer = Guid.NewGuid()
|
||||
};
|
||||
|
||||
context.Functions.Add(function);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> DeleteFunction(Guid id)
|
||||
{
|
||||
Function? function = await context.Functions.Where(x => x.RowPointer == id).FirstOrDefaultAsync() ?? null;
|
||||
|
||||
if (function == null) return 0;
|
||||
|
||||
context.Functions.Remove(function);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> UpdateFunction(FunctionDto functionDto)
|
||||
{
|
||||
Function function = mapper.Map<Function>(functionDto);
|
||||
context.Functions.Update(function);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<FunctionDto>> GetFunctionsByRoleId(Guid roleId)
|
||||
{
|
||||
Role? role = await context.Roles.FirstOrDefaultAsync(x => x.RowPointer == roleId);
|
||||
|
||||
IList<FunctionDto> functions = await context.Functions.Where(x => role != null && x.RoleId == role.Id)
|
||||
.Select(x => mapper.Map<FunctionDto>(x)).ToListAsync();
|
||||
|
||||
return functions;
|
||||
}
|
||||
|
||||
public async Task<int> AddFunctionToRole(Guid roleId, Guid functionId)
|
||||
{
|
||||
Role? role = await context.Roles.FirstOrDefaultAsync(x => x.RowPointer == roleId);
|
||||
Function? function = await context.Functions.FirstOrDefaultAsync(x => x.RowPointer == functionId);
|
||||
|
||||
if (role == null || function == null) return 0;
|
||||
|
||||
function.RoleId = role.Id;
|
||||
context.Functions.Update(function);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> RemoveFunctionFromRole(Guid roleId, Guid functionId)
|
||||
{
|
||||
Role? role = await context.Roles.FirstOrDefaultAsync(x => x.RowPointer == roleId);
|
||||
Function? function = await context.Functions.FirstOrDefaultAsync(x => x.RowPointer == functionId);
|
||||
|
||||
if (role == null || function == null) return 0;
|
||||
|
||||
function.RoleId = 0;
|
||||
context.Functions.Update(function);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
16
OrdersManagementDataModel/Services/IFunctionService.cs
Normal file
16
OrdersManagementDataModel/Services/IFunctionService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagementDataModel.Services;
|
||||
|
||||
public interface IFunctionService
|
||||
{
|
||||
Task<IEnumerable<FunctionDto>> GetFunctions();
|
||||
Task<FunctionDto?> GetFunctionById(Guid id);
|
||||
Task<FunctionDto?> GetFunctionByName(string name);
|
||||
Task<int> AddFunction(FunctionDto functionDto);
|
||||
Task<int> DeleteFunction(Guid id);
|
||||
Task<int> UpdateFunction(FunctionDto functionDto);
|
||||
Task<IEnumerable<FunctionDto>> GetFunctionsByRoleId(Guid roleId);
|
||||
Task<int> AddFunctionToRole(Guid roleId, Guid functionId);
|
||||
Task<int> RemoveFunctionFromRole(Guid roleId, Guid functionId);
|
||||
}
|
||||
12
OrdersManagementDataModel/Services/IRoleService.cs
Normal file
12
OrdersManagementDataModel/Services/IRoleService.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagementDataModel.Services;
|
||||
|
||||
public interface IRoleService
|
||||
{
|
||||
Task<IEnumerable<RoleDto>> GetRoles();
|
||||
Task<RoleDto?> GetRoleById(Guid id);
|
||||
Task<RoleDto?> GetRoleByName(string name);
|
||||
Task<int> AddRole(RoleDto roleDto);
|
||||
Task<int> DeleteRole(Guid id);
|
||||
}
|
||||
15
OrdersManagementDataModel/Services/IUserRoleService.cs
Normal file
15
OrdersManagementDataModel/Services/IUserRoleService.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagementDataModel.Services;
|
||||
|
||||
public interface IUserRoleService
|
||||
{
|
||||
Task<IEnumerable<UserRoleDto>> GetUserRoles();
|
||||
Task<UserRoleDto?> GetUserRoleById(Guid id);
|
||||
Task<int> AddUserRole(UserRoleDto userRoleDto);
|
||||
Task<int> DeleteUserRole(Guid id);
|
||||
Task<int> UpdateUserRole(UserRoleDto userRoleDto);
|
||||
Task<IEnumerable<UserRoleDto>> GetUserRolesByUserId(Guid userId);
|
||||
Task<IEnumerable<UserRoleDto>> GetUserRolesByRoleId(Guid roleId);
|
||||
Task<int> AddRoleToUser(Guid userId, Guid roleId);
|
||||
}
|
||||
15
OrdersManagementDataModel/Services/IUserService.cs
Normal file
15
OrdersManagementDataModel/Services/IUserService.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagementDataModel.Services;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
Task<IEnumerable<UserDto>> GetUsers();
|
||||
Task<UserDto?> GetUserById(Guid id);
|
||||
Task<UserDto?> GetUserByUsername(string username);
|
||||
Task<int> AddUser(UserDto userDto);
|
||||
Task<int> UpdateUser(UserDto userDto);
|
||||
Task<int> DeleteUser(Guid id);
|
||||
Task<IList<UserRoleDto>> GetUserRoles(Guid userId);
|
||||
Task<UserDto?> GetUserByLoginAndPassword(string login, string password);
|
||||
}
|
||||
54
OrdersManagementDataModel/Services/RoleService.cs
Normal file
54
OrdersManagementDataModel/Services/RoleService.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
using OrdersManagementDataModel.Entities;
|
||||
|
||||
namespace OrdersManagementDataModel.Services;
|
||||
|
||||
public class RoleService(OrdersManagementDbContext context, IMapper mapper) : IRoleService
|
||||
{
|
||||
public async Task<IEnumerable<RoleDto>> GetRoles()
|
||||
{
|
||||
IList<RoleDto> roles = await context.Roles.Select(x => mapper.Map<RoleDto>(x)).ToListAsync();
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
public async Task<RoleDto?> GetRoleById(Guid id)
|
||||
{
|
||||
RoleDto? role = await context.Roles.Where(x => x.RowPointer == id)
|
||||
.Select(x => mapper.Map<RoleDto>(x)).FirstOrDefaultAsync();
|
||||
|
||||
return role;
|
||||
}
|
||||
|
||||
public async Task<RoleDto?> GetRoleByName(string name)
|
||||
{
|
||||
RoleDto? role = await context.Roles.Where(x => x.Name == name)
|
||||
.Select(x => mapper.Map<RoleDto>(x)).FirstOrDefaultAsync();
|
||||
|
||||
return role;
|
||||
}
|
||||
|
||||
public async Task<int> AddRole(RoleDto roleDto)
|
||||
{
|
||||
Role role = new Role
|
||||
{
|
||||
Name = roleDto.Name,
|
||||
RowPointer = Guid.NewGuid()
|
||||
};
|
||||
|
||||
context.Roles.Add(role);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> DeleteRole(Guid id)
|
||||
{
|
||||
Role? role = await context.Roles.Where(x => x.RowPointer == id).FirstOrDefaultAsync() ?? null;
|
||||
|
||||
if (role == null) return 0;
|
||||
|
||||
context.Roles.Remove(role);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
86
OrdersManagementDataModel/Services/UserRoleService.cs
Normal file
86
OrdersManagementDataModel/Services/UserRoleService.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
using OrdersManagementDataModel.Entities;
|
||||
|
||||
namespace OrdersManagementDataModel.Services;
|
||||
|
||||
public class UserRoleService(OrdersManagementDbContext context, IMapper mapper) : IUserRoleService
|
||||
{
|
||||
public async Task<IEnumerable<UserRoleDto>> GetUserRoles()
|
||||
{
|
||||
IList<UserRoleDto> userRoles = await context.UserRoles.Select(x => mapper.Map<UserRoleDto>(x)).ToListAsync();
|
||||
|
||||
return userRoles;
|
||||
}
|
||||
|
||||
public async Task<UserRoleDto?> GetUserRoleById(Guid id)
|
||||
{
|
||||
UserRoleDto? userRole = await context.UserRoles.Where(x => x.RowPointer == id)
|
||||
.Select(x => mapper.Map<UserRoleDto>(x)).FirstOrDefaultAsync();
|
||||
|
||||
return userRole;
|
||||
}
|
||||
|
||||
public Task<int> AddUserRole(UserRoleDto userRoleDto)
|
||||
{
|
||||
UserRole userRole = mapper.Map<UserRole>(userRoleDto);
|
||||
context.UserRoles.Add(userRole);
|
||||
return context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> DeleteUserRole(Guid id)
|
||||
{
|
||||
UserRole? userRole = await context.UserRoles.Where(x => x.RowPointer == id).FirstOrDefaultAsync() ?? null;
|
||||
|
||||
if (userRole == null) return 0;
|
||||
|
||||
context.UserRoles.Remove(userRole);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> UpdateUserRole(UserRoleDto userRoleDto)
|
||||
{
|
||||
UserRole userRole = mapper.Map<UserRole>(userRoleDto);
|
||||
context.UserRoles.Update(userRole);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UserRoleDto>> GetUserRolesByUserId(Guid userId)
|
||||
{
|
||||
IList<UserRoleDto> userRoles = await context.UserRoles
|
||||
.Where(x => x.User.RowPointer == userId)
|
||||
.Select(x => mapper.Map<UserRoleDto>(x))
|
||||
.ToListAsync();
|
||||
|
||||
return userRoles;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UserRoleDto>> GetUserRolesByRoleId(Guid roleId)
|
||||
{
|
||||
IList<UserRoleDto> userRoles = await context.UserRoles
|
||||
.Where(x => x.Role.RowPointer == roleId)
|
||||
.Select(x => mapper.Map<UserRoleDto>(x))
|
||||
.ToListAsync();
|
||||
|
||||
return userRoles;
|
||||
}
|
||||
|
||||
public async Task<int> AddRoleToUser(Guid userId, Guid roleId)
|
||||
{
|
||||
User? user = await context.Users.FirstOrDefaultAsync(x => x.RowPointer == userId);
|
||||
Role? role = await context.Roles.FirstOrDefaultAsync(x => x.RowPointer == roleId);
|
||||
|
||||
if (user == null || role == null) return 0;
|
||||
|
||||
UserRole userRole = new UserRole
|
||||
{
|
||||
User = user,
|
||||
Role = role,
|
||||
RowPointer = Guid.NewGuid()
|
||||
};
|
||||
|
||||
context.UserRoles.Add(userRole);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
88
OrdersManagementDataModel/Services/UserService.cs
Normal file
88
OrdersManagementDataModel/Services/UserService.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
using OrdersManagementDataModel.Entities;
|
||||
|
||||
namespace OrdersManagementDataModel.Services;
|
||||
|
||||
public class UserService(OrdersManagementDbContext context, IMapper mapper) : IUserService
|
||||
{
|
||||
public async Task<IEnumerable<UserDto>> GetUsers()
|
||||
{
|
||||
IList<UserDto> users = await context.Users.Select(x => mapper.Map<UserDto>(x)).ToListAsync();
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
public async Task<UserDto?> GetUserById(Guid id)
|
||||
{
|
||||
UserDto? user = await context.Users.Where(x => x.RowPointer == id)
|
||||
.Select(x => mapper.Map<UserDto>(x)).FirstOrDefaultAsync();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<UserDto?> GetUserByUsername(string username)
|
||||
{
|
||||
UserDto? user = await context.Users.Where(x => x.Login == username)
|
||||
.Select(x => mapper.Map<UserDto>(x)).FirstOrDefaultAsync();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<int> AddUser(UserDto userDto)
|
||||
{
|
||||
User user = new User
|
||||
{
|
||||
Login = userDto.Login,
|
||||
PasswordHash = userDto.PasswordHash,
|
||||
IsTemporaryPassword = true,
|
||||
IsActive = true,
|
||||
Email = userDto.Email,
|
||||
RowPointer = Guid.NewGuid(),
|
||||
CreatedDate = DateTime.Now,
|
||||
ActiveFrom = DateTime.Now,
|
||||
FirstName = userDto.FirstName,
|
||||
LastName = userDto.LastName
|
||||
};
|
||||
|
||||
context.Users.Add(user);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> UpdateUser(UserDto userDto)
|
||||
{
|
||||
User user = mapper.Map<User>(userDto);
|
||||
context.Users.Update(user);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> DeleteUser(Guid id)
|
||||
{
|
||||
User? user = await context.Users.Where(x => x.RowPointer == id).FirstOrDefaultAsync() ?? null;
|
||||
|
||||
if (user == null) return 0;
|
||||
|
||||
context.Users.Remove(user);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<IList<UserRoleDto>> GetUserRoles(Guid userId)
|
||||
{
|
||||
List<UserRoleDto> userRoles = await context.Users
|
||||
.Where(x => x.RowPointer == userId)
|
||||
.SelectMany(x => x.UserRoles)
|
||||
.Select(x => mapper.Map<UserRoleDto>(x))
|
||||
.ToListAsync();
|
||||
|
||||
return userRoles;
|
||||
}
|
||||
|
||||
public async Task<UserDto?> GetUserByLoginAndPassword(string login, string password)
|
||||
{
|
||||
UserDto? user = await context.Users.Where(x => x.Login == login && x.PasswordHash == password)
|
||||
.Select(x => mapper.Map<UserDto>(x)).FirstOrDefaultAsync();
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user