using System; using System.Threading.Tasks; using DevHive.Data.Interfaces; using DevHive.Data.Models; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class RoleRepository : BaseRepository, IRoleRepository { private readonly RoleManager _roleManager; public RoleRepository(DevHiveContext context, RoleManager roleManager) : base(context) { this._roleManager = roleManager; } #region Create public override async Task AddAsync(Role entity) { IdentityResult result = await this._roleManager.CreateAsync(entity); return result.Succeeded; } #endregion #region Read public async Task GetByNameAsync(string name) { return await this._roleManager.FindByNameAsync(name); } #endregion public override async Task EditAsync(Guid id, Role newEntity) { newEntity.Id = id; IdentityResult result = await this._roleManager.UpdateAsync(newEntity); return result.Succeeded; } #region Validations public async Task DoesNameExist(string name) { return await this._roleManager.RoleExistsAsync(name); } public async Task DoesRoleExist(Guid id) { return await this._roleManager.Roles.AnyAsync(r => r.Id == id); } #endregion } }