Files
FA_WEB/OrdersManagementDataModel/Services/RoleService.cs
Piotr Kus 9b8d621f42 * Managed API to handle User maintenance
* Managed Users view in application
2025-02-20 06:39:06 +01:00

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