// Update Existing public async Task <bool> UpdateExistingMotionAsync(MotionUpdate model) { var motion = await _context.Motions.FindAsync(model.MotionId); if (motion == null || motion.PresentingUserId != _userId) { return(false); } if (motion.Title == model.Title && motion.Description == model.Description) { return(false); } int changeCount = 0; for (int totalVotes = motion.Votes.Count; changeCount < totalVotes; changeCount++) { _context.Votes.Remove(motion.Votes.ElementAt(0)); } motion.Title = model.Title; motion.Description = model.Description; return(await _context.SaveChangesAsync() == changeCount + 1); }
public async Task <IHttpActionResult> UpdateExistingMotion(int motionId, MotionUpdate model) { _service.SetUserId(User.Identity.GetUserId()); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (model == null) { return(BadRequest("Request body cannot be empty.")); } if (motionId != model.MotionId) { return(BadRequest($"Body ID ({model.MotionId}) and URI ID ({motionId}) mismatch.")); } if (await _service.UpdateExistingMotionAsync(model)) { return(Ok("Motion updated.")); } return(BadRequest("Cannot update motion.")); }