Skip to content

Commit

Permalink
Fixed tests and separation of services by entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
japarejo committed Feb 12, 2020
1 parent c44003c commit 874b4af
Show file tree
Hide file tree
Showing 18 changed files with 479 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public void addPet(Pet pet) {
getPetsInternal().add(pet);
pet.setOwner(this);
}

public boolean removePet(Pet pet) {
return getPetsInternal().remove(pet);
}

/**
* Return the Pet with the given name, or null if none found for this Owner.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2002-2013 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
*
* http://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.service;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
* Mostly used as a facade for all Petclinic controllers Also a placeholder
* for @Transactional and @Cacheable annotations
*
* @author Michael Isvy
*/
@Service
public class OwnerService {

private OwnerRepository ownerRepository;

@Autowired
private UserService userService;

@Autowired
private AuthoritiesService authoritiesService;

@Autowired
public OwnerService(OwnerRepository ownerRepository) {
this.ownerRepository = ownerRepository;
}

@Transactional(readOnly = true)
public Owner findOwnerById(int id) throws DataAccessException {
return ownerRepository.findById(id);
}

@Transactional(readOnly = true)
public Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException {
return ownerRepository.findByLastName(lastName);
}

@Transactional
public void saveOwner(Owner owner) throws DataAccessException {
//creating owner
ownerRepository.save(owner);
//creating user
userService.saveUser(owner.getUser());
//creating authorities
authoritiesService.saveAuthorities(owner.getUser().getUsername(), "owner");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,56 +41,25 @@
* @author Michael Isvy
*/
@Service
public class ClinicService {
public class PetService {

private PetRepository petRepository;

private VetRepository vetRepository;

private OwnerRepository ownerRepository;

private VisitRepository visitRepository;

@Autowired
private UserService userService;
private VisitRepository visitRepository;

@Autowired
private AuthoritiesService authoritiesService;

@Autowired
public ClinicService(PetRepository petRepository, VetRepository vetRepository, OwnerRepository ownerRepository,
public PetService(PetRepository petRepository,
VisitRepository visitRepository) {
this.petRepository = petRepository;
this.vetRepository = vetRepository;
this.ownerRepository = ownerRepository;
this.visitRepository = visitRepository;
}

@Transactional(readOnly = true)
public Collection<PetType> findPetTypes() throws DataAccessException {
return petRepository.findPetTypes();
}

@Transactional(readOnly = true)
public Owner findOwnerById(int id) throws DataAccessException {
return ownerRepository.findById(id);
}

@Transactional(readOnly = true)
public Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException {
return ownerRepository.findByLastName(lastName);
}

@Transactional
public void saveOwner(Owner owner) throws DataAccessException {
//creating owner
ownerRepository.save(owner);
//creating user
userService.saveUser(owner.getUser());
//creating authorities
authoritiesService.saveAuthorities(owner.getUser().getUsername(), "owner");
}


@Transactional
public void saveVisit(Visit visit) throws DataAccessException {
visitRepository.save(visit);
Expand All @@ -101,19 +70,15 @@ public Pet findPetById(int id) throws DataAccessException {
return petRepository.findById(id);
}

@Transactional
@Transactional(rollbackFor = DuplicatedPetNameException.class)
public void savePet(Pet pet) throws DataAccessException, DuplicatedPetNameException {
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && pet.getOwner().getPet(pet.getName(), true) != null) {
throw new DuplicatedPetNameException();
}
petRepository.save(pet);
Pet otherPet=pet.getOwner().getPet(pet.getName(), true);
if (StringUtils.hasLength(pet.getName()) && (otherPet!= null && otherPet.getId()!=pet.getId())) {
throw new DuplicatedPetNameException();
}else
petRepository.save(pet);
}

@Transactional(readOnly = true)
@Cacheable(value = "vets")
public Collection<Vet> findVets() throws DataAccessException {
return vetRepository.findAll();
}

public Collection<Visit> findVisitsByPetId(int petId) {
return visitRepository.findByPetId(petId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2002-2013 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
*
* http://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.service;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
* Mostly used as a facade for all Petclinic controllers Also a placeholder
* for @Transactional and @Cacheable annotations
*
* @author Michael Isvy
*/
@Service
public class VetService {

private VetRepository vetRepository;


@Autowired
public VetService(VetRepository vetRepository) {
this.vetRepository = vetRepository;
}

@Transactional(readOnly = true)
public Collection<Vet> findVets() throws DataAccessException {
return vetRepository.findAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.service.AuthoritiesService;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.service.OwnerService;
import org.springframework.samples.petclinic.service.VetService;
import org.springframework.samples.petclinic.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -43,11 +44,11 @@ public class OwnerController {

private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";

private final ClinicService clinicService;
private final OwnerService ownerService;

@Autowired
public OwnerController(ClinicService clinicService, UserService userService, AuthoritiesService authoritiesService) {
this.clinicService = clinicService;
public OwnerController(OwnerService ownerService, UserService userService, AuthoritiesService authoritiesService) {
this.ownerService = ownerService;
}

@InitBinder
Expand All @@ -69,7 +70,7 @@ public String processCreationForm(@Valid Owner owner, BindingResult result) {
}
else {
//creating owner, user and authorities
this.clinicService.saveOwner(owner);
this.ownerService.saveOwner(owner);

return "redirect:/owners/" + owner.getId();
}
Expand All @@ -90,7 +91,7 @@ public String processFindForm(Owner owner, BindingResult result, Map<String, Obj
}

// find owners by last name
Collection<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName());
Collection<Owner> results = this.ownerService.findOwnerByLastName(owner.getLastName());
if (results.isEmpty()) {
// no owners found
result.rejectValue("lastName", "notFound", "not found");
Expand All @@ -110,7 +111,7 @@ else if (results.size() == 1) {

@GetMapping(value = "/owners/{ownerId}/edit")
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.clinicService.findOwnerById(ownerId);
Owner owner = this.ownerService.findOwnerById(ownerId);
model.addAttribute(owner);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
Expand All @@ -123,7 +124,7 @@ public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
}
else {
owner.setId(ownerId);
this.clinicService.saveOwner(owner);
this.ownerService.saveOwner(owner);
return "redirect:/owners/{ownerId}";
}
}
Expand All @@ -136,7 +137,7 @@ public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
@GetMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails");
mav.addObject(this.clinicService.findOwnerById(ownerId));
mav.addObject(this.ownerService.findOwnerById(ownerId));
return mav;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.service.VetService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
Expand All @@ -35,6 +35,8 @@
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.service.OwnerService;
import org.springframework.samples.petclinic.service.PetService;
import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException;

/**
Expand All @@ -48,21 +50,23 @@ public class PetController {

private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";

private final ClinicService clinicService;
private final PetService petService;
private final OwnerService ownerService;

@Autowired
public PetController(ClinicService clinicService) {
this.clinicService = clinicService;
public PetController(PetService petService, OwnerService ownerService) {
this.petService = petService;
this.ownerService = ownerService;
}

@ModelAttribute("types")
public Collection<PetType> populatePetTypes() {
return this.clinicService.findPetTypes();
return this.petService.findPetTypes();
}

@ModelAttribute("owner")
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
return this.clinicService.findOwnerById(ownerId);
return this.ownerService.findOwnerById(ownerId);
}

/*@ModelAttribute("pet")
Expand Down Expand Up @@ -101,8 +105,8 @@ public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult res
}
else {
try{
owner.addPet(pet);
this.clinicService.savePet(pet);
owner.addPet(pet);
this.petService.savePet(pet);
}catch(DuplicatedPetNameException ex){
result.rejectValue("name", "duplicate", "already exists");
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
Expand All @@ -113,7 +117,7 @@ public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult res

@GetMapping(value = "/pets/{petId}/edit")
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
Pet pet = this.clinicService.findPetById(petId);
Pet pet = this.petService.findPetById(petId);
model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
}
Expand All @@ -135,10 +139,10 @@ public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owne
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
}
else {
Pet petToUpdate=this.clinicService.findPetById(petId);
Pet petToUpdate=this.petService.findPetById(petId);
BeanUtils.copyProperties(pet, petToUpdate, "id","owner","visits");
try {
this.clinicService.savePet(petToUpdate);
this.petService.savePet(petToUpdate);
} catch (DuplicatedPetNameException ex) {
result.rejectValue("name", "duplicate", "already exists");
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
Expand Down
Loading

0 comments on commit 874b4af

Please sign in to comment.