Skip to content

Commit

Permalink
Added dices on session controller and improved PetService.
Browse files Browse the repository at this point in the history
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!)
  • Loading branch information
japarejo committed Nov 25, 2021
1 parent 9806b13 commit 1a7581c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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<NUM_DICES;i++)
dices[i]=1+(int)Math.floor(Math.random()*NUM_DICES_SIDES);
session.setAttribute("dices", dices);
return dices;
}

@GetMapping("/session/sumdices")
public @ResponseBody Integer sumDices(HttpSession session){
Integer[] dices=(Integer[])session.getAttribute("dices");
Integer result=0;
for(int i=0;i<NUM_DICES;i++)
result+=dices[i];
return result;
}
}
1 change: 1 addition & 0 deletions src/main/resources/db/hsqldb/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ INSERT INTO types VALUES (3, 'lizard');
INSERT INTO types VALUES (4, 'snake');
INSERT INTO types VALUES (5, 'bird');
INSERT INTO types VALUES (6, 'hamster');
INSERT INTO types VALUES (7, 'turtle');

INSERT INTO owners VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023', 'owner1');
INSERT INTO owners VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749', 'owner1');
Expand Down

0 comments on commit 1a7581c

Please sign in to comment.