* 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user