Commit e743b1b9772946a4008521c9257d087dd0de46b1
1 parent
e1305e8c
Ajout d'un utilisateur si non présent
Lors de l'ajout d'un utilisateur, vérification dans la base de données si le pseudo est libre
Showing
5 changed files
with
31 additions
and
16 deletions
Show diff stats
PFE06/src/main/java/com/PFE/ServerManager/Customer.java
@@ -10,6 +10,7 @@ import javax.persistence.Table; | @@ -10,6 +10,7 @@ import javax.persistence.Table; | ||
10 | @Entity // This tells Hibernate to make a table out of this class | 10 | @Entity // This tells Hibernate to make a table out of this class |
11 | @Table(name = "Customer") // DON'T USE "User" because it is a reserved name in PostgreSQL | 11 | @Table(name = "Customer") // DON'T USE "User" because it is a reserved name in PostgreSQL |
12 | public class Customer{ | 12 | public class Customer{ |
13 | + | ||
13 | @Id | 14 | @Id |
14 | @GeneratedValue(strategy=GenerationType.AUTO) | 15 | @GeneratedValue(strategy=GenerationType.AUTO) |
15 | private Integer id; | 16 | private Integer id; |
@@ -44,5 +45,4 @@ public class Customer{ | @@ -44,5 +45,4 @@ public class Customer{ | ||
44 | this.password = password; | 45 | this.password = password; |
45 | } | 46 | } |
46 | 47 | ||
47 | - | ||
48 | } | 48 | } |
49 | \ No newline at end of file | 49 | \ No newline at end of file |
PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java
1 | package com.PFE.ServerManager; | 1 | package com.PFE.ServerManager; |
2 | 2 | ||
3 | +import org.springframework.data.jpa.repository.JpaRepository; | ||
3 | import org.springframework.data.repository.CrudRepository; | 4 | import org.springframework.data.repository.CrudRepository; |
5 | +import org.springframework.stereotype.Repository; | ||
4 | 6 | ||
5 | // This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository | 7 | // This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository |
6 | // CRUD refers Create, Read, Update, Delete | 8 | // CRUD refers Create, Read, Update, Delete |
7 | 9 | ||
8 | //This class allows the JPA to know that the Customer class is a table | 10 | //This class allows the JPA to know that the Customer class is a table |
9 | //Need to check if "extends JpaRepository is more useful" | 11 | //Need to check if "extends JpaRepository is more useful" |
12 | +@Repository | ||
10 | public interface CustomerRepository extends CrudRepository<Customer, Integer> { | 13 | public interface CustomerRepository extends CrudRepository<Customer, Integer> { |
11 | 14 | ||
12 | -} | ||
13 | \ No newline at end of file | 15 | \ No newline at end of file |
16 | + Customer findByPseudo(String pseudo); | ||
17 | + | ||
18 | +} |
PFE06/src/main/java/com/PFE/ServerManager/MainController.java
@@ -2,10 +2,7 @@ package com.PFE.ServerManager; | @@ -2,10 +2,7 @@ package com.PFE.ServerManager; | ||
2 | 2 | ||
3 | import org.springframework.beans.factory.annotation.Autowired; | 3 | import org.springframework.beans.factory.annotation.Autowired; |
4 | import org.springframework.stereotype.Controller; | 4 | import org.springframework.stereotype.Controller; |
5 | -import org.springframework.web.bind.annotation.RequestMapping; | ||
6 | -import org.springframework.web.bind.annotation.GetMapping; | ||
7 | -import org.springframework.web.bind.annotation.RequestParam; | ||
8 | -import org.springframework.web.bind.annotation.ResponseBody; | 5 | +import org.springframework.web.bind.annotation.*; |
9 | 6 | ||
10 | import java.util.Map; | 7 | import java.util.Map; |
11 | 8 | ||
@@ -15,20 +12,30 @@ public class MainController { | @@ -15,20 +12,30 @@ public class MainController { | ||
15 | CustomerRepository customerRepository; | 12 | CustomerRepository customerRepository; |
16 | 13 | ||
17 | @RequestMapping(value="/") | 14 | @RequestMapping(value="/") |
18 | - public String login(){ | ||
19 | - return "login"; | 15 | + public String home(){ |
16 | + return "redirect:login"; | ||
20 | } | 17 | } |
21 | 18 | ||
22 | @GetMapping(path="/login") // Map ONLY GET Requests | 19 | @GetMapping(path="/login") // Map ONLY GET Requests |
23 | - public String addNewUser (@RequestParam String pseudo, @RequestParam String password, Map<String, Object> model) { | 20 | + public String login() { |
21 | + return "login"; //return "redirect:/...."; //to send a request to redirect the current page | ||
22 | + } | ||
23 | + | ||
24 | + @PostMapping(path="/login") | ||
25 | + public String addNewUser(@RequestParam String pseudo, @RequestParam String password) { | ||
24 | // @RequestParam means it is a parameter from the GET or POST request | 26 | // @RequestParam means it is a parameter from the GET or POST request |
25 | //the model Map is used by thymeleaf as a storage for values display on the html page | 27 | //the model Map is used by thymeleaf as a storage for values display on the html page |
26 | - model.put("message", "vous avez ajouté l'utilisateur : "+pseudo +", avec le pseudo : "+ password); | ||
27 | Customer n = new Customer(); | 28 | Customer n = new Customer(); |
28 | n.setPseudo(pseudo); | 29 | n.setPseudo(pseudo); |
29 | n.setPassword(password); | 30 | n.setPassword(password); |
31 | + Customer temp = customerRepository.findByPseudo(pseudo); | ||
32 | + | ||
33 | + if(temp != null) { | ||
34 | + return "redirect:login?error"; | ||
35 | + } | ||
36 | + | ||
30 | customerRepository.save(n); | 37 | customerRepository.save(n); |
31 | - return "login"; //return "redirect:/...."; //to send a request to redirect the current page | 38 | + return "redirect:login?ok"; |
32 | } | 39 | } |
33 | 40 | ||
34 | @GetMapping(path="/all") | 41 | @GetMapping(path="/all") |
PFE06/src/main/resources/application.properties
@@ -5,6 +5,6 @@ | @@ -5,6 +5,6 @@ | ||
5 | spring.jpa.hibernate.ddl-auto=update | 5 | spring.jpa.hibernate.ddl-auto=update |
6 | #"create" if the database doesn't exist : it will reinitialize the DB every time the process is restarted | 6 | #"create" if the database doesn't exist : it will reinitialize the DB every time the process is restarted |
7 | #"update" if the database already exists | 7 | #"update" if the database already exists |
8 | -spring.datasource.url=jdbc:postgresql://localhost/sql_only | 8 | +spring.datasource.url=jdbc:postgresql://localhost:3306/sql_only |
9 | spring.datasource.username=postgres | 9 | spring.datasource.username=postgres |
10 | -spring.datasource.password=idalurf123 | ||
11 | \ No newline at end of file | 10 | \ No newline at end of file |
11 | +spring.datasource.password=admin | ||
12 | \ No newline at end of file | 12 | \ No newline at end of file |
PFE06/src/main/resources/templates/login.html
@@ -7,7 +7,7 @@ | @@ -7,7 +7,7 @@ | ||
7 | <body> | 7 | <body> |
8 | <div> | 8 | <div> |
9 | <h5>Add new users</h5> | 9 | <h5>Add new users</h5> |
10 | - <form th:action="@{/login}" method="get"> | 10 | + <form th:action="@{/login}" method="POST"> |
11 | <div class="form1"> | 11 | <div class="form1"> |
12 | <label for="username">User Name: </label> | 12 | <label for="username">User Name: </label> |
13 | <input type="text" id="username" placeholder="Enter UserName" name="pseudo"/> | 13 | <input type="text" id="username" placeholder="Enter UserName" name="pseudo"/> |
@@ -18,8 +18,11 @@ | @@ -18,8 +18,11 @@ | ||
18 | </div> | 18 | </div> |
19 | <button type="submit">Ajouter</button> | 19 | <button type="submit">Ajouter</button> |
20 | </form> | 20 | </form> |
21 | - <div th:if="${message != null}"> | ||
22 | - <span th:text="${message}"></span> | 21 | + <div th:if="${param.ok}"> |
22 | + <span>L'utilisateur a été ajouté !</span> | ||
23 | + </div> | ||
24 | + <div th:if="${param.error}"> | ||
25 | + <span>Le pseudo existe déjà !</span> | ||
23 | </div> | 26 | </div> |
24 | </div> | 27 | </div> |
25 | </body> | 28 | </body> |