Beispiel #1
0
        public static void AddTagsToVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidAppContext())
            {
                // Mit LINQ:
                // SELECT FROM Tags WHERE Name IN ('classics', 'drama')
                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();
            }
        }
Beispiel #2
0
 public static void AddVideo(Video video)
 {
     using (var context = new VidAppContext())
     {
         context.Videos.Add(video);
         context.SaveChanges();
     }
 }
Beispiel #3
0
        public static void RemoveVideo(int videoId)
        {
            using (var context = new VidAppContext())
            {
                var video = context.Videos.SingleOrDefault(v => v.Id == videoId);
                if (video == null)
                {
                    return;
                }

                context.Videos.Remove(video);
                context.SaveChanges();
            }
        }
Beispiel #4
0
        public static void RemoveTagsFromVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidAppContext())
            {
                context.Tags.Where(t => tagNames.Contains(t.Name)).Load();

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

                foreach (var tagName in tagNames)
                {
                    // Wir haben die Logik für das Entfernen von Tags in der Video-Klasse gekapselt.
                    // Das ist sauberes OO. Die Video-Klasse soll verantwortlich sein, um Tags
                    // um Tags zu ihrer Liste hinzuzufügen bzw. zu entfernen.
                    video.RemoveTag(tagName);
                }

                context.SaveChanges();
            }
        }
Beispiel #5
0
        public static void RemoveGenre(int genreId, bool enforceDeletingVideos)
        {
            using (var context = new VidAppContext())
            {
                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();
            }
        }
Beispiel #6
0
        public static void AddTags(params string[] tagNames)
        {
            using (var context = new VidAppContext())
            {
                // Wir laden die Tags vorgängig um Duplikate zu vermeiten
                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();
            }
        }