Exemple #1
0
        public IActionResult Vote(VoteViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Episode episode = _episodeRepository.GetById(model.EpisodeId);

                    if (episode == null)
                    {
                        ModelState.AddModelError("EpisodeId", "Episódio não encontrado.");
                        ViewBag.Episodes = _episodeRepository.GetAll().ToList();

                        return(View("Index", model));
                    }

                    Participation participation = new Participation
                    {
                        ParticipantId = Int32.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value),
                        EpisodeId     = episode.Id,
                        CreatedAt     = DateTime.Now
                    };

                    _participationRepository.Add(participation);
                    _participationRepository.SaveChanges();

                    TempData["Success"] = "Voto registrado com sucesso!";

                    return(RedirectToAction("Index", "Home"));
                }
            }
            catch (Exception exception)
            {
                TempData["Error"] = "Internal server error";
                _logger.LogError("Register vote error: " + exception);
            }

            ViewBag.Episodes = _episodeRepository.GetAll().ToList();

            return(View("Index", model));
        }