Commit 716532ee956b318a5cc985cc4997b743193f8842

Authored by Zak
1 parent ffbf5d61

Modifications des REST

@@ -41,6 +41,10 @@ @@ -41,6 +41,10 @@
41 <artifactId>spring-boot-starter-tomcat</artifactId> 41 <artifactId>spring-boot-starter-tomcat</artifactId>
42 <scope>provided</scope> 42 <scope>provided</scope>
43 </dependency> 43 </dependency>
  44 + <dependency>
  45 + <groupId>org.springframework.boot</groupId>
  46 + <artifactId>spring-boot-devtools</artifactId>
  47 + </dependency>
44 <dependency> 48 <dependency>
45 <groupId>org.springframework.boot</groupId> 49 <groupId>org.springframework.boot</groupId>
46 <artifactId>spring-boot-starter-security</artifactId> 50 <artifactId>spring-boot-starter-security</artifactId>
src/main/java/fr/plil/sio/web/mvc/Dette.java
@@ -16,42 +16,17 @@ public class Dette { @@ -16,42 +16,17 @@ public class Dette {
16 16
17 17
18 @ManyToOne(optional = false) 18 @ManyToOne(optional = false)
19 - private User dueToUser; 19 + private User creditor;
20 20
21 @ManyToOne(optional = false) 21 @ManyToOne(optional = false)
22 - private User holderOfDebt;  
23 -  
24 - public Double getSommeDette() {  
25 - return sommeDette;  
26 - }  
27 -  
28 - public void setSommeDette(Double sommeDette) {  
29 - this.sommeDette = sommeDette;  
30 - }  
31 - 22 + private User borrower;
32 23
33 - public User getDueToUser() {  
34 - return dueToUser;  
35 - }  
36 -  
37 - public void setDueToUser(User dueToUser) {  
38 - this.dueToUser = dueToUser;  
39 - }  
40 -  
41 - public User getHolderOfDebt() {  
42 - return holderOfDebt;  
43 - }  
44 -  
45 - public void setHolderOfDebt(User holderOfDebt) {  
46 - this.holderOfDebt = holderOfDebt;  
47 - }  
48 -  
49 -  
50 -  
51 @Override 24 @Override
52 public int hashCode() { 25 public int hashCode() {
53 - int hash = 5;  
54 - hash = 71 * hash + Objects.hashCode(this.sommeDette); 26 + int hash = 7;
  27 + hash = 97 * hash + Objects.hashCode(this.sommeDette);
  28 + hash = 97 * hash + Objects.hashCode(this.creditor);
  29 + hash = 97 * hash + Objects.hashCode(this.borrower);
55 return hash; 30 return hash;
56 } 31 }
57 32
@@ -64,18 +39,45 @@ public class Dette { @@ -64,18 +39,45 @@ public class Dette {
64 return false; 39 return false;
65 } 40 }
66 final Dette other = (Dette) obj; 41 final Dette other = (Dette) obj;
  42 + if (!Objects.equals(this.id, other.id)) {
  43 + return false;
  44 + }
67 if (!Objects.equals(this.sommeDette, other.sommeDette)) { 45 if (!Objects.equals(this.sommeDette, other.sommeDette)) {
68 return false; 46 return false;
69 } 47 }
70 - if (!Objects.equals(this.dueToUser, other.dueToUser)) { 48 + if (!Objects.equals(this.creditor, other.creditor)) {
71 return false; 49 return false;
72 } 50 }
73 - if (!Objects.equals(this.holderOfDebt, other.holderOfDebt)) { 51 + if (!Objects.equals(this.borrower, other.borrower)) {
74 return false; 52 return false;
75 } 53 }
76 return true; 54 return true;
77 } 55 }
78 56
  57 + public User getCreditor() {
  58 + return creditor;
  59 + }
  60 +
  61 + public void setCreditor(User creditor) {
  62 + this.creditor = creditor;
  63 + }
  64 +
  65 + public User getBorrower() {
  66 + return borrower;
  67 + }
  68 +
  69 + public void setBorrower(User borrower) {
  70 + this.borrower = borrower;
  71 + }
  72 +
  73 + public Double getSommeDette() {
  74 + return sommeDette;
  75 + }
  76 +
  77 + public void setSommeDette(Double sommeDette) {
  78 + this.sommeDette = sommeDette;
  79 + }
  80 +
