* Maintain DataModel for User, Role, Function and UserRole

This commit is contained in:
2025-02-20 05:20:30 +01:00
parent c2078d1614
commit cd5990039d
21 changed files with 740 additions and 1 deletions

View 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();
}
}