Create models and Clean Arch boilerplate
This commit is contained in:
parent
3926db5446
commit
872dc1e263
21 changed files with 576 additions and 27 deletions
|
@ -0,0 +1,55 @@
|
|||
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
|
||||
{
|
||||
Id = role.Id,
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue