public ActionResult Cadastrar(int idNoticia) { var model = new CadastrarNoticiaModel { AutoresListar = ListarAutores(), CategoriasListar = ListarCategorias() }; if (idNoticia > 0) { var noticia = _repository.ObterNoticiaPorId(idNoticia); if (noticia == null) { throw new Exception("Notícia não encontrada."); } model.Corpo = noticia.Corpo; model.Data = noticia.Data; model.FotoHome = noticia.FotoHome; model.Id = noticia.Id; model.IdAutor = noticia.IdAutor; model.IdCategoria = noticia.IdCategoria; model.Titulo = noticia.Titulo; } return(View(model)); }
public ActionResult SalvarNoticia(CadastrarNoticiaModel model) { if (model.Id > 0) { var noticia = _repository.ObterNoticiaPorId(model.Id); if (noticia == null) { throw new Exception("Notícia não encontrada."); } var categoria = _repository.ObterCategoriaPorId(model.IdCategoria); if (categoria == null) { throw new Exception("Categoria não encontrada."); } var autor = _repository.ObterAutorPorId(model.IdAutor); if (autor == null) { throw new Exception("Autor não encontrado."); } string home = model.VaiParaHome ? "S" : "N"; noticia.Corpo = model.Corpo; noticia.Categoria = categoria; noticia.VaiParaHome = home; noticia.Status = model.Status; noticia.Autor = autor; noticia.Titulo = model.Titulo; noticia.FotoHome = model.FotoHome; _repository.SalvarNoticia(noticia); } else { var categoria = _repository.ObterCategoriaPorId(model.IdCategoria); if (categoria == null) { throw new Exception("Categoria não encontrada."); } var autor = _repository.ObterAutorPorId(model.IdAutor); if (autor == null) { throw new Exception("Autor não encontrado."); } string home = model.VaiParaHome ? "S" : "N"; var noticia = new Noticia(model.Titulo, model.Corpo, home, categoria, autor); noticia.FotoHome = model.FotoHome; var result = _repository.SalvarNoticia(noticia); model.Id = result.Id; } var retorno = new CadastrarNoticiaModel { AutoresListar = ListarAutores(), CategoriasListar = ListarCategorias() }; return(View("~/Areas/Admin/Views/Noticias/Cadastrar.cshtml", retorno)); }