public void Consolidate(IEnumerable <T> items, AnimeDbContext context)
 {
     foreach (T item in items)
     {
         Consolidate(item, context);
     }
 }
        public static void CopyOrAddItem <T>(this AnimeDbContext context, T copy, DatabaseGetter <T> getter) where T : class, DatabaseObject <T>
        {
            var dbObject = getter.GetFromDatabase(context, copy);

            if (dbObject != null)
            {
                context.CopyItem(dbObject, copy);
            }
            //else
            //    getter.AddToDatabase(context, copy);
        }
 public void RefreshAnimeList(string username)
 {
     User = mAnimeListRetriever.Get(username);
     using (var context = new AnimeDbContext(mDbPath))
     {
         context.Database.EnsureCreated();
         mDatabaseConsolidator.ConsolidateUser(User, context);
         context.SaveChanges();
     }
     foreach (var anime in User.AnimeList.Select(a => a.Anime))
     {
         UpdateCharactersForAnime(anime);
     }
 }
        public T Consolidate(T item, AnimeDbContext context)
        {
            T dbItem = getFromDatabase(context, item);

            if (dbItem != null)
            {
                dbItem.CopyFrom(item);
                return(dbItem);
            }
            else
            {
                return(item);
            }
        }
 internal IEnumerable <Language> GetLanguages(Character selectedCharacter)
 {
     if (selectedCharacter == null)
     {
         return(new List <Language>());
     }
     using (var context = new AnimeDbContext(mDbPath))
     {
         var dbCharacter = CharacterDbGetter.Instance.GetFromDatabase(context, selectedCharacter);
         context.Entry(dbCharacter).Collection(c => c.Actors).Load();
         foreach (var characterActor in dbCharacter.Actors)
         {
             context.Entry(characterActor).Reference(ca => ca.Language).Load();
         }
         return(dbCharacter.Actors.Select(a => a.Language).Distinct());
     }
 }
 internal Character IncludeCharacterValues(Character character)
 {
     if (character == null)
     {
         return(null);
     }
     using (var context = new AnimeDbContext(mDbPath))
     {
         var dbCharacter = CharacterDbGetter.Instance.GetFromDatabase(context, character);
         context.Entry(dbCharacter).Collection(c => c.Actors).Load();
         foreach (var characterActor in dbCharacter.Actors)
         {
             context.Entry(characterActor).Reference(ca => ca.Language).Load();
             context.Entry(characterActor).Reference(ca => ca.Actor).Load();
         }
         return(dbCharacter);
     }
 }
        public void ConsolidateUser(User user, AnimeDbContext context)
        {
            var  dbUser = context.Users.Find(user.UserId);
            User addedUser;

            if (dbUser != null)
            {
                context.CopyItem(dbUser, user);
                addedUser = dbUser;
            }
            else
            {
                context.Add(user);
                addedUser = user;
            }

            foreach (var animeEntry in addedUser.AnimeList)
            {
                var          dbAnimeEntry = context.MyAnimeEntries.Find(animeEntry.AnimeId, animeEntry.UserId);
                MyAnimeEntry addedAnimeEntry;
                if (dbAnimeEntry != null)
                {
                    context.CopyItem(dbAnimeEntry, animeEntry);
                    addedAnimeEntry = dbAnimeEntry;
                }
                else
                {
                    context.Add(animeEntry);
                    addedAnimeEntry = animeEntry;
                }

                var dbAnime = context.Anime.Find(addedAnimeEntry.Anime.AnimeID);
                if (dbAnime != null)
                {
                    context.CopyItem(dbAnime, addedAnimeEntry.Anime);
                }
                else
                {
                    context.Add(addedAnimeEntry.Anime);
                }
            }
        }
        public User Consolidate(User user, AnimeDbContext context)
        {
            var animeEntries    = user.AnimeList;
            var animes          = animeEntries.Select(a => a.Anime).Distinct();
            var characterAnimes = animes.SelectMany(a => a.CharacterAnimes).Distinct();
            var characters      = characterAnimes.Select(a => a.Character).Distinct();
            var characterActors = characters.SelectMany(c => c.Actors).Distinct();
            var voiceActors     = characterActors.Select(c => c.Actor).Distinct();
            var languages       = characterActors.Select(c => c.Language).Distinct();
            var names           = characters.Select(c => c.Name).Union(voiceActors.Select(v => v.Name)).Distinct();

            mNameConsolidator.Consolidate(names, context);
            mLanguageConsolidator.Consolidate(languages, context);
            mVoiceActorConsolidator.Consolidate(voiceActors, context);
            mCharacterActorConsolidator.Consolidate(characterActors, context);
            mCharacterConsolidator.Consolidate(characters, context);
            mCharacterAnimeConsolidator.Consolidate(characterAnimes, context);
            mAnimeConsolidator.Consolidate(animes, context);
            mAnimeEntryConsolidator.Consolidate(animeEntries, context);
            mUserConsolidator.Consolidate(user, context);
            return(user);
        }
        public Anime UpdateCharactersForAnime(Anime anime)
        {
            Anime dbAnime;

            using (var context = new AnimeDbContext(mDbPath))
            {
                dbAnime = context.Anime
                          .Include(a => a.CharacterAnimes)
                          .ThenInclude(ca => ca.Character)
                          .ThenInclude(c => c.Name)
                          .Where(a => a.AnimeID == anime.AnimeID).FirstOrDefault();

                if (dbAnime != null)
                {
                    context.CopyItem(dbAnime, anime);
                }
                if (dbAnime == null || dbAnime.Characters.Count() == 0)
                {
                    forceUpdateCharactersForAnime(anime, context);
                }
                context.SaveChanges();
            }
            return(dbAnime);
        }
        public IEnumerable <CharacterActor> CrossReference(Character selectedCharacter, Language selectedLanguage)
        {
            var actor = selectedCharacter.Actors.Where(a => a.Language.Equals(selectedLanguage)).FirstOrDefault()?.Actor;
            IEnumerable <CharacterActor> ret;

            using (var context = new AnimeDbContext(mDbPath))
            {
                var animeIds = User.AnimeList.Select(a => a.AnimeId);
                ret = (from animeId in User.AnimeList.Select(a => a.AnimeId)
                       join characterAnime in context.CharacterAnime on animeId equals characterAnime.AnimeId
                       join characterActor in context.CharacterActors
                       .Include(ca => ca.Character)
                       .ThenInclude(c => c.CharacterAnimes)
                       .ThenInclude(ca => ca.Anime)
                       .Include(ca => ca.Character)
                       .ThenInclude(c => c.Name)
                       .Include(ca => ca.Actor)
                       .ThenInclude(a => a.Name)
                       on characterAnime.CharacterId equals characterActor.CharacterId
                       where characterActor.ActorId == actor.VoiceActorID
                       select characterActor).Distinct().ToList();
            }
            return(ret);
        }
 public Anime GetFromDatabase(AnimeDbContext context, Anime item)
 {
     return(item?.AnimeID != null?context.Anime.Find(item.AnimeID) : null);
 }
 public void AddToDatabase(AnimeDbContext context, Anime item)
 {
     context.Anime.Add(item);
 }
 protected override User getFromDatabase(AnimeDbContext context, User item)
 {
     return(context.Users.Find(item.UserId));
 }
 public CharacterActor GetFromDatabase(AnimeDbContext context, CharacterActor item)
 {
     return(item?.CharacterId != null && item?.ActorId != null && item?.LanguageId != null?
            context.CharacterActors.Find(item.CharacterId, item.ActorId, item.LanguageId) : null);
 }
 public void AddToDatabase(AnimeDbContext context, CharacterActor item)
 {
     context.CharacterActors.Add(item);
 }
 protected abstract T getFromDatabase(AnimeDbContext context, T item);
 protected override CharacterAnime getFromDatabase(AnimeDbContext context, CharacterAnime item)
 {
     return(context.CharacterAnime.Find(item.CharacterId, item.AnimeId));
 }
 public CharacterAnime GetFromDatabase(AnimeDbContext context, CharacterAnime item)
 {
     return(item?.CharacterId != null && item?.AnimeId != null?context.CharacterAnime.Find(item.CharacterId, item.AnimeId) : null);
 }
 public void AddToDatabase(AnimeDbContext context, Name item)
 {
     context.Names.Add(item);
 }
