Ejemplo n.º 1
0
        private static void AddTagToVideo(string videoName, string tagName)
        {
            using (var context = new VidzyContext())
            {
                Tag newTag = context.Tags
                             .Where(t => t.Name == tagName)
                             .FirstOrDefault();

                if (newTag == null)
                {
                    newTag = new Tag
                    {
                        Name = tagName
                    };

                    context.Tags.Add(newTag);
                    context.SaveChanges();
                }

                var video = context.Videos
                            .Where(v => v.Name == videoName)
                            .FirstOrDefault();

                var videoTag = video.Tags
                               .Where(t => t.Name == tagName)
                               .FirstOrDefault();

                if (videoTag == null)
                {
                    video.Tags.Add(newTag);
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public static void AddTagstoVideo(int id, params string[] tagNames)
        {
            using (var context = new VidzyContext())
            {
                var tags = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList();
                if (tags == null)
                {
                    return;
                }

                foreach (var item in tagNames)
                {
                    if (!tags.Any(t => t.Name.Contains(item)))
                    {
                        tags.Add(new Tag {
                            Name = item
                        });
                    }
                }
                var video = context.Videos.Find(id);
                if (video == null)
                {
                    return;
                }

                tags.ForEach(t => video.AddTag(t));



                context.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        public static void addTagsToVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidzyContext())
            {
                var tags      = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList();
                var video     = context.Videos.Find(videoId);
                var videoTags = video.Tags;

                foreach (var name in tagNames)
                {
                    if (!tags.Any(t => t.Name.Equals(name)))
                    {
                        tags.Add(new Tag {
                            Name = name
                        });
                    }
                    if (!videoTags.Any(t => t.Name.Equals(name)))
                    {
                        video.Tags.Add(new Tag {
                            Name = name
                        });
                    }
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 4
0
        // In terms of API design, this method expects tag names in the form of a string array.
        // We shouldn't use TagId because that would only work if the given tag exists in the database.
        // But often, in an application with a good user experience, the user should be able to pick a
        // tag from a drop-down list, or add one at the same time as adding or editing a video. So,
        // we should use tag names to add a new tag to the database. Plus, tag names should be unique,
        // so conceptually, they can be treated as primary keys, but we use an int (TagId) for optimization.
        public static void AddTagsToVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidzyContext())
            {
                // This technique with LINQ leads to
                //
                // SELECT FROM Tags WHERE Name IN ('classics', 'drama')
                var tags = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList();

                // So, first we load tags with the given names from the database
                // to ensure we won't duplicate them. Now, we loop through the list of
                // tag names, and if we don't have such a tag in the database, we add
                // them to the list.
                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();
            }
        }
Ejemplo n.º 5
0
 public static void AddVideo(Video video)
 {
     using (var context = new VidzyContext())
     {
         context.Videos.Add(video);
         context.SaveChanges();
     }
 }
Ejemplo n.º 6
0
        public static void removeVideo(int videoId)
        {
            using (var context = new VidzyContext())
            {
                var video = context.Videos.Find(videoId);
                if (video == null)
                {
                    return;
                }

                context.Videos.Remove(video);
                context.SaveChanges();
            }
        }
Ejemplo n.º 7
0
        public static void RemoveVideo(int videoId)
        {
            using (var context = new VidzyContext())
            {
                var video = context.Videos.SingleOrDefault(v => v.Id == videoId);
                if (video == null)
                {
                    return;
                }

                context.Videos.Remove(video);
                context.SaveChanges();
            }
        }
Ejemplo n.º 8
0
        public static void RemoveTagFromVideo(int videoId, params string [] tagNames)
        {
            using (var context = new VidzyContext())
            {
                context.Tags.Where(t => tagNames.Contains(t.Name)).Load();

                var video = context.Videos.Single(v => v.Id == videoId);
                foreach (var tagName in tagNames)
                {
                    video.RemoveTag(tagName);
                }
                context.SaveChanges();
            }
        }
Ejemplo n.º 9
0
 public static void AddVideo()
 {
     using (var context = new VidzyContext())
     {
         context.Videos.Add(
             new Video
         {
             Name           = "Terminator1",
             GenreId        = 2,
             ReleaseDate    = new System.DateTime(1984, 10, 26),
             Classification = Classification.Silver
         });
         context.SaveChanges();
     }
 }
Ejemplo n.º 10
0
        private static void RemoveVideo(string name)
        {
            using (var context = new VidzyContext())
            {
                var video = context.Videos
                            .Where(v => v.Name == name)
                            .FirstOrDefault();

                if (video != null)
                {
                    context.Videos.Remove(video);
                    context.SaveChanges();
                }
            }
        }
Ejemplo n.º 11
0
        private static void RemoveGenre(string name)
        {
            using (var context = new VidzyContext())
            {
                var genre = context.Genres
                            .Where(g => g.Name == name)
                            .FirstOrDefault();

                if (genre != null)
                {
                    context.Videos.RemoveRange(genre.Videos);
                    context.Genres.Remove(genre);
                    context.SaveChanges();
                }
            }
        }
Ejemplo n.º 12
0
        static void RemoveGenre(int genreId)
        {
            using (var context = new VidzyContext())
            {
                var genre = context.Genres.Include(g => g.Videos).SingleOrDefault(g => g.Id == genreId);

                if (genre == null)
                {
                    return;
                }

                context.Videos.RemoveRange(genre.Videos);
                context.Genres.Remove(genre);

                context.SaveChanges();
            }
        }
Ejemplo n.º 13
0
 public static void AddTag(params string [] tagNames)
 {
     using (var context = new VidzyContext())
     {
         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)))
             {
                 context.Tags.Add(new Tag {
                     Name = tagName
                 });
             }
         }
         context.SaveChanges();
     }
 }
