示例#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));
        }
示例#2
0
        public async Task <IActionResult> Subscribe(string onUserId)
        {
            var currentUser = await GetCurrentUserAsync();

            if (onUserId == currentUser.Id)
            {
                return(Ok());
            }

            var existing = _context.Subscriptions
                           .Where(subs => subs.SubscriberUser.Id == currentUser.Id)
                           .FirstOrDefault(subs => subs.SubscriptionUser.Id == onUserId);

            if (existing is null)
            {
                _context.Subscriptions.Add(new Subscriptions
                {
                    SubscriberUser   = currentUser,
                    SubscriptionUser = _context.Users.FindAsync(onUserId).Result as User
                });
                _context.SaveChanges();
            }
            return(Ok());
        }
示例#3
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));
        }
示例#4
0
        public static void InitializeFilms(IServiceProvider serviceProvider)
        {
            using (var context = new IJustWatchedContext(
                       serviceProvider.GetRequiredService <
                           DbContextOptions <IJustWatchedContext> >()))
            {
                if (!context.Films.Any())
                {
                    context.Films.AddRange(
                        new Film
                    {
                        Title       = "Joker",
                        ReleaseDate = DateTime.Parse("2019-10-4"),
                        Directors   = "Todd Phillips",
                        PosterURL   = "https://m.media-amazon.com/images/M/MV5BNGVjNWI4ZGUtNzE0MS00YTJmLWE0ZDctN2ZiYTk2YmI3NTYyXkEyXkFqcGdeQXVyMTkxNjUyNQ@@._V1_SX300.jpg",
                        TrailerURL  = "https://www.youtube.com/embed/zAGVQLHvwOY"
                    },

                        new Film
                    {
                        Title       = "Fight Club",
                        ReleaseDate = DateTime.Parse("1999-10-15"),
                        Directors   = "David Fincher",
                        PosterURL   = "https://m.media-amazon.com/images/M/MV5BMmEzNTkxYjQtZTc0MC00YTVjLTg5ZTEtZWMwOWVlYzY0NWIwXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg",
                        TrailerURL  = "https://www.youtube.com/embed/SUXWAEX2jlg"
                    },

                        new Film
                    {
                        Title       = "Blade runner",
                        ReleaseDate = DateTime.Parse("1982-6-25"),
                        Directors   = "Ridley Scott",
                        PosterURL   = "https://m.media-amazon.com/images/M/MV5BNzQzMzJhZTEtOWM4NS00MTdhLTg0YjgtMjM4MDRkZjUwZDBlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_SX300.jpg",
                        TrailerURL  = "https://www.youtube.com/embed/eogpIG53Cis"
                    },

                        new Film
                    {
                        Title       = "Pulp ficiton",
                        ReleaseDate = DateTime.Parse("1994-10-14"),
                        Directors   = "Quentin Tarantino",
                        PosterURL   = "https://m.media-amazon.com/images/M/MV5BNGNhMDIzZTUtNTBlZi00MTRlLWFjM2ItYzViMjE3YzI5MjljXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg",
                        TrailerURL  = "https://www.youtube.com/embed/5ZAhzsi1ybM"
                    },
                        new Film()
                    {
                        Title       = "Interstellar",
                        ReleaseDate = DateTime.Parse("2014-11-7"),
                        Directors   = "Christopher Nolan",
                        PosterURL   = "https://m.media-amazon.com/images/M/MV5BZjdkOTU3MDktN2IxOS00OGEyLWFmMjktY2FiMmZkNWIyODZiXkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg",
                        TrailerURL  = "https://www.youtube.com/embed/zSWdZVtXT7E"
                    },
                        new Film()
                    {
                        Title       = "Mommy",
                        ReleaseDate = DateTime.Parse("2014-10-4"),
                        Directors   = "Xavier Dolan",
                        PosterURL   = "https://m.media-amazon.com/images/M/MV5BMGI3YWFmNDQtNjc0Ny00ZDBjLThlNjYtZTc1ZTk5MzU2YTVjXkEyXkFqcGdeQXVyNzA4ODc3ODU@._V1_SX300.jpg",
                        TrailerURL  = "https://www.youtube.com/embed/dcmueTy2FoM"
                    }
                        );
                    context.SaveChanges();
                }

                if (!context.Users.Any())
                {
                    var userManager = serviceProvider.GetRequiredService <UserManager <User> >();
                    using (var roleContext = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >())
                    {
                        roleContext.CreateAsync(new IdentityRole("admin"));
                        roleContext.CreateAsync(new IdentityRole("moderator"));
                        roleContext.CreateAsync(new IdentityRole("user"));
                    }
                    var users = new List <User>()
                    {
                        new User()
                        {
                            Email        = "*****@*****.**",
                            UserName     = "******",
                            BirthdayDate = DateTime.Now,
                        },
                        new User()
                        {
                            Email        = "*****@*****.**",
                            UserName     = "******",
                            BirthdayDate = DateTime.Now,
                        },
                        new User()
                        {
                            Email        = "*****@*****.**",
                            UserName     = "******",
                            BirthdayDate = DateTime.Now,
                        },
                    };

                    var admin     = userManager.CreateAsync(users[0], "Qwerty_9");
                    var moderator = userManager.CreateAsync(users[1], "Mo_1234");
                    var user      = userManager.CreateAsync(users[2], "Us_1234");

                    if (admin.IsCompletedSuccessfully && moderator.IsCompletedSuccessfully &&
                        user.IsCompletedSuccessfully)
                    {
                        userManager.AddToRoleAsync(users[0], "admin");
                        userManager.AddToRoleAsync(users[1], "moderator");
                        userManager.AddToRoleAsync(users[2], "user");
                    }
                    context.SaveChanges();
                }
            }
        }