public async Task <bool> UpdateSchedulerAsync(Scheduler request) { try { var schedulerResponse = await _schedulerRepository.SingleOrDefault(x => x.Id == request.Id); if (schedulerResponse != null) { var toInsert = request.RoomSchedulers.Where(x => !schedulerResponse.RoomSchedulers.Any(y => y.IdRoom == x.IdRoom)); var toRemove = schedulerResponse.RoomSchedulers.Where(x => !request.RoomSchedulers.Any(y => y.IdRoom == x.IdRoom)) .Select(x => new RoomScheduler { IdRoom = x.IdRoom, IdScheduler = x.IdScheduler }); await _schedulerRepository.DetachLocal(x => x.Id == request.Id); await _schedulerRepository.UpdateAsync(request); await _roomSchedulerRepository.AddRangeAsync(toInsert); await _roomSchedulerRepository.RemoveRange(toRemove); return(await _schedulerRepository.CommitAsync()); } else { await _mediator.Publish(new Notification("SchedulerService", $"Não foi possível localizar a agenda informada.")); } } catch (Exception ex) { await _mediator.Publish(new Notification("SchedulerService_Exception", $"Ocorreu um erro ao tentar alterar o agendamento.")); } return(false); }