using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading.Tasks; using AutoMapper.Mappers; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Data.RelationModels; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class UserRepository : BaseRepository, IUserRepository { private readonly DevHiveContext _context; public UserRepository(DevHiveContext context) : base(context) { this._context = context; } #region Read public IEnumerable QueryAll() { return this._context.Users .Include(x => x.Roles) .AsNoTracking() .AsEnumerable(); } public override async Task GetByIdAsync(Guid id) { return await this._context.Users .Include(x => x.Friends) .Include(x => x.Roles) .Include(x => x.Languages) .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.Id == id); } public async Task GetByUsernameAsync(string username) { return await this._context.Users .Include(x => x.Friends) .Include(x => x.Roles) .Include(x => x.Languages) .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } #endregion #region Update public override async Task EditAsync(Guid id, User newEntity) { User user = await this.GetByIdAsync(id); this._context .Entry(user) .CurrentValues .SetValues(newEntity); user.Languages.Clear(); foreach (var lang in newEntity.Languages) user.Languages.Add(lang); user.Roles.Clear(); foreach (var role in newEntity.Roles) user.Roles.Add(role); // foreach (var friend in user.Friends) // { // friend.Friends.Remove(user); // this._context.Entry(friend).State = EntityState.Modified; // } // user.Friends.Clear(); // foreach (var friend in newEntity.Friends) // { // friend.Friends.Add(user); // user.Friends.Add(friend); // } this._context.UserFriends.RemoveRange( this._context.UserFriends .Where(x => x.FriendId == user.Id && x.UserId == user.Id)); user.Technologies.Clear(); foreach (var tech in newEntity.Technologies) user.Technologies.Add(tech); this._context.Entry(user).State = EntityState.Modified; return await this.SaveChangesAsync(this._context); } #endregion #region Validations public async Task DoesUserExistAsync(Guid id) { return await this._context.Users .AsNoTracking() .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.GetByIdAsync(userId); User friend = await this.GetByIdAsync(friendId); return user.Friends.Any(x => x.Friend.Id == friendId); } public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users .AsNoTracking() .Any(x => x.Id == id && x.UserName == username); } #endregion } }