79 81
80 82
81 83
src/main/java/fr/plil/sio/web/mvc/UserRestController.java
@@ -25,13 +25,13 @@ public class UserRestController { @@ -25,13 +25,13 @@ public class UserRestController {
25 return userService.findByUsername(username); 25 return userService.findByUsername(username);
26 } 26 }
27 27
28 - @RequestMapping(value = "/api/debts/{username}/", method = RequestMethod.GET)  
29 - public List<Dette> listDettes(@PathVariable String username) {  
30 - return userService.findDettesByUsername(username); 28 + @RequestMapping(value = "/api/debts/", method = RequestMethod.GET)
  29 + public List<Dette> listDettes() {
  30 + return userService.findDettes();
31 } 31 }
32 32
33 @RequestMapping(value = "/api/addDette/", method = RequestMethod.POST) 33 @RequestMapping(value = "/api/addDette/", method = RequestMethod.POST)
34 - public boolean addDette(@RequestParam(value="username",required=false) String username,@RequestParam(value="sommeDette",required=false) String sommeDette) { 34 + public Dette addDette(@RequestParam(value="username",required=false) String username,@RequestParam(value="sommeDette",required=false) String sommeDette) {
35 return userService.addDette(username,sommeDette); 35 return userService.addDette(username,sommeDette);
36 } 36 }
37 37
src/main/java/fr/plil/sio/web/mvc/UserService.java
@@ -9,9 +9,9 @@ public interface UserService { @@ -9,9 +9,9 @@ public interface UserService {
9 9
10 User findByUsername(String username); 10 User findByUsername(String username);
11 11
12 - List<Dette> findDettesByUsername(String username); 12 + List<Dette> findDettes();
13 13
14 List<User> findAll(); 14 List<User> findAll();
15 15
16 - boolean addDette(String username,String sommeDette); 16 + Dette addDette(String username,String sommeDette);
17 } 17 }
src/main/webapp/WEB-INF/pages/viewUsers.jsp
@@ -7,13 +7,17 @@ @@ -7,13 +7,17 @@
7 </jsp:include> 7 </jsp:include>
8 <body> 8 <body>
9 9
  10 + <div id="dettes">
  11 +
  12 + </div>
  13 +
  14 +
10 <form> 15 <form>
11 <div class="form-group"> 16 <div class="form-group">
12 <label for="debt">Somme de la dette</label> 17 <label for="debt">Somme de la dette</label>
13 <input type="number" class="form-control" id="sommeDette"/> 18 <input type="number" class="form-control" id="sommeDette"/>
14 <label for="debtFor">A : </label> 19 <label for="debtFor">A : </label>
15 <input type="text" class="form-control" id="username"/> 20 <input type="text" class="form-control" id="username"/>
16 -  
17 </div> 21 </div>
18 <button type="submit" onclick="updateDebtOfUser()">Update</button> 22 <button type="submit" onclick="updateDebtOfUser()">Update</button>
19 </form> 23 </form>
src/main/webapp/js/kawafile.js
1 var debtsOfUser = []; 1 var debtsOfUser = [];
2 2
3 -function getDebtsOfUser(username){  
4 - var url = "/api/userDebts"+username;  
5 -  
6 - $.getJson(url,function(d){  
7 - debtsOfUser = d; 3 +function getDebtsOfUser(){
  4 + var url = "/api/debts/";
  5 + var html ="";
  6 + $.getJSON(url,function(d){
  7 + html+="<ul>";
  8 + $.each(d,function(index,item){
  9 + html+="<li>"+item.holderOfDebt.username+"</li>";
  10 + html+="<li>"+item.sommeDette+"</li>";
  11 + });
8 }); 12 });
  13 + $("#dettes").html(html);
9 } 14 }
10 15
11 function updateDebtOfUser(){ 16 function updateDebtOfUser(){
@@ -36,13 +41,17 @@ function updateDebtOfUser(){ @@ -36,13 +41,17 @@ function updateDebtOfUser(){
36 $.ajax({ 41 $.ajax({
37 url:url, 42 url:url,
38 method:"POST", 43 method:"POST",
39 - data:requestData 44 + data:requestData,
  45 + async:false,
40 }).done(function(d){ 46 }).done(function(d){
41 alert("Ok"); 47 alert("Ok");
  48 + getDebtsOfUser();
42 }).success(function(d){ 49 }).success(function(d){
43 - console.log(d); 50 + getDebtsOfUser();
  51 + }).fail(function(d){
  52 + alert("Erreur");
44 }); 53 });
45 -// 54 + getDebtsOfUser();
46 } 55 }
47 56
48 57
@@ -50,7 +59,7 @@ function updateDebtOfUser(){ @@ -50,7 +59,7 @@ function updateDebtOfUser(){
50 'use strict'; 59 'use strict';
51 60
52 61
53 - 62 + getDebtsOfUser();
54 63
55 64
56 })(); 65 })();