95 lines
3.2 KiB
C#
95 lines
3.2 KiB
C#
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>> GetAll()
|
|
{
|
|
IList<FunctionDto> functions = await context.Functions.Select(x => mapper.Map<FunctionDto>(x)).ToListAsync();
|
|
|
|
return functions;
|
|
}
|
|
|
|
public async Task<FunctionDto?> GetById(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?> GetByName(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> Add(FunctionDto functionDto)
|
|
{
|
|
Function function = new Function
|
|
{
|
|
Name = functionDto.Name,
|
|
RowPointer = Guid.NewGuid()
|
|
};
|
|
|
|
context.Functions.Add(function);
|
|
return await context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<int> Delete(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> Update(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();
|
|
}
|
|
} |