Commit e743b1b9772946a4008521c9257d087dd0de46b1

Authored by Antoine Duquenoy
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
PFE06/src/main/java/com/PFE/ServerManager/Customer.java
... ... @@ -10,6 +10,7 @@ import javax.persistence.Table;
10 10 @Entity // This tells Hibernate to make a table out of this class
11 11 @Table(name = "Customer") // DON'T USE "User" because it is a reserved name in PostgreSQL
12 12 public class Customer{
  13 +
13 14 @Id
14 15 @GeneratedValue(strategy=GenerationType.AUTO)
15 16 private Integer id;
... ... @@ -44,5 +45,4 @@ public class Customer{
44 45 this.password = password;
45 46 }
46 47  
47   -
48 48 }
49 49 \ No newline at end of file
... ...
PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java
1 1 package com.PFE.ServerManager;
2 2  
  3 +import org.springframework.data.jpa.repository.JpaRepository;
3 4 import org.springframework.data.repository.CrudRepository;
  5 +import org.springframework.stereotype.Repository;
4 6  
5 7 // This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
6 8 // CRUD refers Create, Read, Update, Delete
7 9  
8 10 //This class allows the JPA to know that the Customer class is a table
9 11 //Need to check if "extends JpaRepository is more useful"
  12 +@Repository
10 13 public interface CustomerRepository extends CrudRepository<Customer, Integer> {
11 14  
12   -}
13 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 2  
3 3 import org.springframework.beans.factory.annotation.Autowired;
4 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 7 import java.util.Map;
11 8  
... ... @@ -15,20 +12,30 @@ public class MainController {
15 12 CustomerRepository customerRepository;
16 13  
17 14 @RequestMapping(value="/")
18   - public String login(){
19   - return "login";
  15 + public String home(){
  16 + return "redirect:login";
20 17 }
21 18  
22 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 26 // @RequestParam means it is a parameter from the GET or POST request
25 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 28 Customer n = new Customer();
28 29 n.setPseudo(pseudo);
29 30 n.setPassword(password);
  31 + Customer temp = customerRepository.findByPseudo(pseudo);
  32 +
  33 + if(temp != null) {
  34 + return "redirect:login?error";
  35 + }
  36 +
30 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 41 @GetMapping(path="/all")
... ...
PFE06/src/main/resources/application.properties
... ... @@ -5,6 +5,6 @@
5 5 spring.jpa.hibernate.ddl-auto=update
6 6 #"create" if the database doesn't exist : it will reinitialize the DB every time the process is restarted
7 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 9 spring.datasource.username=postgres
10   -spring.datasource.password=idalurf123
11 10 \ No newline at end of file
  11 +spring.datasource.password=admin
12 12 \ No newline at end of file
... ...
PFE06/src/main/resources/templates/login.html
... ... @@ -7,7 +7,7 @@
7 7 <body>
8 8 <div>
9 9 <h5>Add new users</h5>
10   - <form th:action="@{/login}" method="get">
  10 + <form th:action="@{/login}" method="POST">
11 11 <div class="form1">
12 12 <label for="username">User Name: </label>
13 13 <input type="text" id="username" placeholder="Enter UserName" name="pseudo"/>
... ... @@ -18,8 +18,11 @@
18 18 </div>
19 19 <button type="submit">Ajouter</button>
20 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 26 </div>
24 27 </div>
25 28 </body>
... ...