Exemplo n.º 1
0
        public async Task <IActionResult> New(ReviewViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Review review = new Review
                {
                    Author           = await GetCurrentUserAsync(),
                    Content          = viewModel.ReviewText,
                    CreationDateTime = DateTime.Now,
                    ReviewTitle      = viewModel.Title,
                    ReviewFilm       = _context.Films.First(film => film.Title.Equals(viewModel.FilmTitle,
                                                                                      StringComparison.OrdinalIgnoreCase))
                };
                _context.Add(review);
                _context.SaveChanges();

                if (viewModel.Tags != null)
                {
                    var tags = Regex.Split(viewModel.Tags, @"\s+")
                               .Where(tag => tag != "")
                               .Select(tag => tag.Trim('#').ToLower());
                    foreach (var tagString in tags)
                    {
                        var tag = _context.Tags.FirstOrDefault(exTag => exTag.TagText == tagString);
                        if (tag is null)
                        {
                            tag = new Tag {
                                TagText = tagString
                            };
                            _context.Add(tag);
                        }
                        _context.Add(new TagReview {
                            TaggedReview = review, TagInReview = tag
                        });
                        _context.SaveChanges();
                    }
                }
                return(RedirectToAction("Index", "Review", new { reviewId = review.Id }));
            }
            return(View(viewModel));
        }
Exemplo n.º 2
0
        public async Task SendComment(string userName, string content, int reviewId)
        {
            var author = await _userManager.FindByNameAsync(userName);

            var comment = new Comment
            {
                Author           = author,
                CommentedReview  = _context.Reviews.FirstOrDefault(r => r.Id == reviewId),
                Content          = content,
                CreationDateTime = DateTime.Now
            };

            _context.Add(comment);
            _context.SaveChanges();
            await Clients.All.SendAsync("ReceiveComment", reviewId, userName, author.Id,
                                        author.PhotoUrl, content,
                                        comment.CreationDateTime.ToString(CultureInfo.CurrentCulture));
        }