mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 07:15:49 +00:00
Implements DTOs and Services
This commit is contained in:
parent
45e0b07d6f
commit
6d5b55fc10
23 changed files with 111 additions and 83 deletions
|
@ -45,6 +45,7 @@ class OwnerController {
|
||||||
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||||
|
|
||||||
private final OwnerService ownerService;
|
private final OwnerService ownerService;
|
||||||
|
|
||||||
private final VisitService visitService;
|
private final VisitService visitService;
|
||||||
|
|
||||||
OwnerController(OwnerService ownerService, VisitService visitService) {
|
OwnerController(OwnerService ownerService, VisitService visitService) {
|
||||||
|
|
|
@ -45,7 +45,9 @@ class PetController {
|
||||||
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
|
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
|
||||||
|
|
||||||
private final OwnerService ownerService;
|
private final OwnerService ownerService;
|
||||||
|
|
||||||
private final PetService petService;
|
private final PetService petService;
|
||||||
|
|
||||||
private final PetTypeService petTypeService;
|
private final PetTypeService petTypeService;
|
||||||
|
|
||||||
PetController(OwnerService ownerService, PetService petService, PetTypeService petTypeService) {
|
PetController(OwnerService ownerService, PetService petService, PetTypeService petTypeService) {
|
||||||
|
@ -54,7 +56,6 @@ class PetController {
|
||||||
this.petTypeService = petTypeService;
|
this.petTypeService = petTypeService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ModelAttribute("types")
|
@ModelAttribute("types")
|
||||||
public Collection<PetTypeDTO> populatePetTypes() {
|
public Collection<PetTypeDTO> populatePetTypes() {
|
||||||
return this.petTypeService.findPetTypes();
|
return this.petTypeService.findPetTypes();
|
||||||
|
|
|
@ -40,7 +40,6 @@ class VetController {
|
||||||
this.vetService = vetService;
|
this.vetService = vetService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/vets.html")
|
@GetMapping("/vets.html")
|
||||||
public String showVetList(Map<String, Object> model) {
|
public String showVetList(Map<String, Object> model) {
|
||||||
// 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
|
||||||
|
|
|
@ -23,7 +23,6 @@ import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
import org.springframework.samples.petclinic.dto.VisitDTO;
|
import org.springframework.samples.petclinic.dto.VisitDTO;
|
||||||
import org.springframework.samples.petclinic.service.PetService;
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
import org.springframework.samples.petclinic.service.VisitService;
|
import org.springframework.samples.petclinic.service.VisitService;
|
||||||
import org.springframework.samples.petclinic.visit.Visit;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
|
@ -45,6 +44,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||||
class VisitController {
|
class VisitController {
|
||||||
|
|
||||||
private final VisitService visitService;
|
private final VisitService visitService;
|
||||||
|
|
||||||
private final PetService petService;
|
private final PetService petService;
|
||||||
|
|
||||||
VisitController(VisitService visitService, PetService petService) {
|
VisitController(VisitService visitService, PetService petService) {
|
||||||
|
@ -52,7 +52,6 @@ class VisitController {
|
||||||
this.petService = petService;
|
this.petService = petService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@InitBinder
|
@InitBinder
|
||||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||||
dataBinder.setDisallowedFields("id");
|
dataBinder.setDisallowedFields("id");
|
||||||
|
@ -62,7 +61,7 @@ class VisitController {
|
||||||
* Called before each and every @RequestMapping annotated method. 2 goals: - Make sure
|
* Called before each and every @RequestMapping annotated method. 2 goals: - Make sure
|
||||||
* we always have fresh data - Since we do not use the session scope, make sure that
|
* we always have fresh data - Since we do not use the session scope, make sure that
|
||||||
* Pet object always has an id (Even though id is not part of the form fields)
|
* Pet object always has an id (Even though id is not part of the form fields)
|
||||||
* @param petId
|
* @param petId Pet identification
|
||||||
* @return Pet
|
* @return Pet
|
||||||
*/
|
*/
|
||||||
@ModelAttribute("visit")
|
@ModelAttribute("visit")
|
||||||
|
|
|
@ -18,8 +18,8 @@ package org.springframework.samples.petclinic.dto;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple Data Transfert Object with an id property. Used as a base class for DTO
|
* Simple Data Transfert Object with an id property. Used as a base class for DTO needing
|
||||||
* needing this property.
|
* this property.
|
||||||
*
|
*
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
|
@ -38,4 +38,5 @@ public class BaseDTO implements Serializable {
|
||||||
public boolean isNew() {
|
public boolean isNew() {
|
||||||
return this.id == null;
|
return this.id == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,10 +15,9 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.dto;
|
package org.springframework.samples.petclinic.dto;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple Data Transfert Object with a name property to <code>BaseDTO</code>. Used as
|
* Simple Data Transfert Object with a name property to <code>BaseDTO</code>. Used as a
|
||||||
* a base class for DTOs needing these properties.
|
* base class for DTOs needing these properties.
|
||||||
*
|
*
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -73,7 +73,7 @@ public class OwnerDTO extends PersonDTO {
|
||||||
return this.pets;
|
return this.pets;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setPetsInternal(Set<PetDTO> pets) {
|
public void setPetsInternal(Set<PetDTO> pets) {
|
||||||
this.pets = pets;
|
this.pets = pets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ public class PersonDTO extends BaseDTO {
|
||||||
|
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
|
@ -44,4 +45,5 @@ public class PersonDTO extends BaseDTO {
|
||||||
public void setLastName(String lastName) {
|
public void setLastName(String lastName) {
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@ package org.springframework.samples.petclinic.dto;
|
||||||
import org.springframework.beans.support.MutableSortDefinition;
|
import org.springframework.beans.support.MutableSortDefinition;
|
||||||
import org.springframework.beans.support.PropertyComparator;
|
import org.springframework.beans.support.PropertyComparator;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.samples.petclinic.owner.PetType;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@ -33,7 +32,7 @@ public class PetDTO extends NamedDTO {
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||||
private LocalDate birthDate;
|
private LocalDate birthDate;
|
||||||
|
|
||||||
private PetType type;
|
private PetTypeDTO type;
|
||||||
|
|
||||||
private OwnerDTO owner;
|
private OwnerDTO owner;
|
||||||
|
|
||||||
|
@ -47,11 +46,11 @@ public class PetDTO extends NamedDTO {
|
||||||
return this.birthDate;
|
return this.birthDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PetType getType() {
|
public PetTypeDTO getType() {
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(PetType type) {
|
public void setType(PetTypeDTO type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@ package org.springframework.samples.petclinic.repository;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.dao.DataAccessException;
|
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.samples.petclinic.vet.Vet;
|
import org.springframework.samples.petclinic.vet.Vet;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
@ -42,6 +41,6 @@ public interface VetRepository extends Repository<Vet, Integer> {
|
||||||
*/
|
*/
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
@Cacheable("vets")
|
@Cacheable("vets")
|
||||||
Collection<Vet> findAll() throws DataAccessException;
|
Collection<Vet> findAll();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,6 @@ package org.springframework.samples.petclinic.repository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.dao.DataAccessException;
|
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.samples.petclinic.model.BaseEntity;
|
import org.springframework.samples.petclinic.model.BaseEntity;
|
||||||
import org.springframework.samples.petclinic.visit.Visit;
|
import org.springframework.samples.petclinic.visit.Visit;
|
||||||
|
@ -40,7 +39,7 @@ public interface VisitRepository extends Repository<Visit, Integer> {
|
||||||
* @param visit the <code>Visit</code> to save
|
* @param visit the <code>Visit</code> to save
|
||||||
* @see BaseEntity#isNew
|
* @see BaseEntity#isNew
|
||||||
*/
|
*/
|
||||||
void save(Visit visit) throws DataAccessException;
|
void save(Visit visit);
|
||||||
|
|
||||||
List<Visit> findByPetId(Integer petId);
|
List<Visit> findByPetId(Integer petId);
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,14 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
public interface BaseService<E,D> {
|
public interface BaseService<E, D> {
|
||||||
|
|
||||||
public E dtoToEntity(D dto);
|
public E dtoToEntity(D dto);
|
||||||
|
|
||||||
public D entityToDTO(E entity);
|
public D entityToDTO(E entity);
|
||||||
|
|
||||||
public Collection<D> entitiesToDTOS(Collection<E> entities);
|
public Collection<D> entitiesToDTOS(Collection<E> entities);
|
||||||
|
|
||||||
public Collection<E> dtosToEntities(Collection<D> dtos);
|
public Collection<E> dtosToEntities(Collection<D> dtos);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,10 @@ import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
@Service("OwnerService")
|
@Service("OwnerService")
|
||||||
public class OwnerService implements BaseService<Owner,OwnerDTO>{
|
public class OwnerService implements BaseService<Owner, OwnerDTO> {
|
||||||
|
|
||||||
private final OwnerRepository ownerRepository;
|
private final OwnerRepository ownerRepository;
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
public OwnerService(OwnerRepository ownerRepository) {
|
public OwnerService(OwnerRepository ownerRepository) {
|
||||||
|
@ -33,7 +34,7 @@ public class OwnerService implements BaseService<Owner,OwnerDTO>{
|
||||||
public Collection<OwnerDTO> entitiesToDTOS(Collection<Owner> entities) {
|
public Collection<OwnerDTO> entitiesToDTOS(Collection<Owner> entities) {
|
||||||
Collection<OwnerDTO> dtos = new HashSet<>();
|
Collection<OwnerDTO> dtos = new HashSet<>();
|
||||||
|
|
||||||
for(Owner entity: entities) {
|
for (Owner entity : entities) {
|
||||||
dtos.add(entityToDTO(entity));
|
dtos.add(entityToDTO(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +45,7 @@ public class OwnerService implements BaseService<Owner,OwnerDTO>{
|
||||||
public Collection<Owner> dtosToEntities(Collection<OwnerDTO> dtos) {
|
public Collection<Owner> dtosToEntities(Collection<OwnerDTO> dtos) {
|
||||||
Collection<Owner> entities = new HashSet<>();
|
Collection<Owner> entities = new HashSet<>();
|
||||||
|
|
||||||
for(OwnerDTO dto: dtos) {
|
for (OwnerDTO dto : dtos) {
|
||||||
entities.add(dtoToEntity(dto));
|
entities.add(dtoToEntity(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,4 +66,5 @@ public class OwnerService implements BaseService<Owner,OwnerDTO>{
|
||||||
Owner owner = ownerRepository.findById(ownerId);
|
Owner owner = ownerRepository.findById(ownerId);
|
||||||
return entityToDTO(owner);
|
return entityToDTO(owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,10 @@ import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
@Service("PetService")
|
@Service("PetService")
|
||||||
public class PetService implements BaseService<Pet,PetDTO>{
|
public class PetService implements BaseService<Pet, PetDTO> {
|
||||||
|
|
||||||
private final PetRepository petRepository;
|
private final PetRepository petRepository;
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
public PetService(PetRepository petRepository) {
|
public PetService(PetRepository petRepository) {
|
||||||
|
@ -32,7 +34,7 @@ public class PetService implements BaseService<Pet,PetDTO>{
|
||||||
public Collection<PetDTO> entitiesToDTOS(Collection<Pet> entities) {
|
public Collection<PetDTO> entitiesToDTOS(Collection<Pet> entities) {
|
||||||
Collection<PetDTO> dtos = new HashSet<>();
|
Collection<PetDTO> dtos = new HashSet<>();
|
||||||
|
|
||||||
for(Pet entity:entities) {
|
for (Pet entity : entities) {
|
||||||
dtos.add(entityToDTO(entity));
|
dtos.add(entityToDTO(entity));
|
||||||
}
|
}
|
||||||
return dtos;
|
return dtos;
|
||||||
|
@ -42,7 +44,7 @@ public class PetService implements BaseService<Pet,PetDTO>{
|
||||||
public Collection<Pet> dtosToEntities(Collection<PetDTO> dtos) {
|
public Collection<Pet> dtosToEntities(Collection<PetDTO> dtos) {
|
||||||
Collection<Pet> entities = new HashSet<>();
|
Collection<Pet> entities = new HashSet<>();
|
||||||
|
|
||||||
for(PetDTO dto: dtos) {
|
for (PetDTO dto : dtos) {
|
||||||
entities.add(dtoToEntity(dto));
|
entities.add(dtoToEntity(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,4 +62,3 @@ public class PetService implements BaseService<Pet,PetDTO>{
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,10 @@ import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
@Service("PerTypeService")
|
@Service("PerTypeService")
|
||||||
public class PetTypeService implements BaseService<PetType, PetTypeDTO>{
|
public class PetTypeService implements BaseService<PetType, PetTypeDTO> {
|
||||||
|
|
||||||
private final PetRepository petRepository;
|
private final PetRepository petRepository;
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
public PetTypeService(PetRepository petRepository) {
|
public PetTypeService(PetRepository petRepository) {
|
||||||
|
@ -32,7 +34,7 @@ public class PetTypeService implements BaseService<PetType, PetTypeDTO>{
|
||||||
public Collection<PetTypeDTO> entitiesToDTOS(Collection<PetType> entities) {
|
public Collection<PetTypeDTO> entitiesToDTOS(Collection<PetType> entities) {
|
||||||
Collection<PetTypeDTO> dtos = new HashSet<>();
|
Collection<PetTypeDTO> dtos = new HashSet<>();
|
||||||
|
|
||||||
for(PetType entity : entities) {
|
for (PetType entity : entities) {
|
||||||
dtos.add(entityToDTO(entity));
|
dtos.add(entityToDTO(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,16 +45,15 @@ public class PetTypeService implements BaseService<PetType, PetTypeDTO>{
|
||||||
public Collection<PetType> dtosToEntities(Collection<PetTypeDTO> dtos) {
|
public Collection<PetType> dtosToEntities(Collection<PetTypeDTO> dtos) {
|
||||||
Collection<PetType> entities = new HashSet<>();
|
Collection<PetType> entities = new HashSet<>();
|
||||||
|
|
||||||
for(PetTypeDTO dto: dtos) {
|
for (PetTypeDTO dto : dtos) {
|
||||||
entities.add(dtoToEntity(dto));
|
entities.add(dtoToEntity(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Collection<PetTypeDTO> findPetTypes() {
|
public Collection<PetTypeDTO> findPetTypes() {
|
||||||
Collection<PetType> petTypes = petRepository.findPetTypes();
|
Collection<PetType> petTypes = petRepository.findPetTypes();
|
||||||
return entitiesToDTOS(petTypes);
|
return entitiesToDTOS(petTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import java.util.HashSet;
|
||||||
|
|
||||||
@Service("SpecialityService")
|
@Service("SpecialityService")
|
||||||
public class SpecialityService implements BaseService<Specialty, SpecialtyDTO> {
|
public class SpecialityService implements BaseService<Specialty, SpecialtyDTO> {
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -26,7 +27,7 @@ public class SpecialityService implements BaseService<Specialty, SpecialtyDTO> {
|
||||||
public Collection<SpecialtyDTO> entitiesToDTOS(Collection<Specialty> entities) {
|
public Collection<SpecialtyDTO> entitiesToDTOS(Collection<Specialty> entities) {
|
||||||
Collection<SpecialtyDTO> dtos = new HashSet<>();
|
Collection<SpecialtyDTO> dtos = new HashSet<>();
|
||||||
|
|
||||||
for(Specialty entity:entities) {
|
for (Specialty entity : entities) {
|
||||||
dtos.add(entityToDTO(entity));
|
dtos.add(entityToDTO(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,10 +38,11 @@ public class SpecialityService implements BaseService<Specialty, SpecialtyDTO> {
|
||||||
public Collection<Specialty> dtosToEntities(Collection<SpecialtyDTO> dtos) {
|
public Collection<Specialty> dtosToEntities(Collection<SpecialtyDTO> dtos) {
|
||||||
Collection<Specialty> entities = new HashSet<>();
|
Collection<Specialty> entities = new HashSet<>();
|
||||||
|
|
||||||
for(SpecialtyDTO dto: dtos) {
|
for (SpecialtyDTO dto : dtos) {
|
||||||
entities.add(dtoToEntity(dto));
|
entities.add(dtoToEntity(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,10 @@ import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
@Service("VetService")
|
@Service("VetService")
|
||||||
public class VetService implements BaseService<Vet,VetDTO>{
|
public class VetService implements BaseService<Vet, VetDTO> {
|
||||||
|
|
||||||
private final VetRepository vetRepository;
|
private final VetRepository vetRepository;
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
public VetService(VetRepository vetRepository) {
|
public VetService(VetRepository vetRepository) {
|
||||||
|
@ -33,7 +34,7 @@ public class VetService implements BaseService<Vet,VetDTO>{
|
||||||
public Collection<VetDTO> entitiesToDTOS(Collection<Vet> entities) {
|
public Collection<VetDTO> entitiesToDTOS(Collection<Vet> entities) {
|
||||||
Collection<VetDTO> dtos = new HashSet<>();
|
Collection<VetDTO> dtos = new HashSet<>();
|
||||||
|
|
||||||
for(Vet entity: entities) {
|
for (Vet entity : entities) {
|
||||||
dtos.add(entityToDTO(entity));
|
dtos.add(entityToDTO(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,14 +45,13 @@ public class VetService implements BaseService<Vet,VetDTO>{
|
||||||
public Collection<Vet> dtosToEntities(Collection<VetDTO> dtos) {
|
public Collection<Vet> dtosToEntities(Collection<VetDTO> dtos) {
|
||||||
Collection<Vet> entities = new HashSet<>();
|
Collection<Vet> entities = new HashSet<>();
|
||||||
|
|
||||||
for(VetDTO dto: dtos) {
|
for (VetDTO dto : dtos) {
|
||||||
entities.add(dtoToEntity(dto));
|
entities.add(dtoToEntity(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Collection<VetDTO> findAll() {
|
public Collection<VetDTO> findAll() {
|
||||||
Collection<Vet> vets = vetRepository.findAll();
|
Collection<Vet> vets = vetRepository.findAll();
|
||||||
return entitiesToDTOS(vets);
|
return entitiesToDTOS(vets);
|
||||||
|
|
|
@ -10,9 +10,10 @@ import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
@Service("VisitService")
|
@Service("VisitService")
|
||||||
public class VisitService implements BaseService<Visit, VisitDTO>{
|
public class VisitService implements BaseService<Visit, VisitDTO> {
|
||||||
|
|
||||||
private final VisitRepository visitRepository;
|
private final VisitRepository visitRepository;
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
public VisitService(VisitRepository visitRepository) {
|
public VisitService(VisitRepository visitRepository) {
|
||||||
|
@ -33,7 +34,7 @@ public class VisitService implements BaseService<Visit, VisitDTO>{
|
||||||
public Collection<VisitDTO> entitiesToDTOS(Collection<Visit> entities) {
|
public Collection<VisitDTO> entitiesToDTOS(Collection<Visit> entities) {
|
||||||
Collection<VisitDTO> dtos = new HashSet<>();
|
Collection<VisitDTO> dtos = new HashSet<>();
|
||||||
|
|
||||||
for(Visit entity: entities) {
|
for (Visit entity : entities) {
|
||||||
dtos.add(entityToDTO(entity));
|
dtos.add(entityToDTO(entity));
|
||||||
}
|
}
|
||||||
return dtos;
|
return dtos;
|
||||||
|
@ -43,7 +44,7 @@ public class VisitService implements BaseService<Visit, VisitDTO>{
|
||||||
public Collection<Visit> dtosToEntities(Collection<VisitDTO> dtos) {
|
public Collection<Visit> dtosToEntities(Collection<VisitDTO> dtos) {
|
||||||
Collection<Visit> entities = new HashSet<>();
|
Collection<Visit> entities = new HashSet<>();
|
||||||
|
|
||||||
for(VisitDTO dto: dtos) {
|
for (VisitDTO dto : dtos) {
|
||||||
entities.add(dtoToEntity(dto));
|
entities.add(dtoToEntity(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,10 +60,11 @@ public class VisitService implements BaseService<Visit, VisitDTO>{
|
||||||
Collection<Visit> visits = visitRepository.findByPetId(petId);
|
Collection<Visit> visits = visitRepository.findByPetId(petId);
|
||||||
Collection<VisitDTO> visitDTOS = new HashSet<>();
|
Collection<VisitDTO> visitDTOS = new HashSet<>();
|
||||||
|
|
||||||
for (Visit visit: visits) {
|
for (Visit visit : visits) {
|
||||||
visitDTOS.add(entityToDTO(visit));
|
visitDTOS.add(entityToDTO(visit));
|
||||||
}
|
}
|
||||||
|
|
||||||
return visitDTOS;
|
return visitDTOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.controller;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -28,10 +28,12 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.samples.petclinic.controller.OwnerController;
|
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
||||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
import org.springframework.samples.petclinic.visit.Visit;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
import org.springframework.samples.petclinic.dto.VisitDTO;
|
||||||
|
import org.springframework.samples.petclinic.service.OwnerService;
|
||||||
|
import org.springframework.samples.petclinic.service.VisitService;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.empty;
|
import static org.hamcrest.Matchers.empty;
|
||||||
|
@ -59,24 +61,24 @@ class OwnerControllerTests {
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private OwnerRepository owners;
|
private OwnerService owners;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private VisitRepository visits;
|
private VisitService visits;
|
||||||
|
|
||||||
private Owner george;
|
private OwnerDTO george;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
george = new Owner();
|
george = new OwnerDTO();
|
||||||
george.setId(TEST_OWNER_ID);
|
george.setId(TEST_OWNER_ID);
|
||||||
george.setFirstName("George");
|
george.setFirstName("George");
|
||||||
george.setLastName("Franklin");
|
george.setLastName("Franklin");
|
||||||
george.setAddress("110 W. Liberty St.");
|
george.setAddress("110 W. Liberty St.");
|
||||||
george.setCity("Madison");
|
george.setCity("Madison");
|
||||||
george.setTelephone("6085551023");
|
george.setTelephone("6085551023");
|
||||||
Pet max = new Pet();
|
PetDTO max = new PetDTO();
|
||||||
PetType dog = new PetType();
|
PetTypeDTO dog = new PetTypeDTO();
|
||||||
dog.setName("dog");
|
dog.setName("dog");
|
||||||
max.setId(1);
|
max.setId(1);
|
||||||
max.setType(dog);
|
max.setType(dog);
|
||||||
|
@ -84,7 +86,7 @@ class OwnerControllerTests {
|
||||||
max.setBirthDate(LocalDate.now());
|
max.setBirthDate(LocalDate.now());
|
||||||
george.setPetsInternal(Collections.singleton(max));
|
george.setPetsInternal(Collections.singleton(max));
|
||||||
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
|
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
|
||||||
Visit visit = new Visit();
|
VisitDTO visit = new VisitDTO();
|
||||||
visit.setDate(LocalDate.now());
|
visit.setDate(LocalDate.now());
|
||||||
given(this.visits.findByPetId(max.getId())).willReturn(Collections.singletonList(visit));
|
given(this.visits.findByPetId(max.getId())).willReturn(Collections.singletonList(visit));
|
||||||
}
|
}
|
||||||
|
@ -120,7 +122,7 @@ class OwnerControllerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testProcessFindFormSuccess() throws Exception {
|
void testProcessFindFormSuccess() throws Exception {
|
||||||
given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
|
given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new OwnerDTO()));
|
||||||
mockMvc.perform(get("/owners")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
|
mockMvc.perform(get("/owners")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,13 +180,13 @@ class OwnerControllerTests {
|
||||||
.andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
|
.andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
|
||||||
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
|
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
|
||||||
.andExpect(model().attribute("owner", hasProperty("pets", not(empty()))))
|
.andExpect(model().attribute("owner", hasProperty("pets", not(empty()))))
|
||||||
.andExpect(model().attribute("owner", hasProperty("pets", new BaseMatcher<List<Pet>>() {
|
.andExpect(model().attribute("owner", hasProperty("pets", new BaseMatcher<List<PetDTO>>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean matches(Object item) {
|
public boolean matches(Object item) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
List<Pet> pets = (List<Pet>) item;
|
List<PetDTO> pets = (List<PetDTO>) item;
|
||||||
Pet pet = pets.get(0);
|
PetDTO pet = pets.get(0);
|
||||||
if (pet.getVisits().isEmpty()) {
|
if (pet.getVisits().isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.controller;
|
||||||
|
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
@ -32,8 +32,18 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.FilterType;
|
import org.springframework.context.annotation.FilterType;
|
||||||
import org.springframework.samples.petclinic.controller.PetController;
|
import org.springframework.samples.petclinic.controller.PetController;
|
||||||
|
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
|
import org.springframework.samples.petclinic.owner.Owner;
|
||||||
|
import org.springframework.samples.petclinic.owner.Pet;
|
||||||
|
import org.springframework.samples.petclinic.owner.PetType;
|
||||||
|
import org.springframework.samples.petclinic.owner.PetTypeFormatter;
|
||||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||||
|
import org.springframework.samples.petclinic.service.OwnerService;
|
||||||
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
|
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,19 +63,22 @@ class PetControllerTests {
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private PetRepository pets;
|
private PetService petService;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private OwnerRepository owners;
|
private PetTypeService petTypeService;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private OwnerService ownerService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
PetType cat = new PetType();
|
PetTypeDTO cat = new PetTypeDTO();
|
||||||
cat.setId(3);
|
cat.setId(3);
|
||||||
cat.setName("hamster");
|
cat.setName("hamster");
|
||||||
given(this.pets.findPetTypes()).willReturn(Lists.newArrayList(cat));
|
given(this.petTypeService.findPetTypes()).willReturn(Lists.newArrayList(cat));
|
||||||
given(this.owners.findById(TEST_OWNER_ID)).willReturn(new Owner());
|
given(this.ownerService.findById(TEST_OWNER_ID)).willReturn(new OwnerDTO());
|
||||||
given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
|
given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.vet;
|
package org.springframework.samples.petclinic.controller;
|
||||||
|
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
@ -31,8 +31,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.samples.petclinic.controller.VetController;
|
import org.springframework.samples.petclinic.dto.VetDTO;
|
||||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
import org.springframework.samples.petclinic.service.VetService;
|
||||||
|
import org.springframework.samples.petclinic.vet.Specialty;
|
||||||
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;
|
||||||
|
|
||||||
|
@ -46,15 +47,15 @@ class VetControllerTests {
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private VetRepository vets;
|
private VetService vetService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
Vet james = new Vet();
|
VetDTO james = new VetDTO();
|
||||||
james.setFirstName("James");
|
james.setFirstName("James");
|
||||||
james.setLastName("Carter");
|
james.setLastName("Carter");
|
||||||
james.setId(1);
|
james.setId(1);
|
||||||
Vet helen = new Vet();
|
VetDTO helen = new VetDTO();
|
||||||
helen.setFirstName("Helen");
|
helen.setFirstName("Helen");
|
||||||
helen.setLastName("Leary");
|
helen.setLastName("Leary");
|
||||||
helen.setId(2);
|
helen.setId(2);
|
||||||
|
@ -62,7 +63,7 @@ class VetControllerTests {
|
||||||
radiology.setId(1);
|
radiology.setId(1);
|
||||||
radiology.setName("radiology");
|
radiology.setName("radiology");
|
||||||
helen.addSpecialty(radiology);
|
helen.addSpecialty(radiology);
|
||||||
given(this.vets.findAll()).willReturn(Lists.newArrayList(james, helen));
|
given(this.vetService.findAll()).willReturn(Lists.newArrayList(james, helen));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.controller;
|
||||||
|
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
@ -28,9 +28,9 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.samples.petclinic.controller.VisitController;
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
import org.springframework.samples.petclinic.service.VisitService;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,14 +47,14 @@ class VisitControllerTests {
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private VisitRepository visits;
|
private VisitService visitService;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private PetRepository pets;
|
private PetService petService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void init() {
|
void init() {
|
||||||
given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
|
given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
|
@ -112,10 +112,10 @@ class ClinicServiceTests {
|
||||||
owner.setCity("Wollongong");
|
owner.setCity("Wollongong");
|
||||||
owner.setTelephone("4444444444");
|
owner.setTelephone("4444444444");
|
||||||
this.owners.save(owner);
|
this.owners.save(owner);
|
||||||
assertThat(owner.getId().longValue()).isNotEqualTo(0);
|
assertThat(owner.getId().longValue()).isNotZero();
|
||||||
|
|
||||||
owners = this.owners.findByLastName("Schultz");
|
owners = this.owners.findByLastName("Schultz");
|
||||||
assertThat(owners.size()).isEqualTo(found + 1);
|
assertThat(owners).hasSize(found + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -176,7 +176,7 @@ class ClinicServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
void shouldUpdatePetName() throws Exception {
|
void shouldUpdatePetName() {
|
||||||
Pet pet7 = this.pets.findById(7);
|
Pet pet7 = this.pets.findById(7);
|
||||||
String oldName = pet7.getName();
|
String oldName = pet7.getName();
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ class ClinicServiceTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldFindVisitsByPetId() throws Exception {
|
void shouldFindVisitsByPetId() {
|
||||||
Collection<Visit> visits = this.visits.findByPetId(7);
|
Collection<Visit> visits = this.visits.findByPetId(7);
|
||||||
assertThat(visits).hasSize(2);
|
assertThat(visits).hasSize(2);
|
||||||
Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
|
Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
|
||||||
|
|
Loading…
Reference in a new issue