Ejemplo n.º 1
0
        public async Task <IHttpActionResult> UpdateExistingMotion(int motionId, AmendmentUpdate 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.AmendmentId)
            {
                return(BadRequest($"Body ID ({model.AmendmentId}) and URI ID ({motionId}) mismatch."));
            }

            if (await _service.UpdateExistingAmendmentAsync(model))
            {
                return(Ok("Amendment updated."));
            }

            return(BadRequest("Cannot update amendment."));
        }
Ejemplo n.º 2
0
        // Update Existing
        public async Task <bool> UpdateExistingAmendmentAsync(AmendmentUpdate model)
        {
            var motion = await _context.Amendments.FindAsync(model.AmendmentId);

            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);
        }