From 1a7581c937b7b904b07c969bb51a7929a39a9e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Thu, 25 Nov 2021 13:47:21 +0100 Subject: [PATCH] Added dices on session controller and improved PetService. In orded to illustrate the use of sesion context we have created the DicesOnSessionController. Additionally we have improved the implementation of savePet (in PetService), and added a new PetType (turtles!) --- .../configuration/SecurityConfiguration.java | 1 + .../samples/petclinic/pet/PetService.java | 13 +++++--- .../web/DicesOnSessionController.java | 32 +++++++++++++++++++ src/main/resources/db/hsqldb/data.sql | 1 + 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java diff --git a/src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java index 04ba8db1c76..4ac8f0844c6 100644 --- a/src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java @@ -36,6 +36,7 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers("/resources/**","/webjars/**","/h2-console/**").permitAll() .antMatchers(HttpMethod.GET, "/","/oups").permitAll() .antMatchers("/users/new").permitAll() + .antMatchers("/session/**").permitAll() .antMatchers("/admin/**").hasAnyAuthority("admin") .antMatchers("/owners/**").hasAnyAuthority("owner","admin") .antMatchers("/vets/**").authenticated() diff --git a/src/main/java/org/springframework/samples/petclinic/pet/PetService.java b/src/main/java/org/springframework/samples/petclinic/pet/PetService.java index 09331bb7857..ce0002fb1b8 100644 --- a/src/main/java/org/springframework/samples/petclinic/pet/PetService.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetService.java @@ -62,11 +62,14 @@ public Pet findPetById(int id) throws DataAccessException { @Transactional(rollbackFor = DuplicatedPetNameException.class) public void savePet(Pet pet) throws DataAccessException, DuplicatedPetNameException { - Pet otherPet=pet.getOwner().getPetwithIdDifferent(pet.getName(), pet.getId()); - if (StringUtils.hasLength(pet.getName()) && (otherPet!= null && otherPet.getId()!=pet.getId())) { - throw new DuplicatedPetNameException(); - }else - petRepository.save(pet); + if(pet.getOwner()!=null){ + Pet otherPet=pet.getOwner().getPetwithIdDifferent(pet.getName(), pet.getId()); + if (StringUtils.hasLength(pet.getName()) && (otherPet!= null && otherPet.getId()!=pet.getId())) { + throw new DuplicatedPetNameException(); + }else + petRepository.save(pet); + }else + petRepository.save(pet); } diff --git a/src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java b/src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java new file mode 100644 index 00000000000..2a8522b0ad7 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java @@ -0,0 +1,32 @@ +package org.springframework.samples.petclinic.web; + +import javax.servlet.http.HttpSession; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class DicesOnSessionController { + + public static int NUM_DICES=5; + public static int NUM_DICES_SIDES=6; + + @GetMapping("/session/rolldices") + public @ResponseBody Integer[] rollDices(HttpSession session){ + Integer[] dices=new Integer[NUM_DICES]; + for(int i=0;i