public IHttpActionResult Put(int id, LockStatusModel Lock) { try { if (Lock == null) { return(BadRequest("Could not read Lock from body")); } var originalLock = _ctx.LockStatus.Find(id); if (originalLock == null || originalLock.ID != id) { return(BadRequest("Lock is not found")); } else { Lock.ID = id; } _ctx.Entry(originalLock).CurrentValues.SetValues(Lock); _ctx.SaveChanges(); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } } // UpLock Lock Method
public IHttpActionResult Post(LockStatusModel Lock) { try { if (!ModelState.IsValid || Lock == null) { return(BadRequest(ModelState)); } var oldLock = _ctx.LockStatus.SingleOrDefault(w => w.DepartmentCode == Lock.DepartmentCode && w.DateId == Lock.DateId ); if (oldLock != null) { return(Put(oldLock.ID, Lock)); } var newLock = _ctx.LockStatus.Add(Lock); _ctx.SaveChanges(); if (newLock == null) { return(BadRequest(ModelState)); } return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } } // New Lock Method