54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
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>> GetAll()
|
|
{
|
|
IList<RoleDto> roles = await context.Roles.Select(x => mapper.Map<RoleDto>(x)).ToListAsync();
|
|
|
|
return roles;
|
|
}
|
|
|
|
public async Task<RoleDto?> GetById(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?> GetByName(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> Add(RoleDto roleDto)
|
|
{
|
|
Role role = new Role
|
|
{
|
|
Name = roleDto.Name,
|
|
RowPointer = Guid.NewGuid()
|
|
};
|
|
|
|
context.Roles.Add(role);
|
|
return await context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<int> Delete(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();
|
|
}
|
|
} |