Separate the VetRepository from the Spring Data Repository

This commit is contained in:
Ismail Ismailov 2023-04-27 14:40:20 +02:00
parent f360094b99
commit ea698ba161
9 changed files with 53 additions and 24 deletions

View file

@ -1,5 +0,0 @@
package org.springframework.samples.petclinic.application;
public class UseCase {
}

View file

@ -2,10 +2,8 @@ package org.springframework.samples.petclinic.application;
import java.util.Collection; import java.util.Collection;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.samples.petclinic.domain.VetRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet; import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@ -18,12 +16,11 @@ public class VetService {
} }
public Page<Vet> getVetPage(int page, int pageSize) { public Page<Vet> getVetPage(int page, int pageSize) {
Pageable pageable = PageRequest.of(page - 1, pageSize); return this.vetRepository.getVetPage(page, pageSize);
return this.vetRepository.findAll(pageable);
} }
public Collection<Vet> getVets() { public Collection<Vet> getVets() {
return this.vetRepository.findAll(); return this.vetRepository.getVets();
} }
} }

View file

@ -1,5 +0,0 @@
package org.springframework.samples.petclinic.domain;
public class MyDomain {
}

View file

@ -0,0 +1,13 @@
package org.springframework.samples.petclinic.domain;
import java.util.Collection;
import org.springframework.data.domain.Page;
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
public interface VetRepository {
Page<Vet> getVetPage(int page, int pageSize);
Collection<Vet> getVets();
}

View file

@ -35,7 +35,7 @@ import java.util.Collection;
* @author Sam Brannen * @author Sam Brannen
* @author Michael Isvy * @author Michael Isvy
*/ */
public interface VetRepository extends Repository<Vet, Integer> { public interface VetDataRepository extends Repository<Vet, Integer> {
/** /**
* Retrieve all <code>Vet</code>s from the data store. * Retrieve all <code>Vet</code>s from the data store.

View file

@ -0,0 +1,30 @@
package org.springframework.samples.petclinic.infrastructure.persistence.vet;
import java.util.Collection;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.samples.petclinic.domain.VetRepository;
import org.springframework.stereotype.Repository;
@Repository
public class VetRepositoryImpl implements VetRepository {
private final VetDataRepository vetDataRepository;
public VetRepositoryImpl(VetDataRepository vetDataRepository) {
this.vetDataRepository = vetDataRepository;
}
@Override
public Page<Vet> getVetPage(int page, int pageSize) {
Pageable pageable = PageRequest.of(page - 1, pageSize);
return this.vetDataRepository.findAll(pageable);
}
@Override
public Collection<Vet> getVets() {
return this.vetDataRepository.findAll();
}
}

View file

@ -27,7 +27,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity; import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetRepository; import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetDataRepository;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ -37,7 +37,7 @@ class PetClinicIntegrationTests {
int port; int port;
@Autowired @Autowired
private VetRepository vets; private VetDataRepository vets;
@Autowired @Autowired
private RestTemplateBuilder builder; private RestTemplateBuilder builder;

View file

@ -77,8 +77,7 @@ class VetControllerTests {
given(this.vetService.getVets()).willReturn(vetsList); given(this.vetService.getVets()).willReturn(vetsList);
given(this.vetService.getVetPage(1, 5)) given(this.vetService.getVetPage(1, 5)).willReturn(new PageImpl<>(vetsList));
.willReturn(new PageImpl<>(vetsList));
} }

View file

@ -35,7 +35,7 @@ import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.owner.PetType; import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.owner.Visit; import org.springframework.samples.petclinic.owner.Visit;
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet; import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetRepository; import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetDataRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -67,7 +67,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Michael Isvy * @author Michael Isvy
* @author Dave Syer * @author Dave Syer
*/ */
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class)) @DataJpaTest
// Ensure that if the mysql profile is active we connect to the real database: // Ensure that if the mysql profile is active we connect to the real database:
@AutoConfigureTestDatabase(replace = Replace.NONE) @AutoConfigureTestDatabase(replace = Replace.NONE)
// @TestPropertySource("/application-postgres.properties") // @TestPropertySource("/application-postgres.properties")
@ -77,7 +77,7 @@ class ClinicServiceTests {
protected OwnerRepository owners; protected OwnerRepository owners;
@Autowired @Autowired
protected VetRepository vets; protected VetDataRepository vets;
Pageable pageable; Pageable pageable;