using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Data.Models.Interfaces.Database; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class UserRepository : IRepository { private readonly DbContext _context; public UserRepository(DbContext context) { this._context = context; } //Create public async Task AddAsync(User entity) { await this._context .Set() .AddAsync(entity); await this._context.SaveChangesAsync(); } //Read public IEnumerable QueryAll() { return this._context .Set() .AsNoTracking() .AsEnumerable(); } public async Task GetByIdAsync(Guid id) { return await this._context .Set() .FindAsync(id); } public async Task GetByUsername(string username) { return await this._context .Set() .FirstOrDefaultAsync(x => x.UserName == username); } //Update public async Task EditAsync(User newEntity) { this._context .Set() .Update(newEntity); await this._context.SaveChangesAsync(); } //Delete public async Task DeleteAsync(User entity) { this._context .Set() .Remove(entity); await this._context.SaveChangesAsync(); } //Validations public bool DoesUserExist(Guid id) { return this._context .Set() .Any(x => x.Id == id); } public Task IsUsernameValid(string username) { return this._context .Set() .AnyAsync(u => u.UserName == username); } public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context .Set() .Any(x => x.Id == id && x.UserName == username); } public async Task DoesUsernameExist(string username) { return await this._context .Set() .AsNoTracking() .AnyAsync(u => u.UserName == username); } public async Task DoesEmailExist(string email) { return await this._context .Set() .AsNoTracking() .AnyAsync(u => u.Email == email); } } }