public IHttpActionResult PostActivityVote(ActivityVote activityVote) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var context = new TripServiceAppContext(); lock (lockObj) { ActivityVote vote = context.ActivityVotes.Where(r => r.TripActivityId == activityVote.TripActivityId && r.TripUserId == activityVote.TripUserId).FirstOrDefault(); // user had already voted. update their vote if (vote != null) { vote.Vote = activityVote.Vote; return PutActivityVote(vote.Id, vote); ; } else { context.ActivityVotes.Add(activityVote); context.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = activityVote.Id }, activityVote); } } }
public IHttpActionResult PutActivityVote(int id, ActivityVote activityVote) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != activityVote.Id) { return BadRequest(); } db.Entry(activityVote).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ActivityVoteExists(id)) { return NotFound(); } else { throw; } } return CreatedAtRoute("DefaultApi", new { id = activityVote.Id }, activityVote); }