CSR/CSR.Infrastructure/Persistence/Repositories/RoleRepository.cs

54 lines
1.2 KiB
C#

namespace Csr.Infrastructure.Persistence.Repositories;
using CSR.Domain.Entities;
using CSR.Application.Interfaces;
using Microsoft.EntityFrameworkCore;
public class RoleRepository(CSR.Infrastructure.Persistence.CSRDbContext context) : IRoleRepository
{
private readonly CSR.Infrastructure.Persistence.CSRDbContext _context = context;
public async Task<Role?> GetByIdAsync(int id)
{
var roleEntity = await _context.Roles
.Include(r => r.Id)
.SingleOrDefaultAsync(r => r.Id == id);
if (roleEntity == null)
{
return null; // No entity found, return null domain model
}
var role = Role.LoadExisting(
roleEntity.Id,
roleEntity.Name
);
return role;
}
public async Task AddAsync(Role role)
{
var roleEntity = new CSR.Infrastructure.Persistence.Role
{
Name = role.Name
};
_context.Roles.Add(roleEntity);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var roleEntity = new CSR.Infrastructure.Persistence.Role
{
Id = id,
Name = string.Empty
};
_context.Roles.Remove(roleEntity);
await _context.SaveChangesAsync();
}
}