mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-26 01:15:50 +00:00
Move VetController logic into the application layer
This commit is contained in:
parent
e1b9a6a18e
commit
f360094b99
3 changed files with 45 additions and 14 deletions
|
@ -0,0 +1,29 @@
|
||||||
|
package org.springframework.samples.petclinic.application;
|
||||||
|
|
||||||
|
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.infrastructure.persistence.vet.Vet;
|
||||||
|
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetRepository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class VetService {
|
||||||
|
|
||||||
|
private final VetRepository vetRepository;
|
||||||
|
|
||||||
|
public VetService(VetRepository vetRepository) {
|
||||||
|
this.vetRepository = vetRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Page<Vet> getVetPage(int page, int pageSize) {
|
||||||
|
Pageable pageable = PageRequest.of(page - 1, pageSize);
|
||||||
|
return this.vetRepository.findAll(pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Vet> getVets() {
|
||||||
|
return this.vetRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,13 +15,12 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.infrastructure.controller;
|
package org.springframework.samples.petclinic.infrastructure.controller;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.samples.petclinic.application.VetService;
|
||||||
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.samples.petclinic.infrastructure.view.Vets;
|
import org.springframework.samples.petclinic.infrastructure.view.Vets;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
@ -38,10 +37,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
@Controller
|
@Controller
|
||||||
class VetController {
|
class VetController {
|
||||||
|
|
||||||
private final VetRepository vetRepository;
|
private final VetService vetService;
|
||||||
|
|
||||||
public VetController(VetRepository clinicService) {
|
public VetController(VetService vetService) {
|
||||||
this.vetRepository = clinicService;
|
this.vetService = vetService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/vets.html")
|
@GetMapping("/vets.html")
|
||||||
|
@ -66,8 +65,7 @@ class VetController {
|
||||||
|
|
||||||
private Page<Vet> findPaginated(int page) {
|
private Page<Vet> findPaginated(int page) {
|
||||||
int pageSize = 5;
|
int pageSize = 5;
|
||||||
Pageable pageable = PageRequest.of(page - 1, pageSize);
|
return vetService.getVetPage(page, pageSize);
|
||||||
return vetRepository.findAll(pageable);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping({ "/vets" })
|
@GetMapping({ "/vets" })
|
||||||
|
@ -75,7 +73,7 @@ class VetController {
|
||||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||||
// objects so it is simpler for JSon/Object mapping
|
// objects so it is simpler for JSon/Object mapping
|
||||||
Vets vets = new Vets();
|
Vets vets = new Vets();
|
||||||
vets.getVetList().addAll(this.vetRepository.findAll());
|
vets.getVetList().addAll(vetService.getVets());
|
||||||
return vets;
|
return vets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.infrastructure.controller;
|
package org.springframework.samples.petclinic.infrastructure.controller;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import org.assertj.core.util.Lists;
|
import org.assertj.core.util.Lists;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -25,9 +26,9 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.data.domain.PageImpl;
|
import org.springframework.data.domain.PageImpl;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.samples.petclinic.application.VetService;
|
||||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Specialty;
|
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Specialty;
|
||||||
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.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.ResultActions;
|
import org.springframework.test.web.servlet.ResultActions;
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
@ -48,7 +49,7 @@ class VetControllerTests {
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private VetRepository vets;
|
private VetService vetService;
|
||||||
|
|
||||||
private Vet james() {
|
private Vet james() {
|
||||||
Vet james = new Vet();
|
Vet james = new Vet();
|
||||||
|
@ -72,9 +73,12 @@ class VetControllerTests {
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
given(this.vets.findAll()).willReturn(Lists.newArrayList(james(), helen()));
|
ArrayList<Vet> vetsList = Lists.newArrayList(james(), helen());
|
||||||
given(this.vets.findAll(any(Pageable.class)))
|
|
||||||
.willReturn(new PageImpl<Vet>(Lists.newArrayList(james(), helen())));
|
given(this.vetService.getVets()).willReturn(vetsList);
|
||||||
|
|
||||||
|
given(this.vetService.getVetPage(1, 5))
|
||||||
|
.willReturn(new PageImpl<>(vetsList));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue