internal static void AddVideo(Video video)
 {
     using (var context = new MovieRentalBackendContext())
     {
         context.Videos.Add(video);
         context.SaveChanges();
     }
 }
        internal static void RemoveVideo(int videoID)
        {
            using (var context = new MovieRentalBackendContext())
            {
                var video = context.Videos.SingleOrDefault(v => v.ID == videoID);
                if (video == null)
                {
                    return;
                }

                context.Videos.Remove(video);
                context.SaveChanges();
            }
        }
 internal static void RemoveTagsFromVideo(int videoID, params string[] tagNames)
 {
     using (var context = new MovieRentalBackendContext())
     {
         //Explicit loading could be used to only load tags that are needed to be deleted.
         context.Tags.Where(t => tagNames.Contains(t.Name)).Load();
         var video = context.Videos.Single(v => v.ID == videoID);
         foreach (var tagName in tagNames)
         {
             // The concept of removing a tag has been encapsulated inside the Video class.
             // This is the object-oriented way to implement this. The Video class
             // should be responsible for adding/removing objects to its Tags collection.
             video.RemoveTag(tagName);
         }
         context.SaveChanges();
     }
 }
 internal static void AddTags(params string[] tagNames)
 {
     using (var context = new MovieRentalBackendContext())
     {
         //First load tags with the given names to prevent adding duplicates.
         var tags = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList();
         foreach (var name in tagNames)
         {
             if (!tags.Any(t => t.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)))
             {
                 context.Tags.Add(new Tag {
                     Name = name
                 });
             }
         }
         context.SaveChanges();
     }
 }
        internal static void RemoveGenre(int genreID, bool enforceDeletingVideos)
        {
            using (var context = new MovieRentalBackendContext())
            {
                var genre = context.Genres.Include(g => g.Videos).SingleOrDefault(g => g.ID == genreID);
                if (genre == null)
                {
                    return;
                }

                if (enforceDeletingVideos)
                {
                    context.Videos.RemoveRange(genre.Videos);
                }

                context.Genres.Remove(genre);
                context.SaveChanges();
            }
        }
 internal static void AddTagsToVideo(int videoID, params string[] tagNames)
 {
     using (var context = new MovieRentalBackendContext())
     {
         var tags = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList();
         foreach (var tagName in tagNames)
         {
             if (!tags.Any(t => t.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase)))
             {
                 tags.Add(new Tag {
                     Name = tagName
                 });
             }
         }
         var video = context.Videos.Single(v => v.ID == videoID);
         tags.ForEach(t => video.AddTag(t));
         context.SaveChanges();
     }
 }
Пример #7
0
        internal static void ExecuteExercise_3()
        {
            var context = new MovieRentalBackendContext();

            Console.WriteLine("Action Movies Sorted by Name by Name First\n");

            var ActionMoviesByName = context.Videos.Where(v => v.Genre.Name == "Action").OrderBy(v => v.Name);

            foreach (var x in ActionMoviesByName)
            {
                Console.WriteLine(x.Name);
            }

            Console.WriteLine("\n\n\n");

            Console.WriteLine("Gold Drama Movies by Name by Newest First\n");

            var GoldDramaMoviesByNewestFirst = context.Videos
                                               .Where(v => v.Genre.Name == "Drama" && v.Classification == Classification.Gold)
                                               .OrderByDescending(v => v.ReleaseDate);

            foreach (var x in GoldDramaMoviesByNewestFirst)
            {
                Console.WriteLine(x.Name);
            }

            Console.WriteLine("\n\n\n");

            Console.WriteLine("All movies projected into an anonymous type with two properties\n");

            var MoviesWithTwoProperties = context.Videos
                                          .Select(v => new { MovieName = v.Name, Genre = v.Genre.Name });

            foreach (var x in MoviesWithTwoProperties)
            {
                Console.WriteLine("{0}\t{1}", x.Genre, x.MovieName);
            }

            Console.WriteLine("\n\n\n");

            Console.WriteLine("All movies grouped by their classification\n");

            var MoviesGroupedByClassification = context.Videos
                                                .GroupBy(v => v.Classification)
                                                .Select(g => new
            {
                Classification = g.Key.ToString(),
                Videos         = g.OrderBy(v => v.Name)
            });

            foreach (var x in MoviesGroupedByClassification)
            {
                Console.WriteLine("Classification : {0}", x.Classification);

                foreach (var y in x.Videos)
                {
                    Console.WriteLine("\t{0}", y.Name);
                }
            }

            Console.WriteLine("\n\n\n");

            Console.WriteLine("List of classifications sorted alphabetically and count of videos in them\n");

            var VideosCountByClassification = context.Videos
                                              .GroupBy(v => v.Classification)
                                              .Select(v => new
            {
                Classification = v.Key.ToString(),
                Videos         = v.Count()
            })
                                              .OrderBy(c => c.Classification);

            foreach (var x in VideosCountByClassification)
            {
                Console.WriteLine("{0}({1})", x.Classification, x.Videos);
            }

            Console.WriteLine("\n\n\n");

            Console.WriteLine("List of genres and number of videos they include, sorted by the number of videos\n");

            var GenresCountByNumber = context.Genres
                                      .GroupJoin(context.Videos, g => g.ID, v => v.GenreID, (genre, videos) => new
            {
                Name        = genre.Name,
                VideosCount = videos.Count()
            })
                                      .OrderByDescending(g => g.VideosCount);

            foreach (var x in GenresCountByNumber)
            {
                Console.WriteLine("{0}({1})", x.Name, x.VideosCount);
            }

            Console.WriteLine("\n\n\n");

            var abcde = context.Videos
                        .GroupBy(v => v.Genre.Name)
                        .Select(g => new
            {
                GenreName   = g.Key.ToString(),
                VideosCount = g.Count()
            })
                        .OrderByDescending(c => c.VideosCount);

            foreach (var x in abcde)
            {
                Console.WriteLine("{0}({1})", x.GenreName, x.VideosCount);
            }
        }