* Maintain DataModel for User, Role, Function and UserRole
This commit is contained in:
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user