using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class LanguageRepository : ILanguageRepository { private readonly DevHiveContext _context; public LanguageRepository(DevHiveContext context) { this._context = context; } #region Create public async Task AddAsync(Language entity) { await this._context .Set() .AddAsync(entity); return await RepositoryMethods.SaveChangesAsync(this._context); } #endregion #region Read public async Task GetByIdAsync(Guid id) { return await this._context .Set() .FindAsync(id); } public async Task GetByNameAsync(string languageName) { return await this._context.Languages .FirstOrDefaultAsync(x => x.Name == languageName); } #endregion #region Update public async Task EditAsync(Language newEntity) { this._context .Set() .Update(newEntity); return await RepositoryMethods.SaveChangesAsync(this._context); } #endregion #region Delete public async Task DeleteAsync(Language entity) { this._context .Set() .Remove(entity); return await RepositoryMethods.SaveChangesAsync(this._context); } #endregion #region Validations public async Task DoesLanguageNameExistAsync(string languageName) { return await this._context.Languages .AnyAsync(r => r.Name == languageName); } public async Task DoesLanguageExistAsync(Guid id) { return await this._context.Languages .AnyAsync(r => r.Id == id); } #endregion } }