2024-07-10 09:00:36 +02:00

62 lines
1.6 KiB
C#

using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Project.Domain.Entities;
using Project.Infrastructure.Interfaces;
namespace Project.Infrastructure.Repositories
{
public class RoleRepository : IRoleRepository
{
// FIELDS FOR CTOR
private readonly ApplicationDbContext _context;
// CTOR
public RoleRepository(ApplicationDbContext context)
{
_context = context;
}
// CREATE
public async Task<Role?> AddAsync(Role role)
{
await _context.Roles.AddAsync(role);
await _context.SaveChangesAsync();
return role;
}
// READ ALL
public async Task<IEnumerable<Role>> GetAllAsync()
{
return await _context.Roles.ToListAsync();
}
// READ BY ID
public async Task<Role?> GetByIdAsync(int id)
{
return await _context.Roles.FindAsync(id);
}
// READ BY NAME
public async Task<Role?> GetByNameAsync(string name)
{
return await _context.Roles.FirstOrDefaultAsync(n => n.Name == name);
}
// UPDATE
public async Task<bool> UpdateAsync(Role role)
{
_context.Entry(role).State = EntityState.Modified;
var results = await _context.SaveChangesAsync();
return results > 0;
}
// DELETE
public async Task<bool> DeleteAsync(Role role)
{
_context.Roles.Remove(role);
var result = await _context.SaveChangesAsync();
return result > 0;
}
}
}