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); } }
public async Task <PokemonResponse> Get(int id) { using (var db = Db) { var pokemonRepository = new PokemonRepository(db); var pokemon = await pokemonRepository.FindOneById(id); var pokemonResponse = new PokemonResponse() { Id = pokemon.Id, Name = pokemon.Name, BaseAttack = pokemon.BaseAttack, BaseDefense = pokemon.BaseDefense, BaseHP = pokemon.BaseHP, BaseSpAtk = pokemon.BaseSpAtk, BaseSpDef = pokemon.BaseSpDef, BaseSpeed = pokemon.BaseSpeed, Skills = await GetPokemonSkills(pokemon.Id, db), Types = await GetPokemonTypes(pokemon.Id, db) }; return(pokemonResponse); } }
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); } } } }