Ejemplo n.º 20
0
 public void AddToDatabase(AnimeDbContext context, Language item)
 {
     context.Languages.Add(item);
 }
Ejemplo n.º 21
0
 public VoiceActor GetFromDatabase(AnimeDbContext context, VoiceActor item)
 {
     return(item?.VoiceActorID != null?context.VoiceActors.Find(item.VoiceActorID) : null);
 }
Ejemplo n.º 22
0
 protected override Name getFromDatabase(AnimeDbContext context, Name item)
 {
     return(context.Names.Find(item.NameId));
 }
 public Name GetFromDatabase(AnimeDbContext context, Name item)
 {
     return(item?.NameId != null?context.Names.Find(item.NameId) : null);
 }
        private void forceUpdateCharactersForAnime(Anime anime, AnimeDbContext context)
        {
            ICollection <Character> characters = mCharacterRetriever.Get(anime);

            mDatabaseConsolidator.ConsolidateCharacters(anime, characters, context);
        }
Ejemplo n.º 25
0
 public void AddToDatabase(AnimeDbContext context, VoiceActor item)
 {
     context.VoiceActors.Add(item);
 }
Ejemplo n.º 26
0
 public void AddToDatabase(AnimeDbContext context, User item)
 {
     context.Users.Add(item);
 }
Ejemplo n.º 27
0
 protected override Character getFromDatabase(AnimeDbContext context, Character item)
 {
     return(context.Characters.Find(item.CharacterID));
 }
Ejemplo n.º 28
0
 public User GetFromDatabase(AnimeDbContext context, User item)
 {
     return(item?.UserId != null?context.Users.Find(item.UserId) : null);
 }
Ejemplo n.º 29
0
 public Language GetFromDatabase(AnimeDbContext context, Language item)
 {
     return(item?.LanguageID != null?context.Languages.Find(item.LanguageID) : null);
 }
Ejemplo n.º 30
0
 protected override VoiceActor getFromDatabase(AnimeDbContext context, VoiceActor item)
 {
     return(context.VoiceActors.Find(item.VoiceActorID));
 }