From e632e9241e8afe530f6b37cb683b211769135c45 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 28 Jan 2021 01:08:31 +0200 Subject: USER UPDATE FIIIIIIXED --- src/DevHive.Data/Repositories/BaseRepository.cs | 11 ++++---- src/DevHive.Data/Repositories/UserRepository.cs | 37 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) (limited to 'src/DevHive.Data/Repositories') diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs index 0a97ac1..f1e6673 100644 --- a/src/DevHive.Data/Repositories/BaseRepository.cs +++ b/src/DevHive.Data/Repositories/BaseRepository.cs @@ -13,7 +13,6 @@ namespace DevHive.Data.Repositories public BaseRepository(DbContext context) { this._context = context; - this._context.ChangeTracker.AutoDetectChangesEnabled = false; } public virtual async Task AddAsync(TEntity entity) @@ -34,11 +33,11 @@ namespace DevHive.Data.Repositories public virtual async Task EditAsync(Guid id, TEntity newEntity) { - TEntity currEnt = await this.GetByIdAsync(id); - this._context - .Entry(currEnt) - .CurrentValues - .SetValues(newEntity); + var entry = this._context.Entry(newEntity); + if (entry.State == EntityState.Detached) + this._context.Attach(newEntity); + + entry.State = EntityState.Modified; return await this.SaveChangesAsync(_context); } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 57ae146..fe46226 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Threading.Tasks; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories @@ -48,6 +50,41 @@ namespace DevHive.Data.Repositories } #endregion + #region Update + public override async Task EditAsync(Guid id, User newEntity) + { + User user = await this.GetByIdAsync(id); + + 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); + } + + 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) { -- cgit v1.2.3