using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class UserRepository : IRepository { private readonly DevHiveContext _context; public UserRepository(DevHiveContext context) { this._context = context; } #region Create public async Task AddAsync(User entity) { await this._context.Users .AddAsync(entity); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task AddFriendAsync(User user, User friend) { this._context.Update(user); user.Friends.Add(friend); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task AddLanguageToUserAsync(User user, Language language) { this._context.Update(user); user.Langauges.Add(language); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task AddTechnologyToUserAsync(User user, Technology technology) { this._context.Update(user); user.Technologies.Add(technology); return await RepositoryMethods.SaveChangesAsync(this._context); } #endregion #region Read public IEnumerable QueryAll() { return this._context.Users .Include(x => x.Roles) .AsNoTracking() .AsEnumerable(); } public async Task GetByIdAsync(Guid id) { return await this._context.Users .Include(x => x.Roles) .Include(x => x.Friends) .FirstOrDefaultAsync(x => x.Id == id); } public async Task GetByUsername(string username) { return await this._context.Users .Include(u => u.Roles) .FirstOrDefaultAsync(x => x.UserName == username); } public IList GetUserLanguages(User user) { return user.Langauges; } public Language GetUserLanguage(User user, Language language) { return user.Langauges .FirstOrDefault(x => x.Id == language.Id); } public IList GetUserTechnologies(User user) { return user.Technologies; } public Technology GetUserTechnology(User user, Technology technology) { return user.Technologies .FirstOrDefault(x => x.Id == technology.Id); } #endregion #region Update public async Task EditAsync(User newEntity) { User user = await this.GetByIdAsync(newEntity.Id); this._context .Entry(user) .CurrentValues .SetValues(newEntity); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task EditUserLanguage(User user, Language oldLang, Language newLang) { this._context.Update(user); user.Langauges.Remove(oldLang); user.Langauges.Add(newLang); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task EditUserTechnologies(User user, Technology oldTech, Technology newTech) { this._context.Update(user); user.Technologies.Remove(oldTech); user.Technologies.Add(newTech); return await RepositoryMethods.SaveChangesAsync(this._context); } #endregion #region Delete public async Task DeleteAsync(User entity) { this._context.Users .Remove(entity); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task RemoveFriendAsync(User user, User friend) { this._context.Update(user); user.Friends.Remove(friend); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task RemoveLanguageFromUserAsync(User user, Language language) { this._context.Update(user); user.Langauges.Remove(language); return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task RemoveTechnologyFromUserAsync(User user, Technology technology) { this._context.Update(user); user.Technologies.Remove(technology); return await RepositoryMethods.SaveChangesAsync(this._context); } #endregion #region Validations public async Task DoesUserExistAsync(Guid id) { return await this._context.Users .AnyAsync(x => x.Id == id); } public async Task DoesUsernameExistAsync(string username) { return await this._context.Users .AsNoTracking() .AnyAsync(u => u.UserName == username); } public async Task DoesEmailExistAsync(string email) { return await this._context.Users .AsNoTracking() .AnyAsync(u => u.Email == email); } public async Task DoesUserHaveThisFriendAsync(Guid userId, Guid friendId) { User user = await this._context.Users .FirstOrDefaultAsync(x => x.Id == userId); User friend = await this._context.Users .FirstOrDefaultAsync(x => x.Id == friendId); return user.Friends.Contains(friend); } public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users .Any(x => x.Id == id && x.UserName == username); } public bool DoesUserHaveFriends(User user) { return user.Friends.Count >= 1; } #endregion } }