public bool Remove(int id) { var _hardwareSalaService = new HardwareDeSalaService(_context); var _minhaSalaService = new SalaParticularService(_context); var _horarioSalaService = new HorarioSalaService(_context); var _planejamentoService = new PlanejamentoService(_context); try { if (_hardwareSalaService.GetByIdSala(id).Count == 0 && _minhaSalaService.GetByIdSala(id).Count == 0 && _horarioSalaService.GetByIdSala(id).Count == 0 && _planejamentoService.GetByIdSala(id).Count == 0) { var x = _context.Sala.Where(s => s.Id == id).FirstOrDefault(); if (x != null) { _context.Remove(x); return(_context.SaveChanges() == 1); } } else { throw new ServiceException("Essa sala nao pode ser removida pois existem outros registros associados a ela!"); } } catch (Exception e) { throw e; } return(false); }
public bool Remove(int id) { var plan = new PlanejamentoService(_context); var particular = new SalaParticularService(_context); var horarios = new HorarioSalaService(_context); var usuarioOrg = new UsuarioOrganizacaoService(_context); var x = _context.Usuario.Where(u => u.Id == id).FirstOrDefault(); if (x != null) { //atualizar depois para escolher da qual org //removendo tudo associado ao usuario, pois na tela de remover irá aparecer se tem itens associados // se o usuario concordar, obviamente irá excluir tudo //planejamentos plan.RemoveByUsuario(x.Id); //salas particulares particular.RemoveByUsuario(x.Id); //reservas horarios.RemoveByUsuario(x.Id); // associacao usuarioOrg.RemoveByUsuario(x.Id); using (var transaction = _context.Database.BeginTransaction()) { try { _context.Remove(x); transaction.Commit(); return(_context.SaveChanges() == 1 ? true : false); } catch (Exception e) { transaction.Rollback(); throw e; } } } else { new ServiceException("Houve um erro ao remover o usuário!"); } return(false); }