public IHttpActionResult Post(TB_NEWS tB_NEWS) { string erro = ""; if (!ModelState.IsValid) { var modelErrors = new List <string>(); foreach (var modelState in ModelState.Values) { foreach (var modelError in modelState.Errors) { modelErrors.Add(modelError.ErrorMessage); } } erro = modelErrors.First(); return(BadRequest(erro)); } tB_NEWS.dt_icl = DateTime.Now; db.TB_NEWS.Add(tB_NEWS); db.SaveChanges(); var news = db.TB_NEWS.Include("TB_CATEGORIES").SingleOrDefault(n => n.id_new == tB_NEWS.id_new); return(Ok(news)); }
public IHttpActionResult Get(int id) { TB_NEWS tB_NEWS = db.TB_NEWS.Include("TB_CATEGORIES").Where(n => n.id_new == id).FirstOrDefault(); if (tB_NEWS == null) { return(NotFound()); } return(Ok(tB_NEWS)); }
public IHttpActionResult Delete(int id) { TB_NEWS tB_NEWS = db.TB_NEWS.Find(id); if (tB_NEWS == null) { return(NotFound()); } db.TB_NEWS.Remove(tB_NEWS); db.SaveChanges(); return(Ok(new { id_cat = tB_NEWS.id_cat })); }
public IHttpActionResult Put(TB_NEWS tB_NEWS) { string erro = ""; if (!ModelState.IsValid) { var modelErrors = new List <string>(); foreach (var modelState in ModelState.Values) { foreach (var modelError in modelState.Errors) { modelErrors.Add(modelError.ErrorMessage); } } erro = modelErrors.First(); } tB_NEWS.dt_icl = DateTime.Now; db.Entry(tB_NEWS).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!TB_NEWSExists(tB_NEWS.id_new)) { return(NotFound()); } else { throw; } } var news = db.TB_NEWS.Include("TB_CATEGORIES").SingleOrDefault(n => n.id_new == tB_NEWS.id_new); return(Ok(news)); }