public IHttpActionResult PostComents(OTMasterClosed closed) { Coments coments = new Coments(); coments.Id_MemoComents = closed.Id_MemoComents; coments.Description = closed.Description; if (!ModelState.IsValid) { return BadRequest(ModelState); } Coments coment = (from c in db.Coments orderby c.Id_MemoComents descending select c).FirstOrDefault(); int lastId = coment.Id_MemoComents + 1; coments.Id_MemoComents = lastId; Hist_OT_Master update = (from h in db.Hist_OT_Master where h.OTFolio.Equals(closed.OTFolio) select h).FirstOrDefault(); if (update != null) { if (update.Id_MemoComents != null && update.Id_MemoComents > 0) { Coments cm = (from c in db.Coments where c.Id_MemoComents == update.Id_MemoComents select c).FirstOrDefault(); if (cm != null) { cm.Description = cm.Description + " -> " + coments.Description; db.Entry(cm).State = EntityState.Modified; } } else { update.Id_MemoComents = lastId; db.Entry(update).State = EntityState.Modified; db.Coments.Add(coments); } } else { OT_Master updateOT = (from o in db.OT_Master where o.OTFolio.Equals(closed.OTFolio) select o).FirstOrDefault(); if (updateOT != null) { if (updateOT.Id_MemoComents != null && updateOT.Id_MemoComents > 0) { Coments cm = (from c in db.Coments where c.Id_MemoComents == updateOT.Id_MemoComents select c).FirstOrDefault(); if (cm != null) { cm.Description = cm.Description + " -> " + coments.Description; db.Entry(cm).State = EntityState.Modified; } } else { updateOT.Id_MemoComents = lastId; db.Entry(updateOT).State = EntityState.Modified; db.Coments.Add(coments); } } } try { db.SaveChanges(); } catch (DbUpdateException) { if (ComentsExists(coments.Id_MemoComents)) { return Conflict(); } else { throw; } } return StatusCode(HttpStatusCode.Created); //return Ok(coments); }
public IHttpActionResult PutComents(int id, Coments coments) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != coments.Id_MemoComents) { return BadRequest(); } db.Entry(coments).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ComentsExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }