mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
Implements DTOs and Services
This commit is contained in:
parent
27109010a5
commit
45e0b07d6f
34 changed files with 1030 additions and 75 deletions
7
pom.xml
7
pom.xml
|
@ -70,6 +70,13 @@
|
|||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- ================================================== MODELMAPPER -->
|
||||
<dependency>
|
||||
<groupId>org.modelmapper.extensions</groupId>
|
||||
<artifactId>modelmapper-spring</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Databases - Uses H2 by default -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
|
|
|
@ -13,9 +13,11 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
package org.springframework.samples.petclinic.controller;
|
||||
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.samples.petclinic.dto.*;
|
||||
import org.springframework.samples.petclinic.service.OwnerService;
|
||||
import org.springframework.samples.petclinic.service.VisitService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
@ -35,19 +37,19 @@ import java.util.Map;
|
|||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
* @author Michael Isvy
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
@Controller
|
||||
class OwnerController {
|
||||
|
||||
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||
|
||||
private final OwnerRepository owners;
|
||||
private final OwnerService ownerService;
|
||||
private final VisitService visitService;
|
||||
|
||||
private VisitRepository visits;
|
||||
|
||||
public OwnerController(OwnerRepository clinicService, VisitRepository visits) {
|
||||
this.owners = clinicService;
|
||||
this.visits = visits;
|
||||
OwnerController(OwnerService ownerService, VisitService visitService) {
|
||||
this.ownerService = ownerService;
|
||||
this.visitService = visitService;
|
||||
}
|
||||
|
||||
@InitBinder
|
||||
|
@ -57,30 +59,30 @@ class OwnerController {
|
|||
|
||||
@GetMapping("/owners/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
Owner owner = new Owner();
|
||||
OwnerDTO owner = new OwnerDTO();
|
||||
model.put("owner", owner);
|
||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping("/owners/new")
|
||||
public String processCreationForm(@Valid Owner owner, BindingResult result) {
|
||||
public String processCreationForm(@Valid OwnerDTO owner, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
else {
|
||||
this.owners.save(owner);
|
||||
this.ownerService.save(owner);
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/owners/find")
|
||||
public String initFindForm(Map<String, Object> model) {
|
||||
model.put("owner", new Owner());
|
||||
model.put("owner", new OwnerDTO());
|
||||
return "owners/findOwners";
|
||||
}
|
||||
|
||||
@GetMapping("/owners")
|
||||
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {
|
||||
public String processFindForm(OwnerDTO owner, BindingResult result, Map<String, Object> model) {
|
||||
|
||||
// allow parameterless GET request for /owners to return all records
|
||||
if (owner.getLastName() == null) {
|
||||
|
@ -88,7 +90,7 @@ class OwnerController {
|
|||
}
|
||||
|
||||
// find owners by last name
|
||||
Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
|
||||
Collection<OwnerDTO> results = this.ownerService.findByLastName(owner.getLastName());
|
||||
if (results.isEmpty()) {
|
||||
// no owners found
|
||||
result.rejectValue("lastName", "notFound", "not found");
|
||||
|
@ -108,20 +110,20 @@ class OwnerController {
|
|||
|
||||
@GetMapping("/owners/{ownerId}/edit")
|
||||
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
||||
Owner owner = this.owners.findById(ownerId);
|
||||
model.addAttribute(owner);
|
||||
OwnerDTO ownerDTO = this.ownerService.findById(ownerId);
|
||||
model.addAttribute(ownerDTO);
|
||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping("/owners/{ownerId}/edit")
|
||||
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
|
||||
public String processUpdateOwnerForm(@Valid OwnerDTO owner, BindingResult result,
|
||||
@PathVariable("ownerId") int ownerId) {
|
||||
if (result.hasErrors()) {
|
||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
else {
|
||||
owner.setId(ownerId);
|
||||
this.owners.save(owner);
|
||||
this.ownerService.save(owner);
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
||||
|
@ -134,10 +136,12 @@ class OwnerController {
|
|||
@GetMapping("/owners/{ownerId}")
|
||||
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
|
||||
ModelAndView mav = new ModelAndView("owners/ownerDetails");
|
||||
Owner owner = this.owners.findById(ownerId);
|
||||
for (Pet pet : owner.getPets()) {
|
||||
pet.setVisitsInternal(visits.findByPetId(pet.getId()));
|
||||
OwnerDTO owner = this.ownerService.findById(ownerId);
|
||||
|
||||
for (PetDTO petDTO : owner.getPets()) {
|
||||
petDTO.setVisitsInternal(visitService.findByPetId(petDTO.getId()));
|
||||
}
|
||||
|
||||
mav.addObject(owner);
|
||||
return mav;
|
||||
}
|
|
@ -13,8 +13,15 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
package org.springframework.samples.petclinic.controller;
|
||||
|
||||
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.PetValidator;
|
||||
import org.springframework.samples.petclinic.service.OwnerService;
|
||||
import org.springframework.samples.petclinic.service.PetService;
|
||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
@ -29,6 +36,7 @@ import java.util.Collection;
|
|||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/owners/{ownerId}")
|
||||
|
@ -36,23 +44,25 @@ class PetController {
|
|||
|
||||
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
|
||||
|
||||
private final PetRepository pets;
|
||||
private final OwnerService ownerService;
|
||||
private final PetService petService;
|
||||
private final PetTypeService petTypeService;
|
||||
|
||||
private final OwnerRepository owners;
|
||||
|
||||
public PetController(PetRepository pets, OwnerRepository owners) {
|
||||
this.pets = pets;
|
||||
this.owners = owners;
|
||||
PetController(OwnerService ownerService, PetService petService, PetTypeService petTypeService) {
|
||||
this.ownerService = ownerService;
|
||||
this.petService = petService;
|
||||
this.petTypeService = petTypeService;
|
||||
}
|
||||
|
||||
|
||||
@ModelAttribute("types")
|
||||
public Collection<PetType> populatePetTypes() {
|
||||
return this.pets.findPetTypes();
|
||||
public Collection<PetTypeDTO> populatePetTypes() {
|
||||
return this.petTypeService.findPetTypes();
|
||||
}
|
||||
|
||||
@ModelAttribute("owner")
|
||||
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
|
||||
return this.owners.findById(ownerId);
|
||||
public OwnerDTO findOwner(@PathVariable("ownerId") int ownerId) {
|
||||
return this.ownerService.findById(ownerId);
|
||||
}
|
||||
|
||||
@InitBinder("owner")
|
||||
|
@ -66,38 +76,38 @@ class PetController {
|
|||
}
|
||||
|
||||
@GetMapping("/pets/new")
|
||||
public String initCreationForm(Owner owner, ModelMap model) {
|
||||
Pet pet = new Pet();
|
||||
public String initCreationForm(OwnerDTO owner, ModelMap model) {
|
||||
PetDTO pet = new PetDTO();
|
||||
owner.addPet(pet);
|
||||
model.put("pet", pet);
|
||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping("/pets/new")
|
||||
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
|
||||
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
|
||||
public String processCreationForm(OwnerDTO ownerDTO, @Valid PetDTO pet, BindingResult result, ModelMap model) {
|
||||
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && ownerDTO.getPet(pet.getName(), true) != null) {
|
||||
result.rejectValue("name", "duplicate", "already exists");
|
||||
}
|
||||
owner.addPet(pet);
|
||||
ownerDTO.addPet(pet);
|
||||
if (result.hasErrors()) {
|
||||
model.put("pet", pet);
|
||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
else {
|
||||
this.pets.save(pet);
|
||||
this.petService.save(pet);
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/pets/{petId}/edit")
|
||||
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
|
||||
Pet pet = this.pets.findById(petId);
|
||||
PetDTO pet = this.petService.findById(petId);
|
||||
model.put("pet", pet);
|
||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping("/pets/{petId}/edit")
|
||||
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
|
||||
public String processUpdateForm(@Valid PetDTO pet, BindingResult result, OwnerDTO owner, ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
pet.setOwner(owner);
|
||||
model.put("pet", pet);
|
||||
|
@ -105,7 +115,7 @@ class PetController {
|
|||
}
|
||||
else {
|
||||
owner.addPet(pet);
|
||||
this.pets.save(pet);
|
||||
this.petService.save(pet);
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
|
@ -13,8 +13,11 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.vet;
|
||||
package org.springframework.samples.petclinic.controller;
|
||||
|
||||
import org.springframework.samples.petclinic.dto.VetsDTO;
|
||||
import org.springframework.samples.petclinic.service.VetService;
|
||||
import org.springframework.samples.petclinic.vet.Vets;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
@ -26,32 +29,34 @@ import java.util.Map;
|
|||
* @author Mark Fisher
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
@Controller
|
||||
class VetController {
|
||||
|
||||
private final VetRepository vets;
|
||||
private final VetService vetService;
|
||||
|
||||
public VetController(VetRepository clinicService) {
|
||||
this.vets = clinicService;
|
||||
VetController(VetService vetService) {
|
||||
this.vetService = vetService;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/vets.html")
|
||||
public String showVetList(Map<String, Object> model) {
|
||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||
// objects so it is simpler for Object-Xml mapping
|
||||
Vets vets = new Vets();
|
||||
vets.getVetList().addAll(this.vets.findAll());
|
||||
VetsDTO vets = new VetsDTO();
|
||||
vets.getVetList().addAll(this.vetService.findAll());
|
||||
model.put("vets", vets);
|
||||
return "vets/vetList";
|
||||
}
|
||||
|
||||
@GetMapping({ "/vets" })
|
||||
public @ResponseBody Vets showResourcesVetList() {
|
||||
public @ResponseBody VetsDTO showResourcesVetList() {
|
||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||
// objects so it is simpler for JSon/Object mapping
|
||||
Vets vets = new Vets();
|
||||
vets.getVetList().addAll(this.vets.findAll());
|
||||
VetsDTO vets = new VetsDTO();
|
||||
vets.getVetList().addAll(this.vetService.findAll());
|
||||
return vets;
|
||||
}
|
||||
|
|
@ -13,14 +13,17 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
package org.springframework.samples.petclinic.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||
import org.springframework.samples.petclinic.dto.VisitDTO;
|
||||
import org.springframework.samples.petclinic.service.PetService;
|
||||
import org.springframework.samples.petclinic.service.VisitService;
|
||||
import org.springframework.samples.petclinic.visit.Visit;
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
|
@ -36,19 +39,20 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||
* @author Arjen Poutsma
|
||||
* @author Michael Isvy
|
||||
* @author Dave Syer
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
@Controller
|
||||
class VisitController {
|
||||
|
||||
private final VisitRepository visits;
|
||||
private final VisitService visitService;
|
||||
private final PetService petService;
|
||||
|
||||
private final PetRepository pets;
|
||||
|
||||
public VisitController(VisitRepository visits, PetRepository pets) {
|
||||
this.visits = visits;
|
||||
this.pets = pets;
|
||||
VisitController(VisitService visitService, PetService petService) {
|
||||
this.visitService = visitService;
|
||||
this.petService = petService;
|
||||
}
|
||||
|
||||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
|
@ -62,11 +66,11 @@ class VisitController {
|
|||
* @return Pet
|
||||
*/
|
||||
@ModelAttribute("visit")
|
||||
public Visit loadPetWithVisit(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||
Pet pet = this.pets.findById(petId);
|
||||
pet.setVisitsInternal(this.visits.findByPetId(petId));
|
||||
public VisitDTO loadPetWithVisit(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||
PetDTO pet = this.petService.findById(petId);
|
||||
pet.setVisitsInternal(this.visitService.findByPetId(petId));
|
||||
model.put("pet", pet);
|
||||
Visit visit = new Visit();
|
||||
VisitDTO visit = new VisitDTO();
|
||||
pet.addVisit(visit);
|
||||
return visit;
|
||||
}
|
||||
|
@ -79,12 +83,12 @@ class VisitController {
|
|||
|
||||
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
||||
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
|
||||
public String processNewVisitForm(@Valid Visit visit, BindingResult result) {
|
||||
public String processNewVisitForm(@Valid VisitDTO visit, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return "pets/createOrUpdateVisitForm";
|
||||
}
|
||||
else {
|
||||
this.visits.save(visit);
|
||||
this.visitService.save(visit);
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object with an id property. Used as a base class for DTO
|
||||
* needing this property.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class BaseDTO implements Serializable {
|
||||
|
||||
private Integer id;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public boolean isNew() {
|
||||
return this.id == null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object with a name property to <code>BaseDTO</code>. Used as
|
||||
* a base class for DTOs needing these properties.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class NamedDTO extends BaseDTO {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getName();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
import org.springframework.beans.support.MutableSortDefinition;
|
||||
import org.springframework.beans.support.PropertyComparator;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
||||
import javax.validation.constraints.Digits;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object representing a owner.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class OwnerDTO extends PersonDTO {
|
||||
|
||||
@NotEmpty
|
||||
private String address;
|
||||
|
||||
@NotEmpty
|
||||
private String city;
|
||||
|
||||
@NotEmpty
|
||||
@Digits(fraction = 0, integer = 10)
|
||||
private String telephone;
|
||||
|
||||
private Set<PetDTO> pets;
|
||||
|
||||
public String getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return this.city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getTelephone() {
|
||||
return this.telephone;
|
||||
}
|
||||
|
||||
public void setTelephone(String telephone) {
|
||||
this.telephone = telephone;
|
||||
}
|
||||
|
||||
protected Set<PetDTO> getPetsInternal() {
|
||||
if (this.pets == null) {
|
||||
this.pets = new HashSet<>();
|
||||
}
|
||||
return this.pets;
|
||||
}
|
||||
|
||||
protected void setPetsInternal(Set<PetDTO> pets) {
|
||||
this.pets = pets;
|
||||
}
|
||||
|
||||
public List<PetDTO> getPets() {
|
||||
List<PetDTO> sortedPets = new ArrayList<>(getPetsInternal());
|
||||
PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
|
||||
return Collections.unmodifiableList(sortedPets);
|
||||
}
|
||||
|
||||
public void addPet(PetDTO pet) {
|
||||
if (pet.isNew()) {
|
||||
getPetsInternal().add(pet);
|
||||
}
|
||||
pet.setOwner(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Pet with the given name, or null if none found for this Owner.
|
||||
* @param name to test
|
||||
* @return true if pet name is already in use
|
||||
*/
|
||||
public PetDTO getPet(String name) {
|
||||
return getPet(name, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Pet with the given name, or null if none found for this Owner.
|
||||
* @param name to test
|
||||
* @return true if pet name is already in use
|
||||
*/
|
||||
public PetDTO getPet(String name, boolean ignoreNew) {
|
||||
name = name.toLowerCase();
|
||||
for (PetDTO pet : getPetsInternal()) {
|
||||
if (!ignoreNew || !pet.isNew()) {
|
||||
String compName = pet.getName();
|
||||
compName = compName.toLowerCase();
|
||||
if (compName.equals(name)) {
|
||||
return pet;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringCreator(this)
|
||||
|
||||
.append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName())
|
||||
.append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city)
|
||||
.append("telephone", this.telephone).toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object representing a person.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class PersonDTO extends BaseDTO {
|
||||
|
||||
@NotEmpty
|
||||
private String firstName;
|
||||
@NotEmpty
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return this.firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return this.lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
import org.springframework.beans.support.MutableSortDefinition;
|
||||
import org.springframework.beans.support.PropertyComparator;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.samples.petclinic.owner.PetType;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object representing a pet.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class PetDTO extends NamedDTO {
|
||||
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate birthDate;
|
||||
|
||||
private PetType type;
|
||||
|
||||
private OwnerDTO owner;
|
||||
|
||||
private Set<VisitDTO> visits = new LinkedHashSet<>();
|
||||
|
||||
public void setBirthDate(LocalDate birthDate) {
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public LocalDate getBirthDate() {
|
||||
return this.birthDate;
|
||||
}
|
||||
|
||||
public PetType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setType(PetType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public OwnerDTO getOwnerDTO() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(OwnerDTO owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
protected Set<VisitDTO> getVisitsInternal() {
|
||||
if (this.visits == null) {
|
||||
this.visits = new HashSet<>();
|
||||
}
|
||||
return this.visits;
|
||||
}
|
||||
|
||||
public void setVisitsInternal(Collection<VisitDTO> visits) {
|
||||
this.visits = new LinkedHashSet<>(visits);
|
||||
}
|
||||
|
||||
public List<VisitDTO> getVisits() {
|
||||
List<VisitDTO> sortedVisits = new ArrayList<>(getVisitsInternal());
|
||||
PropertyComparator.sort(sortedVisits, new MutableSortDefinition("date", false, false));
|
||||
return Collections.unmodifiableList(sortedVisits);
|
||||
}
|
||||
|
||||
public void addVisit(VisitDTO visit) {
|
||||
getVisitsInternal().add(visit);
|
||||
visit.setPetId(this.getId());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object representing PetType.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class PetTypeDTO extends NamedDTO {
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object representing a list of specialities.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class SpecialtyDTO extends NamedDTO implements Serializable {
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
import org.springframework.beans.support.MutableSortDefinition;
|
||||
import org.springframework.beans.support.PropertyComparator;
|
||||
import org.springframework.samples.petclinic.vet.Specialty;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object representing a vet.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class VetDTO extends PersonDTO {
|
||||
|
||||
private Set<Specialty> specialties;
|
||||
|
||||
protected Set<Specialty> getSpecialtiesInternal() {
|
||||
if (this.specialties == null) {
|
||||
this.specialties = new HashSet<>();
|
||||
}
|
||||
return this.specialties;
|
||||
}
|
||||
|
||||
protected void setSpecialtiesInternal(Set<Specialty> specialties) {
|
||||
this.specialties = specialties;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
public List<Specialty> getSpecialties() {
|
||||
List<Specialty> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
|
||||
PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true));
|
||||
return Collections.unmodifiableList(sortedSpecs);
|
||||
}
|
||||
|
||||
public int getNrOfSpecialties() {
|
||||
return getSpecialtiesInternal().size();
|
||||
}
|
||||
|
||||
public void addSpecialty(Specialty specialty) {
|
||||
getSpecialtiesInternal().add(specialty);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object representing a list of veterinarians.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class VetsDTO {
|
||||
|
||||
private List<VetDTO> vets;
|
||||
|
||||
public List<VetDTO> getVetList() {
|
||||
if (vets == null) {
|
||||
vets = new ArrayList<>();
|
||||
}
|
||||
return vets;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.dto;
|
||||
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* Simple Data Transfert Object representing a visit.
|
||||
*
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
public class VisitDTO extends BaseDTO {
|
||||
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate date;
|
||||
|
||||
@NotEmpty
|
||||
private String description;
|
||||
|
||||
private Integer petId;
|
||||
|
||||
/**
|
||||
* Creates a new instance of Visit for the current date
|
||||
*/
|
||||
public VisitDTO() {
|
||||
this.date = LocalDate.now();
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return this.date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Integer getPetId() {
|
||||
return this.petId;
|
||||
}
|
||||
|
||||
public void setPetId(Integer petId) {
|
||||
this.petId = petId;
|
||||
}
|
||||
|
||||
}
|
|
@ -21,6 +21,7 @@ import java.util.Locale;
|
|||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.format.Formatter;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,13 +13,14 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
package org.springframework.samples.petclinic.repository;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.samples.petclinic.owner.Owner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
|
@ -13,12 +13,14 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
package org.springframework.samples.petclinic.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.samples.petclinic.owner.Pet;
|
||||
import org.springframework.samples.petclinic.owner.PetType;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
|
@ -13,13 +13,14 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.vet;
|
||||
package org.springframework.samples.petclinic.repository;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.samples.petclinic.vet.Vet;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
|
@ -13,13 +13,14 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.visit;
|
||||
package org.springframework.samples.petclinic.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.samples.petclinic.model.BaseEntity;
|
||||
import org.springframework.samples.petclinic.visit.Visit;
|
||||
|
||||
/**
|
||||
* Repository class for <code>Visit</code> domain objects All method names are compliant
|
|
@ -0,0 +1,10 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface BaseService<E,D> {
|
||||
public E dtoToEntity(D dto);
|
||||
public D entityToDTO(E entity);
|
||||
public Collection<D> entitiesToDTOS(Collection<E> entities);
|
||||
public Collection<E> dtosToEntities(Collection<D> dtos);
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
||||
import org.springframework.samples.petclinic.owner.Owner;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Service("OwnerService")
|
||||
public class OwnerService implements BaseService<Owner,OwnerDTO>{
|
||||
|
||||
private final OwnerRepository ownerRepository;
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
public OwnerService(OwnerRepository ownerRepository) {
|
||||
this.ownerRepository = ownerRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Owner dtoToEntity(OwnerDTO dto) {
|
||||
return modelMapper.map(dto, Owner.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OwnerDTO entityToDTO(Owner entity) {
|
||||
return modelMapper.map(entity, OwnerDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<OwnerDTO> entitiesToDTOS(Collection<Owner> entities) {
|
||||
Collection<OwnerDTO> dtos = new HashSet<>();
|
||||
|
||||
for(Owner entity: entities) {
|
||||
dtos.add(entityToDTO(entity));
|
||||
}
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Owner> dtosToEntities(Collection<OwnerDTO> dtos) {
|
||||
Collection<Owner> entities = new HashSet<>();
|
||||
|
||||
for(OwnerDTO dto: dtos) {
|
||||
entities.add(dtoToEntity(dto));
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
public void save(OwnerDTO ownerDTO) {
|
||||
Owner owner = dtoToEntity(ownerDTO);
|
||||
ownerRepository.save(owner);
|
||||
}
|
||||
|
||||
public Collection<OwnerDTO> findByLastName(String lastName) {
|
||||
Collection<Owner> owners = ownerRepository.findByLastName(lastName);
|
||||
return entitiesToDTOS(owners);
|
||||
}
|
||||
|
||||
public OwnerDTO findById(int ownerId) {
|
||||
Owner owner = ownerRepository.findById(ownerId);
|
||||
return entityToDTO(owner);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||
import org.springframework.samples.petclinic.owner.Pet;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Service("PetService")
|
||||
public class PetService implements BaseService<Pet,PetDTO>{
|
||||
private final PetRepository petRepository;
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
public PetService(PetRepository petRepository) {
|
||||
this.petRepository = petRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pet dtoToEntity(PetDTO dto) {
|
||||
return modelMapper.map(dto, Pet.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PetDTO entityToDTO(Pet entity) {
|
||||
return modelMapper.map(entity, PetDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<PetDTO> entitiesToDTOS(Collection<Pet> entities) {
|
||||
Collection<PetDTO> dtos = new HashSet<>();
|
||||
|
||||
for(Pet entity:entities) {
|
||||
dtos.add(entityToDTO(entity));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Pet> dtosToEntities(Collection<PetDTO> dtos) {
|
||||
Collection<Pet> entities = new HashSet<>();
|
||||
|
||||
for(PetDTO dto: dtos) {
|
||||
entities.add(dtoToEntity(dto));
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
public void save(PetDTO petDTO) {
|
||||
Pet pet = dtoToEntity(petDTO);
|
||||
petRepository.save(pet);
|
||||
}
|
||||
|
||||
public PetDTO findById(int petId) {
|
||||
Pet pet = petRepository.findById(petId);
|
||||
return entityToDTO(pet);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||
import org.springframework.samples.petclinic.owner.PetType;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Service("PerTypeService")
|
||||
public class PetTypeService implements BaseService<PetType, PetTypeDTO>{
|
||||
private final PetRepository petRepository;
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
public PetTypeService(PetRepository petRepository) {
|
||||
this.petRepository = petRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PetType dtoToEntity(PetTypeDTO dto) {
|
||||
return modelMapper.map(dto, PetType.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PetTypeDTO entityToDTO(PetType entity) {
|
||||
return modelMapper.map(entity, PetTypeDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<PetTypeDTO> entitiesToDTOS(Collection<PetType> entities) {
|
||||
Collection<PetTypeDTO> dtos = new HashSet<>();
|
||||
|
||||
for(PetType entity : entities) {
|
||||
dtos.add(entityToDTO(entity));
|
||||
}
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<PetType> dtosToEntities(Collection<PetTypeDTO> dtos) {
|
||||
Collection<PetType> entities = new HashSet<>();
|
||||
|
||||
for(PetTypeDTO dto: dtos) {
|
||||
entities.add(dtoToEntity(dto));
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
public Collection<PetTypeDTO> findPetTypes() {
|
||||
Collection<PetType> petTypes = petRepository.findPetTypes();
|
||||
return entitiesToDTOS(petTypes);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.samples.petclinic.dto.SpecialtyDTO;
|
||||
import org.springframework.samples.petclinic.vet.Specialty;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Service("SpecialityService")
|
||||
public class SpecialityService implements BaseService<Specialty, SpecialtyDTO> {
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
@Override
|
||||
public Specialty dtoToEntity(SpecialtyDTO dto) {
|
||||
return modelMapper.map(dto, Specialty.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpecialtyDTO entityToDTO(Specialty entity) {
|
||||
return modelMapper.map(entity, SpecialtyDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SpecialtyDTO> entitiesToDTOS(Collection<Specialty> entities) {
|
||||
Collection<SpecialtyDTO> dtos = new HashSet<>();
|
||||
|
||||
for(Specialty entity:entities) {
|
||||
dtos.add(entityToDTO(entity));
|
||||
}
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Specialty> dtosToEntities(Collection<SpecialtyDTO> dtos) {
|
||||
Collection<Specialty> entities = new HashSet<>();
|
||||
|
||||
for(SpecialtyDTO dto: dtos) {
|
||||
entities.add(dtoToEntity(dto));
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.samples.petclinic.dto.VetDTO;
|
||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||
import org.springframework.samples.petclinic.vet.Vet;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Service("VetService")
|
||||
public class VetService implements BaseService<Vet,VetDTO>{
|
||||
|
||||
private final VetRepository vetRepository;
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
public VetService(VetRepository vetRepository) {
|
||||
this.vetRepository = vetRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vet dtoToEntity(VetDTO dto) {
|
||||
return modelMapper.map(dto, Vet.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VetDTO entityToDTO(Vet entity) {
|
||||
return modelMapper.map(entity, VetDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<VetDTO> entitiesToDTOS(Collection<Vet> entities) {
|
||||
Collection<VetDTO> dtos = new HashSet<>();
|
||||
|
||||
for(Vet entity: entities) {
|
||||
dtos.add(entityToDTO(entity));
|
||||
}
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Vet> dtosToEntities(Collection<VetDTO> dtos) {
|
||||
Collection<Vet> entities = new HashSet<>();
|
||||
|
||||
for(VetDTO dto: dtos) {
|
||||
entities.add(dtoToEntity(dto));
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
public Collection<VetDTO> findAll() {
|
||||
Collection<Vet> vets = vetRepository.findAll();
|
||||
return entitiesToDTOS(vets);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.samples.petclinic.dto.VisitDTO;
|
||||
import org.springframework.samples.petclinic.visit.Visit;
|
||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Service("VisitService")
|
||||
public class VisitService implements BaseService<Visit, VisitDTO>{
|
||||
|
||||
private final VisitRepository visitRepository;
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
public VisitService(VisitRepository visitRepository) {
|
||||
this.visitRepository = visitRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Visit dtoToEntity(VisitDTO dto) {
|
||||
return modelMapper.map(dto, Visit.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VisitDTO entityToDTO(Visit entity) {
|
||||
return modelMapper.map(entity, VisitDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<VisitDTO> entitiesToDTOS(Collection<Visit> entities) {
|
||||
Collection<VisitDTO> dtos = new HashSet<>();
|
||||
|
||||
for(Visit entity: entities) {
|
||||
dtos.add(entityToDTO(entity));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Visit> dtosToEntities(Collection<VisitDTO> dtos) {
|
||||
Collection<Visit> entities = new HashSet<>();
|
||||
|
||||
for(VisitDTO dto: dtos) {
|
||||
entities.add(dtoToEntity(dto));
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
public void save(VisitDTO visitDTO) {
|
||||
Visit visit = dtoToEntity(visitDTO);
|
||||
visitRepository.save(visit);
|
||||
}
|
||||
|
||||
public Collection<VisitDTO> findByPetId(Integer petId) {
|
||||
Collection<Visit> visits = visitRepository.findByPetId(petId);
|
||||
Collection<VisitDTO> visitDTOS = new HashSet<>();
|
||||
|
||||
for (Visit visit: visits) {
|
||||
visitDTOS.add(entityToDTO(visit));
|
||||
}
|
||||
|
||||
return visitDTOS;
|
||||
}
|
||||
}
|
|
@ -19,7 +19,7 @@ package org.springframework.samples.petclinic;
|
|||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.samples.petclinic.vet.VetRepository;
|
||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||
|
||||
@SpringBootTest
|
||||
class PetclinicIntegrationTests {
|
||||
|
|
|
@ -28,8 +28,10 @@ import org.junit.jupiter.api.Test;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.samples.petclinic.controller.OwnerController;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.visit.Visit;
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
|
|
|
@ -31,6 +31,9 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
|||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.samples.petclinic.controller.PetController;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
/**
|
||||
|
|
|
@ -28,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
|||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
|
|
@ -28,7 +28,9 @@ import org.junit.jupiter.api.Test;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.samples.petclinic.controller.VisitController;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
/**
|
||||
|
|
|
@ -26,14 +26,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.samples.petclinic.owner.Owner;
|
||||
import org.springframework.samples.petclinic.owner.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.owner.Pet;
|
||||
import org.springframework.samples.petclinic.owner.PetRepository;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.owner.PetType;
|
||||
import org.springframework.samples.petclinic.vet.Vet;
|
||||
import org.springframework.samples.petclinic.vet.VetRepository;
|
||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||
import org.springframework.samples.petclinic.visit.Visit;
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
|
|
@ -31,6 +31,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.samples.petclinic.controller.VetController;
|
||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
|
||||
|
|
Loading…
Reference in a new issue