Пример #1
0
        public async void Delete(int id)
        {
            using (var db = Db)
            {
                var pokemonToPokemonSkillRepository = new PokemonToPokemonSkillRepository(db);
                var skillMaps = await pokemonToPokemonSkillRepository.GetByPokemonId(id);

                foreach (var skillMap in skillMaps)
                {
                    pokemonToPokemonSkillRepository.Delete(skillMap);
                }

                var pokemonToPokemonTypeRepository = new PokemonToPokemonTypeRepository(db);
                var typeMaps = await pokemonToPokemonTypeRepository.GetByPokemonId(id);

                foreach (var typeMap in typeMaps)
                {
                    pokemonToPokemonTypeRepository.Delete(typeMap);
                }

                var pokemonRepository = new PokemonRepository(db);
                var pokemon           = await pokemonRepository.FindOneById(id);

                pokemonRepository.Delete(pokemon);
            }
        }
Пример #2
0
        private async Task <List <PokemonType> > GetPokemonTypes(int id, PokedexContext db)
        {
            var pokemonToPokemonTypeRepository = new PokemonToPokemonTypeRepository(db);
            var task = await pokemonToPokemonTypeRepository.GetByPokemonId(id);

            var results = task.Select(x => x.PokemonTypeId);

            var pokemonTypeRepository = new PokemonTypeRepository(db);
            var task2 = await pokemonTypeRepository.GetAll();

            return(task2.Where(x => results.Contains(x.Id)).ToList());
        }
Пример #3
0
        public void Insert(List <PokemonResponse> values)
        {
            using (var db = Db)
            {
                var pokemonRepository = new PokemonRepository(db);

                foreach (var value in values)
                {
                    var pokemon = new Pokemon()
                    {
                        Id          = value.Id,
                        Name        = value.Name,
                        BaseAttack  = value.BaseAttack,
                        BaseDefense = value.BaseDefense,
                        BaseHP      = value.BaseHP,
                        BaseSpAtk   = value.BaseSpAtk,
                        BaseSpDef   = value.BaseSpDef,
                        BaseSpeed   = value.BaseSpeed,
                    };
                    pokemonRepository.Insert(pokemon);

                    var pokemonToPokemonSkillRepository = new PokemonToPokemonSkillRepository(db);
                    foreach (var skill in value.Skills)
                    {
                        var newMap = new PokemonToPokemonSkill()
                        {
                            PokemonId      = value.Id,
                            PokemonSkillId = skill.Id
                        };
                        pokemonToPokemonSkillRepository.Insert(newMap);
                    }

                    var pokemonToPokemonTypeRepository = new PokemonToPokemonTypeRepository(db);
                    foreach (var type in value.Types)
                    {
                        var newMap = new PokemonToPokemonType()
                        {
                            PokemonId     = value.Id,
                            PokemonTypeId = type.Id
                        };
                        pokemonToPokemonTypeRepository.Insert(newMap);
                    }
                }
            }
        }
Пример #4
0
        public async void Update(List <PokemonResponse> values)
        {
            using (var db = Db)
            {
                var pokemonRepository = new PokemonRepository(db);

                foreach (var value in values)
                {
                    var pokemon = await pokemonRepository.FindOneById(value.Id);

                    pokemon.Name        = value.Name;
                    pokemon.BaseAttack  = value.BaseAttack;
                    pokemon.BaseDefense = value.BaseDefense;
                    pokemon.BaseHP      = value.BaseHP;
                    pokemon.BaseSpAtk   = value.BaseSpAtk;
                    pokemon.BaseSpDef   = value.BaseSpDef;
                    pokemon.BaseSpeed   = value.BaseSpeed;

                    pokemonRepository.Update(pokemon);

                    var pokemonToPokemonSkillRepository = new PokemonToPokemonSkillRepository(db);
                    var task = await pokemonToPokemonSkillRepository.GetByPokemonId(value.Id);

                    var oldSkills = task.ToList();

                    var newSkills = value.Skills.Where(p => !oldSkills.Any(p2 => p2.PokemonSkillId.Equals(p.Id)));
                    foreach (var skill in newSkills)
                    {
                        var skillMap = new PokemonToPokemonSkill()
                        {
                            PokemonId      = value.Id,
                            PokemonSkillId = skill.Id
                        };
                        pokemonToPokemonSkillRepository.Insert(skillMap);
                    }

                    var removedSkills = oldSkills.Where(p => !value.Skills.Any(p2 => p2.Id.Equals(p.PokemonSkillId)));
                    foreach (var skill in removedSkills)
                    {
                        pokemonToPokemonSkillRepository.Delete(skill);
                    }

                    var pokemonToPokemonTypeRepository = new PokemonToPokemonTypeRepository(db);
                    var oldTypes = await pokemonToPokemonTypeRepository.GetByPokemonId(value.Id);

                    var newTypes = value.Types.Where(p => !oldTypes.Any(p2 => p2.PokemonTypeId.Equals(p.Id)));
                    foreach (var type in newTypes)
                    {
                        var typeMap = new PokemonToPokemonType()
                        {
                            PokemonId     = value.Id,
                            PokemonTypeId = type.Id
                        };
                        pokemonToPokemonTypeRepository.Insert(typeMap);
                    }

                    var removedTypes = oldTypes.Where(p => !value.Types.Any(p2 => p2.Id.Equals(p.PokemonTypeId)));
                    foreach (var type in removedTypes)
                    {
                        pokemonToPokemonTypeRepository.Delete(type);
                    }
                }
            }
        }