Ejemplo n.º 14
0
        public static void AddTags(string name)
        {
            using (var context = new VidzyContext())
            {
                if (context.Tags.Any(t => t.Name.Contains(name)))
                {
                    return;
                }

                context.Tags.Add(
                    new Tag
                {
                    Name = name
                });
                context.SaveChanges();
            }
        }
Ejemplo n.º 15
0
        public static void addTags(params string[] tagNames)
        {
            using (var context = new VidzyContext())
            {
                var tags = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList();


                foreach (var name in tagNames)
                {
                    if (!tags.Any(t => t.Name.Equals(name)))
                    {
                        context.Tags.Add(new Tag {
                            Name = name
                        });
                    }
                }
                context.SaveChanges();
            }
        }
Ejemplo n.º 16
0
        public static void RemoveTagFromVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidzyContext())
            {
                var video = context.Videos.SingleOrDefault(v => v.Id == videoId);

                if (video == null)
                {
                    return;
                }

                foreach (var item in tagNames)
                {
                    video.RemoveTag(item);
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 17
0
        public static void AddTagToVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidzyContext())
            {
                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();
                }
            }
        }
Ejemplo n.º 18
0
        public static void AddTags(params string[] tagNames)
        {
            using (var context = new VidzyContext())
            {
                // We load the tags with the given names first, 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();
            }
        }
Ejemplo n.º 19
0
        public static void RemoveTagsFromVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidzyContext())
            {
                // We can use explicit loading to only load tags that we're going to delete.
                context.Tags.Where(t => tagNames.Contains(t.Name)).Load();

                var video = context.Videos.Single(v => v.Id == videoId);

                foreach (var tagName in tagNames)
                {
                    // I've encapsulated the concept of removing a tag 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();
            }
        }
Ejemplo n.º 20
0
        private static void AddVideo(string name, string genreName, DateTime releaseDate,
                                     Classification classification)
        {
            using (var context = new VidzyContext())
            {
                var genre = context.Genres
                            .Where(g => g.Name == genreName)
                            .First();

                var video = new Video()
                {
                    Name           = name,
                    Genre          = genre,
                    ReleaseDate    = releaseDate,
                    Classification = classification
                };

                context.Videos.Add(video);
                context.SaveChanges();
            }
        }
Ejemplo n.º 21
0
        private static void RemoveTagFromVideo(string videoName, string tagName)
        {
            using (var context = new VidzyContext())
            {
                var tag = context.Tags
                          .Where(t => t.Name == tagName)
                          .FirstOrDefault();

                if (tag != null)
                {
                    var video = context.Videos
                                .Where(v => v.Name == videoName)
                                .FirstOrDefault();

                    if (video != null)
                    {
                        video.Tags.Remove(tag);
                        context.SaveChanges();
                    }
                }
            }
        }