Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/pet-type-specialty-no-id-temp #124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
*/

public interface PetTypeRepository {

PetType findById(int id) throws DataAccessException;


PetType findByName(String name) throws DataAccessException;

Collection<PetType> findAll() throws DataAccessException;

void save(PetType petType) throws DataAccessException;

void delete(PetType petType) throws DataAccessException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.springframework.samples.petclinic.repository;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.model.Specialty;
Expand All @@ -27,13 +29,15 @@
*/

public interface SpecialtyRepository {

Specialty findById(int id) throws DataAccessException;

Collection<Specialty> findAll() throws DataAccessException;


List<Specialty> findSpecialtiesByNameIn(Set<String> names);

Collection<Specialty> findAll() throws DataAccessException;

void save(Specialty specialty) throws DataAccessException;

void delete(Specialty specialty) throws DataAccessException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
@Repository
@Profile("jdbc")
public class JdbcPetTypeRepositoryImpl implements PetTypeRepository {

private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

private SimpleJdbcInsert insertPetType;

@Autowired
public JdbcPetTypeRepositoryImpl(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
Expand All @@ -76,7 +76,23 @@ public PetType findById(int id) {
return petType;
}

@Override
@Override
public PetType findByName(String name) throws DataAccessException {
PetType petType;
try {
Map<String, Object> params = new HashMap<>();
params.put("name", name);
petType = this.namedParameterJdbcTemplate.queryForObject(
"SELECT id, name FROM types WHERE name= :name",
params,
BeanPropertyRowMapper.newInstance(PetType.class));
} catch (EmptyResultDataAccessException ex) {
throw new ObjectRetrievalFailureException(PetType.class, name);
}
return petType;
}

@Override
public Collection<PetType> findAll() throws DataAccessException {
Map<String, Object> params = new HashMap<>();
return this.namedParameterJdbcTemplate.query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package org.springframework.samples.petclinic.repository.jdbc;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

import javax.sql.DataSource;

Expand All @@ -43,9 +41,9 @@
@Repository
@Profile("jdbc")
public class JdbcSpecialtyRepositoryImpl implements SpecialtyRepository {

private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

private SimpleJdbcInsert insertSpecialty;

@Autowired
Expand All @@ -72,7 +70,25 @@ public Specialty findById(int id) {
return specialty;
}

@Override
@Override
public List<Specialty> findSpecialtiesByNameIn(Set<String> names) {
List<Specialty> specialties;
try{
String sql = "SELECT id, name FROM specialties WHERE specialties.name IN (:names)";
Map<String, Object> params = new HashMap<>();
params.put("names", names);
specialties = this.namedParameterJdbcTemplate.query(
sql,
params,
new BeanPropertyRowMapper<>(Specialty.class));
} catch (EmptyResultDataAccessException ex){
throw new ObjectRetrievalFailureException(Specialty.class, names);
}

return specialties;
}

@Override
public Collection<Specialty> findAll() throws DataAccessException {
Map<String, Object> params = new HashMap<>();
return this.namedParameterJdbcTemplate.query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ public PetType findById(int id) {
return this.em.find(PetType.class, id);
}

@SuppressWarnings("unchecked")
@Override
public PetType findByName(String name) throws DataAccessException {
return this.em.createQuery("SELECT p FROM PetType p WHERE p.name = :name", PetType.class)
.setParameter("name", name)
.getSingleResult();
}


@SuppressWarnings("unchecked")
@Override
public Collection<PetType> findAll() throws DataAccessException {
return this.em.createQuery("SELECT ptype FROM PetType ptype").getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.springframework.samples.petclinic.repository.jpa;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
Expand Down Expand Up @@ -44,7 +46,15 @@ public Specialty findById(int id) {
return this.em.find(Specialty.class, id);
}

@SuppressWarnings("unchecked")
@Override
public List<Specialty> findSpecialtiesByNameIn(Set<String> names) {
final String jpql = "SELECT s FROM Specialty s WHERE s.name IN :names";
return em.createQuery(jpql, Specialty.class)
.setParameter("names", names)
.getResultList();
}

@SuppressWarnings("unchecked")
@Override
public Collection<Specialty> findAll() throws DataAccessException {
return this.em.createQuery("SELECT s FROM Specialty s").getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.samples.petclinic.mapper.VisitMapper;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.rest.api.OwnersApi;
import org.springframework.samples.petclinic.rest.dto.*;
Expand All @@ -38,7 +39,6 @@

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
* @author Vitaliy Fedoriv
Expand Down Expand Up @@ -140,6 +140,8 @@ public ResponseEntity<PetDto> addPetToOwner(Integer ownerId, PetFieldsDto petFie
Owner owner = new Owner();
owner.setId(ownerId);
pet.setOwner(owner);
PetType petType = this.clinicService.findPetTypeByName(pet.getType().getName());
pet.setType(petType);
this.clinicService.savePet(pet);
PetDto petDto = petMapper.toPetDto(pet);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pets/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Vitaliy Fedoriv
Expand Down Expand Up @@ -78,6 +79,10 @@ public ResponseEntity<VetDto> getVet(Integer vetId) {
public ResponseEntity<VetDto> addVet(VetDto vetDto) {
HttpHeaders headers = new HttpHeaders();
Vet vet = vetMapper.toVet(vetDto);
if(vet.getNrOfSpecialties() > 0){
List<Specialty> vetSpecialities = this.clinicService.findSpecialtiesByNameIn(vet.getSpecialties().stream().map(Specialty::getName).collect(Collectors.toSet()));
vet.setSpecialties(vetSpecialities);
}
this.clinicService.saveVet(vet);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/vets/{id}").buildAndExpand(vet.getId()).toUri());
return new ResponseEntity<>(vetMapper.toVetDto(vet), headers, HttpStatus.CREATED);
Expand All @@ -96,6 +101,10 @@ public ResponseEntity<VetDto> updateVet(Integer vetId,VetDto vetDto) {
for (Specialty spec : specialtyMapper.toSpecialtys(vetDto.getSpecialties())) {
currentVet.addSpecialty(spec);
}
if(currentVet.getNrOfSpecialties() > 0){
List<Specialty> vetSpecialities = this.clinicService.findSpecialtiesByNameIn(currentVet.getSpecialties().stream().map(Specialty::getName).collect(Collectors.toSet()));
currentVet.setSpecialties(vetSpecialities);
}
this.clinicService.saveVet(currentVet);
return new ResponseEntity<>(vetMapper.toVetDto(currentVet), HttpStatus.NO_CONTENT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.springframework.samples.petclinic.service;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.model.Owner;
Expand All @@ -25,7 +27,6 @@
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.model.Visit;


/**
* Mostly used as a facade so all controllers have a single point of entry
*
Expand All @@ -44,13 +45,11 @@ public interface ClinicService {
Collection<Visit> findAllVisits() throws DataAccessException;
void saveVisit(Visit visit) throws DataAccessException;
void deleteVisit(Visit visit) throws DataAccessException;

Vet findVetById(int id) throws DataAccessException;
Collection<Vet> findVets() throws DataAccessException;
Collection<Vet> findAllVets() throws DataAccessException;
void saveVet(Vet vet) throws DataAccessException;
void deleteVet(Vet vet) throws DataAccessException;

Owner findOwnerById(int id) throws DataAccessException;
Collection<Owner> findAllOwners() throws DataAccessException;
void saveOwner(Owner owner) throws DataAccessException;
Expand All @@ -62,10 +61,12 @@ public interface ClinicService {
Collection<PetType> findPetTypes() throws DataAccessException;
void savePetType(PetType petType) throws DataAccessException;
void deletePetType(PetType petType) throws DataAccessException;

Specialty findSpecialtyById(int specialtyId);
Collection<Specialty> findAllSpecialties() throws DataAccessException;
void saveSpecialty(Specialty specialty) throws DataAccessException;
void deleteSpecialty(Specialty specialty) throws DataAccessException;

List<Specialty> findSpecialtiesByNameIn(Set<String> names) throws DataAccessException;

PetType findPetTypeByName(String name) throws DataAccessException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package org.springframework.samples.petclinic.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.orm.ObjectRetrievalFailureException;
Expand Down Expand Up @@ -151,7 +153,7 @@ public void deleteOwner(Owner owner) throws DataAccessException {
}

@Override
@Transactional(readOnly = true)
@Transactional(readOnly = true)
public PetType findPetTypeById(int petTypeId) {
PetType petType = null;
try {
Expand Down Expand Up @@ -248,7 +250,6 @@ public Pet findPetById(int id) throws DataAccessException {
@Transactional
public void savePet(Pet pet) throws DataAccessException {
petRepository.save(pet);

}

@Override
Expand All @@ -260,7 +261,6 @@ public void saveVisit(Visit visit) throws DataAccessException {

@Override
@Transactional(readOnly = true)
@Cacheable(value = "vets")
public Collection<Vet> findVets() throws DataAccessException {
return vetRepository.findAll();
}
Expand All @@ -284,7 +284,29 @@ public Collection<Visit> findVisitsByPetId(int petId) {
return visitRepository.findByPetId(petId);
}

@Override
@Transactional(readOnly = true)
public List<Specialty> findSpecialtiesByNameIn(Set<String> names){
List<Specialty> specialties = new ArrayList<>();
try {
specialties = specialtyRepository.findSpecialtiesByNameIn(names);
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
// just ignore not found exceptions for Jdbc/Jpa realization
return specialties;
}
return specialties;
}



@Override
@Transactional(readOnly = true)
public PetType findPetTypeByName(String name){
PetType petType;
try {
petType = petTypeRepository.findByName(name);
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
// just ignore not found exceptions for Jdbc/Jpa realization
return null;
}
return petType;
}
}
Loading
Loading