public object GetById(int id) { using (GaneshaAdriContext db = new GaneshaAdriContext()) { Contenido contenido = db.Contenido.Where(x => x.IdContenido == id).ToList().FirstOrDefault(); ContenidoViewModel contenidoViewModel = new ContenidoViewModel { Id = contenido.IdContenido, Content = contenido.Html == null ? string.Empty : Encoding.ASCII.GetString(contenido.Html), Title = contenido.Titulo }; return(contenidoViewModel); } }
public object Get() { using ( GaneshaAdriContext db = new GaneshaAdriContext()) { List <ContenidoViewModel> lstContenidoViewModel = new List <ContenidoViewModel>(); foreach (var item in db.Contenido.ToList()) { ContenidoViewModel contenidoViewModel = new ContenidoViewModel { Id = item.IdContenido, Content = item.Html == null ? string.Empty : Encoding.ASCII.GetString(item.Html), Title = item.Titulo }; lstContenidoViewModel.Add(contenidoViewModel); } return(lstContenidoViewModel.ToList()); } }
public object Update(ContenidoViewModel model) { try { using (GaneshaAdriContext db = new GaneshaAdriContext()) { Contenido contenido = db.Contenido.Find(model.Id); contenido.Titulo = model.Title; contenido.Html = Encoding.ASCII.GetBytes(model.Content); db.Entry(contenido).State = Microsoft.EntityFrameworkCore.EntityState.Modified; db.SaveChanges(); return(new Response { Status = StatusResponse.Success, Message = "Succesfully Updated" }); } } catch (Exception) { return(new Response { Status = StatusResponse.Error, Message = "Invalid Data." }); } }
public object Add(ContenidoViewModel model) { try { using (GaneshaAdriContext db = new GaneshaAdriContext()) { Contenido contenido = new Contenido(); contenido.Html = Encoding.ASCII.GetBytes(model.Content); contenido.Titulo = model.Title; db.Contenido.Add(contenido); db.SaveChanges(); return(new Response { Status = StatusResponse.Success, Message = "Succesfully Saved" }); } } catch (Exception) { return(new Response { Status = StatusResponse.Error, Message = "Invalid Data." }); } }