Skip to content
Snippets Groups Projects
Commit 536e7e9a authored by Maciej Szarlinski's avatar Maciej Szarlinski
Browse files

Repackage classes to be in accordance with DDD guidance

parent 0a9302d7
No related branches found
No related tags found
No related merge requests found
Showing
with 218 additions and 151 deletions
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
## Understanding the Spring Petclinic application with a few diagrams ## Understanding the Spring Petclinic application with a few diagrams
<a href="https://speakerdeck.com/michaelisvy/spring-petclinic-sample-application">See the presentation here</a> <a href="https://speakerdeck.com/michaelisvy/spring-petclinic-sample-application">See the presentation here</a>
## Implementation of Domain Driven Desing based on following samples
* https://github.com/VaughnVernon/IDDD_Samples
* https://github.com/citerus/dddsample-core
* https://github.com/BottegaIT/ddd-leaven-v2
## Running petclinic locally ## Running petclinic locally
``` ```
git clone https://github.com/spring-projects/spring-petclinic.git git clone https://github.com/spring-projects/spring-petclinic.git
......
...@@ -3,13 +3,7 @@ package org.springframework.samples.petclinic; ...@@ -3,13 +3,7 @@ package org.springframework.samples.petclinic;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.samples.petclinic.infrastructure.config.PetclinicProperties;
import org.springframework.samples.petclinic.config.PetclinicProperties;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication @SpringBootApplication
@EnableConfigurationProperties(PetclinicProperties.class) @EnableConfigurationProperties(PetclinicProperties.class)
......
...@@ -13,25 +13,18 @@ ...@@ -13,25 +13,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.boundary.web.owner;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.domain.model.owner.Owner;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.domain.model.owner.OwnerService;
import org.springframework.samples.petclinic.support.web.AbstractResourceController;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.Collection;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
...@@ -42,21 +35,20 @@ import javax.validation.Valid; ...@@ -42,21 +35,20 @@ import javax.validation.Valid;
@RestController @RestController
public class OwnerResource extends AbstractResourceController { public class OwnerResource extends AbstractResourceController {
private final ClinicService clinicService; private final OwnerService ownerService;
@Autowired @Autowired
public OwnerResource(ClinicService clinicService) { public OwnerResource(OwnerService ownerService) {
this.clinicService = clinicService; this.ownerService = ownerService;
} }
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id"); dataBinder.setDisallowedFields("id");
} }
private Owner retrieveOwner(int ownerId) { private Owner retrieveOwner(int ownerId) {
return this.clinicService.findOwnerById(ownerId); return this.ownerService.findOwnerById(ownerId);
} }
/** /**
...@@ -65,9 +57,9 @@ public class OwnerResource extends AbstractResourceController { ...@@ -65,9 +57,9 @@ public class OwnerResource extends AbstractResourceController {
@RequestMapping(value = "/owner", method = RequestMethod.POST) @RequestMapping(value = "/owner", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public void createOwner(@Valid @RequestBody Owner owner) { public void createOwner(@Valid @RequestBody Owner owner) {
this.clinicService.saveOwner(owner); this.ownerService.saveOwner(owner);
} }
/** /**
* Read single Owner * Read single Owner
*/ */
...@@ -75,31 +67,30 @@ public class OwnerResource extends AbstractResourceController { ...@@ -75,31 +67,30 @@ public class OwnerResource extends AbstractResourceController {
public Owner findOwner(@PathVariable("ownerId") int ownerId) { public Owner findOwner(@PathVariable("ownerId") int ownerId) {
return retrieveOwner(ownerId); return retrieveOwner(ownerId);
} }
/** /**
* Read List of Owners * Read List of Owners
*/ */
@GetMapping("/owner/list") @GetMapping("/owner/list")
public Collection<Owner> findAll() { public Collection<Owner> findAll() {
return clinicService.findAll(); return ownerService.findAll();
} }
/** /**
* Update Owner * Update Owner
*/ */
@RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.PUT) @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.PUT)
public Owner updateOwner(@PathVariable("ownerId") int ownerId, @Valid @RequestBody Owner ownerRequest) { public Owner updateOwner(@PathVariable("ownerId") int ownerId, @Valid @RequestBody Owner ownerRequest) {
Owner ownerModel = retrieveOwner(ownerId); Owner ownerModel = retrieveOwner(ownerId);
// This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. // This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct.
ownerModel.setFirstName(ownerRequest.getFirstName()); ownerModel.setFirstName(ownerRequest.getFirstName());
ownerModel.setLastName(ownerRequest.getLastName()); ownerModel.setLastName(ownerRequest.getLastName());
ownerModel.setCity(ownerRequest.getCity()); ownerModel.setCity(ownerRequest.getCity());
ownerModel.setAddress(ownerRequest.getAddress()); ownerModel.setAddress(ownerRequest.getAddress());
ownerModel.setTelephone(ownerRequest.getTelephone()); ownerModel.setTelephone(ownerRequest.getTelephone());
this.clinicService.saveOwner(ownerModel); this.ownerService.saveOwner(ownerModel);
return ownerModel; return ownerModel;
} }
} }
...@@ -13,16 +13,18 @@ ...@@ -13,16 +13,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.boundary.web.pet;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.domain.model.owner.Owner;
import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.domain.model.owner.OwnerService;
import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.domain.model.pet.Pet;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.domain.model.pet.PetService;
import org.springframework.samples.petclinic.domain.model.pet.PetType;
import org.springframework.samples.petclinic.support.web.AbstractResourceController;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
...@@ -37,21 +39,24 @@ import java.util.Map; ...@@ -37,21 +39,24 @@ import java.util.Map;
@RestController @RestController
public class PetResource extends AbstractResourceController { public class PetResource extends AbstractResourceController {
private final ClinicService clinicService; private final PetService petService;
private final OwnerService ownerService;
@Autowired @Autowired
public PetResource(ClinicService clinicService) { public PetResource(PetService petService, OwnerService ownerService) {
this.clinicService = clinicService; this.petService = petService;
this.ownerService = ownerService;
} }
@GetMapping("/petTypes") @GetMapping("/petTypes")
Object getPetTypes() { Object getPetTypes() {
return clinicService.findPetTypes(); return petService.findPetTypes();
} }
@GetMapping("/owners/{ownerId}/pets/new") @GetMapping("/owners/{ownerId}/pets/new")
public String initCreationForm(@PathVariable("ownerId") int ownerId, Map<String, Object> model) { public String initCreationForm(@PathVariable("ownerId") int ownerId, Map<String, Object> model) {
Owner owner = this.clinicService.findOwnerById(ownerId); Owner owner = this.ownerService.findOwnerById(ownerId);
Pet pet = new Pet(); Pet pet = new Pet();
owner.addPet(pet); owner.addPet(pet);
model.put("pet", pet); model.put("pet", pet);
...@@ -65,7 +70,7 @@ public class PetResource extends AbstractResourceController { ...@@ -65,7 +70,7 @@ public class PetResource extends AbstractResourceController {
@PathVariable("ownerId") int ownerId) { @PathVariable("ownerId") int ownerId) {
Pet pet = new Pet(); Pet pet = new Pet();
Owner owner = this.clinicService.findOwnerById(ownerId); Owner owner = this.ownerService.findOwnerById(ownerId);
owner.addPet(pet); owner.addPet(pet);
save(pet, petRequest); save(pet, petRequest);
...@@ -74,7 +79,7 @@ public class PetResource extends AbstractResourceController { ...@@ -74,7 +79,7 @@ public class PetResource extends AbstractResourceController {
@PutMapping("/owners/{ownerId}/pets/{petId}") @PutMapping("/owners/{ownerId}/pets/{petId}")
@ResponseStatus(HttpStatus.NO_CONTENT) @ResponseStatus(HttpStatus.NO_CONTENT)
public void processUpdateForm(@RequestBody PetRequest petRequest) { public void processUpdateForm(@RequestBody PetRequest petRequest) {
save(clinicService.findPetById(petRequest.getId()), petRequest); save(petService.findPetById(petRequest.getId()), petRequest);
} }
private void save(Pet pet, PetRequest petRequest) { private void save(Pet pet, PetRequest petRequest) {
...@@ -82,18 +87,18 @@ public class PetResource extends AbstractResourceController { ...@@ -82,18 +87,18 @@ public class PetResource extends AbstractResourceController {
pet.setName(petRequest.getName()); pet.setName(petRequest.getName());
pet.setBirthDate(petRequest.getBirthDate()); pet.setBirthDate(petRequest.getBirthDate());
for (PetType petType : clinicService.findPetTypes()) { for (PetType petType : petService.findPetTypes()) {
if (petType.getId() == petRequest.getTypeId()) { if (petType.getId() == petRequest.getTypeId()) {
pet.setType(petType); pet.setType(petType);
} }
} }
clinicService.savePet(pet); petService.savePet(pet);
} }
@GetMapping("/owner/*/pet/{petId}") @GetMapping("/owner/*/pet/{petId}")
public PetDetails findPet(@PathVariable("petId") int petId) { public PetDetails findPet(@PathVariable("petId") int petId) {
Pet pet = this.clinicService.findPetById(petId); Pet pet = this.petService.findPetById(petId);
return new PetDetails(pet); return new PetDetails(pet);
} }
......
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.boundary.web.pet;
import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.domain.model.pet.Pet;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
......
...@@ -13,19 +13,17 @@ ...@@ -13,19 +13,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.boundary.web.vet;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.samples.petclinic.domain.model.vet.Vet;
import org.springframework.samples.petclinic.model.Vet; import org.springframework.samples.petclinic.domain.model.vet.VetService;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.support.web.AbstractResourceController;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Mark Fisher * @author Mark Fisher
...@@ -35,15 +33,15 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -35,15 +33,15 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class VetResource extends AbstractResourceController { public class VetResource extends AbstractResourceController {
private final ClinicService clinicService; private final VetService vetService;
@Autowired @Autowired
public VetResource(ClinicService clinicService) { public VetResource(VetService vetService) {
this.clinicService = clinicService; this.vetService = vetService;
} }
@GetMapping("/vets") @GetMapping("/vets")
public Collection<Vet> showResourcesVetList() { public Collection<Vet> showResourcesVetList() {
return this.clinicService.findVets(); return this.vetService.findVets();
} }
} }
...@@ -13,18 +13,15 @@ ...@@ -13,18 +13,15 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.boundary.web.visit;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.domain.model.pet.PetService;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.domain.model.visit.Visit;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.samples.petclinic.domain.model.visit.VisitService;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.samples.petclinic.support.web.AbstractResourceController;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid; import javax.validation.Valid;
...@@ -37,11 +34,14 @@ import javax.validation.Valid; ...@@ -37,11 +34,14 @@ import javax.validation.Valid;
@RestController @RestController
public class VisitResource extends AbstractResourceController { public class VisitResource extends AbstractResourceController {
private final ClinicService clinicService; private final VisitService visitService;
private final PetService petService;
@Autowired @Autowired
public VisitResource(ClinicService clinicService) { public VisitResource(VisitService visitService, PetService petService) {
this.clinicService = clinicService; this.visitService = visitService;
this.petService = petService;
} }
@PostMapping("/owners/{ownerId}/pets/{petId}/visits") @PostMapping("/owners/{ownerId}/pets/{petId}/visits")
...@@ -50,12 +50,12 @@ public class VisitResource extends AbstractResourceController { ...@@ -50,12 +50,12 @@ public class VisitResource extends AbstractResourceController {
@Valid @RequestBody Visit visit, @Valid @RequestBody Visit visit,
@PathVariable("petId") int petId) { @PathVariable("petId") int petId) {
clinicService.findPetById(petId).addVisit(visit); petService.findPetById(petId).addVisit(visit);
clinicService.saveVisit(visit); visitService.saveVisit(visit);
} }
@GetMapping("/owners/{ownerId}/pets/{petId}/visits") @GetMapping("/owners/{ownerId}/pets/{petId}/visits")
public Object visits(@PathVariable("petId") int petId) { public Object visits(@PathVariable("petId") int petId) {
return clinicService.findPetById(petId).getVisits(); return petService.findPetById(petId).getVisits();
} }
} }
...@@ -13,25 +13,18 @@ ...@@ -13,25 +13,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.domain.model.owner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Digits;
import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.NotEmpty;
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.core.style.ToStringCreator; import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.domain.model.pet.Pet;
import org.springframework.samples.petclinic.domain.shared.Person;
import javax.persistence.*;
import javax.validation.constraints.Digits;
import java.util.*;
/** /**
* Simple JavaBean domain object representing an owner. * Simple JavaBean domain object representing an owner.
......
...@@ -13,17 +13,9 @@ ...@@ -13,17 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.repository; package org.springframework.samples.petclinic.domain.model.owner;
import java.util.Collection;
import org.springframework.dao.DataAccessException;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
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.model.BaseEntity;
import org.springframework.samples.petclinic.model.Owner;
/** /**
* Repository class for <code>Owner</code> domain objects All method names are compliant with Spring Data naming * Repository class for <code>Owner</code> domain objects All method names are compliant with Spring Data naming
......
package org.springframework.samples.petclinic.domain.model.owner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
/**
* @author mszarlinski on 2016-10-30.
*/
@Service
public class OwnerService {
private final OwnerRepository ownerRepository;
@Autowired
public OwnerService(OwnerRepository ownerRepository) {
this.ownerRepository = ownerRepository;
}
@Transactional(readOnly = true)
public Owner findOwnerById(int id) throws DataAccessException {
return ownerRepository.findOne(id);
}
@Transactional(readOnly = true)
public Collection<Owner> findAll() throws DataAccessException {
return ownerRepository.findAll();
}
@Transactional
public void saveOwner(Owner owner) throws DataAccessException {
ownerRepository.save(owner);
}
}
...@@ -13,28 +13,17 @@ ...@@ -13,28 +13,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.domain.model.pet;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
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.samples.petclinic.domain.model.owner.Owner;
import org.springframework.samples.petclinic.domain.model.visit.Visit;
import org.springframework.samples.petclinic.support.jpa.NamedEntity;
import javax.persistence.CascadeType; import javax.persistence.*;
import javax.persistence.Column; import java.util.*;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** /**
* Simple business object representing a pet. * Simple business object representing a pet.
...@@ -80,7 +69,7 @@ public class Pet extends NamedEntity { ...@@ -80,7 +69,7 @@ public class Pet extends NamedEntity {
return this.type; return this.type;
} }
protected void setOwner(Owner owner) { public void setOwner(Owner owner) {
this.owner = owner; this.owner = owner;
} }
...@@ -94,7 +83,7 @@ public class Pet extends NamedEntity { ...@@ -94,7 +83,7 @@ public class Pet extends NamedEntity {
protected Set<Visit> getVisitsInternal() { protected Set<Visit> getVisitsInternal() {
if (this.visits == null) { if (this.visits == null) {
this.visits = new HashSet<Visit>(); this.visits = new HashSet<>();
} }
return this.visits; return this.visits;
} }
......
...@@ -13,16 +13,12 @@ ...@@ -13,16 +13,12 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.repository; package org.springframework.samples.petclinic.domain.model.pet;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
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.Pet; import java.util.List;
import org.springframework.samples.petclinic.model.PetType;
/** /**
* Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming * Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming
......
package org.springframework.samples.petclinic.domain.model.pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
/**
* @author mszarlinski on 2016-10-30.
*/
@Service
public class PetService {
private final PetRepository petRepository;
@Autowired
public PetService(PetRepository petRepository) {
this.petRepository = petRepository;
}
@Transactional(readOnly = true)
public Pet findPetById(int id) throws DataAccessException {
return petRepository.findById(id);
}
@Transactional
public void savePet(Pet pet) throws DataAccessException {
petRepository.save(pet);
}
@Transactional(readOnly = true)
public Collection<PetType> findPetTypes() throws DataAccessException {
return petRepository.findPetTypes();
}
}
...@@ -13,7 +13,9 @@ ...@@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.domain.model.pet;
import org.springframework.samples.petclinic.support.jpa.NamedEntity;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table; import javax.persistence.Table;
......
...@@ -13,7 +13,9 @@ ...@@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.domain.model.vet;
import org.springframework.samples.petclinic.support.jpa.NamedEntity;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table; import javax.persistence.Table;
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.domain.model.vet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
...@@ -31,6 +31,7 @@ import javax.xml.bind.annotation.XmlElement; ...@@ -31,6 +31,7 @@ import javax.xml.bind.annotation.XmlElement;
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.samples.petclinic.domain.shared.Person;
/** /**
* Simple JavaBean domain object representing a veterinarian. * Simple JavaBean domain object representing a veterinarian.
......
...@@ -13,13 +13,12 @@ ...@@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.repository; package org.springframework.samples.petclinic.domain.model.vet;
import java.util.Collection;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.model.Vet;
import java.util.Collection;
/** /**
* Repository class for <code>Vet</code> domain objects All method names are compliant with Spring Data naming * Repository class for <code>Vet</code> domain objects All method names are compliant with Spring Data naming
......
package org.springframework.samples.petclinic.domain.model.vet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.cache.annotation.CacheResult;
import java.util.Collection;
/**
* @author mszarlinski on 2016-10-30.
*/
@Service
public class VetService {
private final VetRepository vetRepository;
@Autowired
public VetService(VetRepository vetRepository) {
this.vetRepository = vetRepository;
}
@Transactional(readOnly = true)
@CacheResult(cacheName = "vets")
public Collection<Vet> findVets() throws DataAccessException {
return vetRepository.findAll();
}
}
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.domain.model.vet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -36,7 +36,7 @@ public class Vets { ...@@ -36,7 +36,7 @@ public class Vets {
@XmlElement @XmlElement
public List<Vet> getVetList() { public List<Vet> getVetList() {
if (vets == null) { if (vets == null) {
vets = new ArrayList<Vet>(); vets = new ArrayList<>();
} }
return vets; return vets;
} }
......
...@@ -13,18 +13,14 @@ ...@@ -13,18 +13,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.domain.model.visit;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.samples.petclinic.domain.model.pet.Pet;
import org.springframework.samples.petclinic.support.jpa.BaseEntity;
import javax.persistence.Column; import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import java.util.Date; import java.util.Date;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment