using System; using System.Threading.Tasks; using Data.Models.Interfaces.Database; using DevHive.Common.Models.Data; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class RoleRepository : IRepository { private readonly DevHiveContext _context; public RoleRepository(DevHiveContext context) { this._context = context; } //Create public async Task AddAsync(Role entity) { await this._context .Set() .AddAsync(entity); return await RepositoryMethods.SaveChangesAsync(this._context); } //Read public async Task GetByIdAsync(Guid id) { return await this._context .Set() .FindAsync(id); } public async Task GetByNameAsync(string name) { return await this._context .Set() .FirstOrDefaultAsync(x => x.Name == name); } //Update public async Task EditAsync(Role newEntity) { Role role = await this.GetByIdAsync(newEntity.Id); this._context .Entry(role) .CurrentValues .SetValues(newEntity); return await RepositoryMethods.SaveChangesAsync(this._context); } //Delete public async Task DeleteAsync(Role entity) { this._context .Set() .Remove(entity); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task DoesNameExist(string name) { return await this._context .Set() .AsNoTracking() .AnyAsync(r => r.Name == name); } public async Task DoesRoleExist(Guid id) { return await this._context .Set() .AsNoTracking() .AnyAsync(r => r.Id == id); } } }