public async Task <IActionResult> Vote(CourseVoteInputModel inputModel) { await courseService.VoteCourseAsync(inputModel); TempData["ConfirmationMessage"] = "Grazie per aver votato!"; return(RedirectToAction(nameof(Detail), new { id = inputModel.Id })); }
public async Task VoteCourseAsync(CourseVoteInputModel inputModel) { if (inputModel.Vote < 1 || inputModel.Vote > 5) { throw new InvalidVoteException(inputModel.Vote); } string userId = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; int updatedRows = await db.CommandAsync($"UPDATE Subscriptions SET Vote={inputModel.Vote} WHERE CourseId={inputModel.Id} AND UserId={userId}"); if (updatedRows == 0) { throw new CourseSubscriptionNotFoundException(inputModel.Id); } }
public async Task VoteCourseAsync(CourseVoteInputModel inputModel) { if (inputModel.Vote < 1 || inputModel.Vote > 5) { throw new InvalidVoteException(inputModel.Vote); } string userId = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; Subscription subscription = await dbContext.Subscriptions.SingleOrDefaultAsync(subscription => subscription.CourseId == inputModel.Id && subscription.UserId == userId); if (subscription == null) { throw new CourseSubscriptionNotFoundException(inputModel.Id); } subscription.Vote = inputModel.Vote; await dbContext.SaveChangesAsync(); }
public Task VoteCourseAsync(CourseVoteInputModel inputModel) { return(courseService.VoteCourseAsync(inputModel)); }