Create models and Clean Arch boilerplate

This commit is contained in:
danial23 2025-05-19 16:13:58 -04:00
parent 3926db5446
commit 872dc1e263
Signed by: danial23
SSH key fingerprint: SHA256:IJ8VP0j2WMUVweTYnzUUnEjNgPnGx+mAt+RhqWZ01bU
21 changed files with 576 additions and 27 deletions

View file

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