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)); }
public async Task <IActionResult> Update(int id, UpdateVoteCommand command) { if (id != command.Id) { return(BadRequest()); } return(Ok(await Mediator.Send(command))); }
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(); }