Commit 21cb4904 authored by dnokov's avatar dnokov
Browse files

Adding findSpecialtiesByName and findPetTypeByName

adding to respective repository interfaces and to service interface/implementation layer
parent ed80d7cf
Branches
2 merge requests!3Forking spring-petclinic-rest attempt #2,!2Forked spring-petclinic-rest
Showing with 62 additions and 19 deletions
......@@ -27,13 +27,15 @@ import org.springframework.samples.petclinic.model.PetType;
*/
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;
}
......@@ -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;
......@@ -27,13 +29,15 @@ import org.springframework.samples.petclinic.model.Specialty;
*/
public interface SpecialtyRepository {
Specialty findById(int id) throws DataAccessException;
Collection<Specialty> findAll() throws DataAccessException;
List<Specialty> findByNameIn(Set<String> names);
Collection<Specialty> findAll() throws DataAccessException;
void save(Specialty specialty) throws DataAccessException;
void delete(Specialty specialty) throws DataAccessException;
}
......@@ -24,6 +24,7 @@ import org.springframework.samples.petclinic.mapper.PetMapper;
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.*;
......@@ -38,7 +39,6 @@ import jakarta.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
/**
* @author Vitaliy Fedoriv
......@@ -140,6 +140,8 @@ public class OwnerRestController implements OwnersApi {
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}")
......
......@@ -22,6 +22,7 @@ import org.springframework.samples.petclinic.mapper.SpecialtyMapper;
import org.springframework.samples.petclinic.mapper.VetMapper;
import org.springframework.samples.petclinic.model.Specialty;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.rest.api.VetsApi;
import org.springframework.samples.petclinic.rest.dto.VetDto;
import org.springframework.samples.petclinic.service.ClinicService;
......@@ -31,7 +32,10 @@ import org.springframework.web.util.UriComponentsBuilder;
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author Vitaliy Fedoriv
......@@ -78,6 +82,10 @@ public class VetRestController implements VetsApi {
public ResponseEntity<VetDto> addVet(VetDto vetDto) {
HttpHeaders headers = new HttpHeaders();
Vet vet = vetMapper.toVet(vetDto);
if(vet.getNrOfSpecialties() > 0){
List<Specialty> vetSpecialities = this.clinicService.findSpecialtiesByName(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);
......@@ -96,6 +104,10 @@ public class VetRestController implements VetsApi {
for (Specialty spec : specialtyMapper.toSpecialtys(vetDto.getSpecialties())) {
currentVet.addSpecialty(spec);
}
if(currentVet.getNrOfSpecialties() > 0){
List<Specialty> vetSpecialities = this.clinicService.findSpecialtiesByName(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);
}
......
......@@ -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;
......@@ -25,7 +27,6 @@ import org.springframework.samples.petclinic.model.Specialty;
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
*
......@@ -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;
......@@ -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> findSpecialtiesByName(Set<String> names) throws DataAccessException;
PetType findPetTypeByName(String name) throws DataAccessException;
}
......@@ -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;
......@@ -151,7 +153,7 @@ public class ClinicServiceImpl implements ClinicService {
}
@Override
@Transactional(readOnly = true)
@Transactional(readOnly = true)
public PetType findPetTypeById(int petTypeId) {
PetType petType = null;
try {
......@@ -248,7 +250,6 @@ public class ClinicServiceImpl implements ClinicService {
@Transactional
public void savePet(Pet pet) throws DataAccessException {
petRepository.save(pet);
}
@Override
......@@ -260,7 +261,6 @@ public class ClinicServiceImpl implements ClinicService {
@Override
@Transactional(readOnly = true)
@Cacheable(value = "vets")
public Collection<Vet> findVets() throws DataAccessException {
return vetRepository.findAll();
}
......@@ -284,7 +284,29 @@ public class ClinicServiceImpl implements ClinicService {
return visitRepository.findByPetId(petId);
}
@Override
@Transactional(readOnly = true)
public List<Specialty> findSpecialtiesByName(Set<String> names){
List<Specialty> specialties = new ArrayList<>();
try {
specialties = specialtyRepository.findByNameIn(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;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment