Skip to content
Snippets Groups Projects
Commit 078bdc6c authored by Antoine Rey's avatar Antoine Rey
Browse files

#149 JdbcPetRepositoryImpl:: findById() simplification

parent 4c722465
Branches
No related tags found
No related merge requests found
...@@ -82,26 +82,16 @@ public class JdbcPetRepositoryImpl implements PetRepository { ...@@ -82,26 +82,16 @@ public class JdbcPetRepositoryImpl implements PetRepository {
@Override @Override
public Pet findById(int id) throws DataAccessException { public Pet findById(int id) throws DataAccessException {
JdbcPet pet; Integer ownerId;
try { try {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("id", id); params.put("id", id);
pet = this.namedParameterJdbcTemplate.queryForObject( ownerId = this.namedParameterJdbcTemplate.queryForObject("SELECT owner_id FROM pets WHERE id=:id", params, Integer.class);
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
params,
new JdbcPetRowMapper());
} catch (EmptyResultDataAccessException ex) { } catch (EmptyResultDataAccessException ex) {
throw new ObjectRetrievalFailureException(Pet.class, id); throw new ObjectRetrievalFailureException(Pet.class, id);
} }
Owner owner = this.ownerRepository.findById(pet.getOwnerId()); Owner owner = this.ownerRepository.findById(ownerId);
owner.addPet(pet); return EntityUtils.getById(owner.getPets(), Pet.class, id);
pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));
List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
for (Visit visit : visits) {
pet.addVisit(visit);
}
return pet;
} }
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment