public async Task HandleAsync(UserUpdateGeneralCommand command) { var user = await _context .User .Include(t => t.UserPrivilege) .FirstOrDefaultAsync(x => x.Id == command.UserId); var currentGeneralPrivileges = user.UserPrivilege.Select(x => x.PrivilegeId); var res = currentGeneralPrivileges.Union(command.Privileges).Except(currentGeneralPrivileges.Intersect(command.Privileges)).ToList(); res.ForEach(x => { if (currentGeneralPrivileges.Contains(x)) { var privileges = _context.UserPrivilege.Where(y => y.PrivilegeId == x && y.UserId == command.UserId); _context.UserPrivilege.RemoveRange(privileges); } else { _context.UserPrivilege.Add(new UserPrivilege() { PrivilegeId = x, UserId = command.UserId }); } }); await _context.SaveChangesAsync(); }
public async Task <IActionResult> UpdateGeneral(UserUpdateGeneralCommand command) { await _commandBus.ExecuteAsync(command); return(Ok()); }