public ActionResult Create(AgendaModel model) { try { var agenda = Mapper.Map<Agenda>(model); var list = _servicoDeHorario.Adicionar(agenda.Horario); var erro = 0; foreach (var agendaAdd in list.Select(horario => new Agenda { ComputadorId = agenda.ComputadorId, AlunoId = agenda.AlunoId, HorarioId = horario.Id })) { if (_servicoDeAgenda.VerificarSePodeAgendar(agendaAdd)) { _servicoDeAgenda.Adicionar(agendaAdd); } else { erro++; } } if (erro < list.Count) return RedirectToAction("Index"); InitSelectsCreate(); ViewBag.Erro = "Esse computador já está agendado para este horário"; return View(); } catch { InitSelectsCreate(); return View(); } }
public ActionResult Delete(AgendaModel model) { try { var agenda = _servicoDeAgenda.GetById(model.Id); var result = _servicoDeAgenda.Excluir(agenda); if (!result) return RedirectToAction("Index"); result = _servicoDeAgenda.Get(x => x.HorarioId == agenda.HorarioId).Any(); if (!result) { _servicoDeHorario.Excluir(agenda.HorarioId); } return RedirectToAction("Index"); } catch { return View(model); } }