forked from gii-is-DP2/spring-petclinic
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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!)
- Loading branch information
Showing
4 changed files
with
42 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters