Пример #1
0
        public async Task <IActionResult> Edit(UpdateVoteCommand command)
        {
            try
            {
                var vote         = _voteService.GetVote(command.Id);
                var isAuthorized = await _authorizationService.AuthorizeAsync(User, vote, "CanManageVote");

                if (!isAuthorized.Succeeded)
                {
                    return(new ForbidResult());
                }

                if (ModelState.IsValid)
                {
                    _voteService.UpdateVote(command);
                    return(RedirectToAction(nameof(Details), new { id = command.Id }));
                }
            }
            catch (Exception)
            {
                // Add a model-level error by using an empty string key
                ModelState.AddModelError(string.Empty, "An error occured saving the vote");
            }

            // If we got to here, something went wrong
            return(View(command));
        }
Пример #2
0
 public async Task <IActionResult> Update(int id, UpdateVoteCommand command)
 {
     if (id != command.Id)
     {
         return(BadRequest());
     }
     return(Ok(await Mediator.Send(command)));
 }
Пример #3
0
        public void UpdateVote(UpdateVoteCommand command)
        {
            var vote = _context.Votes.Find(command.Id);

            if (vote == null)
            {
                throw new Exception("Unable to find the vote");
            }

            command.UpdateVote(vote);
            _context.SaveChanges();
        }