diff --git a/PFE06/src/main/java/com/PFE/ServerManager/Customer.java b/PFE06/src/main/java/com/PFE/ServerManager/Customer.java index a494411..5082a68 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/Customer.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/Customer.java @@ -10,6 +10,7 @@ import javax.persistence.Table; @Entity // This tells Hibernate to make a table out of this class @Table(name = "Customer") // DON'T USE "User" because it is a reserved name in PostgreSQL public class Customer{ + @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; @@ -44,5 +45,4 @@ public class Customer{ this.password = password; } - } \ No newline at end of file diff --git a/PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java b/PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java index 74b65fe..7bea20f 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java @@ -1,12 +1,17 @@ package com.PFE.ServerManager; +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; // This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository // CRUD refers Create, Read, Update, Delete //This class allows the JPA to know that the Customer class is a table //Need to check if "extends JpaRepository is more useful" +@Repository public interface CustomerRepository extends CrudRepository { -} \ No newline at end of file + Customer findByPseudo(String pseudo); + +} diff --git a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java index 316190a..c8cfb8e 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java @@ -2,10 +2,7 @@ package com.PFE.ServerManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.*; import java.util.Map; @@ -15,20 +12,30 @@ public class MainController { CustomerRepository customerRepository; @RequestMapping(value="/") - public String login(){ - return "login"; + public String home(){ + return "redirect:login"; } @GetMapping(path="/login") // Map ONLY GET Requests - public String addNewUser (@RequestParam String pseudo, @RequestParam String password, Map model) { + public String login() { + return "login"; //return "redirect:/...."; //to send a request to redirect the current page + } + + @PostMapping(path="/login") + public String addNewUser(@RequestParam String pseudo, @RequestParam String password) { // @RequestParam means it is a parameter from the GET or POST request //the model Map is used by thymeleaf as a storage for values display on the html page - model.put("message", "vous avez ajouté l'utilisateur : "+pseudo +", avec le pseudo : "+ password); Customer n = new Customer(); n.setPseudo(pseudo); n.setPassword(password); + Customer temp = customerRepository.findByPseudo(pseudo); + + if(temp != null) { + return "redirect:login?error"; + } + customerRepository.save(n); - return "login"; //return "redirect:/...."; //to send a request to redirect the current page + return "redirect:login?ok"; } @GetMapping(path="/all") diff --git a/PFE06/src/main/resources/application.properties b/PFE06/src/main/resources/application.properties index 3ec1656..b1d84a5 100644 --- a/PFE06/src/main/resources/application.properties +++ b/PFE06/src/main/resources/application.properties @@ -5,6 +5,6 @@ spring.jpa.hibernate.ddl-auto=update #"create" if the database doesn't exist : it will reinitialize the DB every time the process is restarted #"update" if the database already exists -spring.datasource.url=jdbc:postgresql://localhost/sql_only +spring.datasource.url=jdbc:postgresql://localhost:3306/sql_only spring.datasource.username=postgres -spring.datasource.password=idalurf123 \ No newline at end of file +spring.datasource.password=admin \ No newline at end of file diff --git a/PFE06/src/main/resources/templates/login.html b/PFE06/src/main/resources/templates/login.html index a760db3..44cc934 100644 --- a/PFE06/src/main/resources/templates/login.html +++ b/PFE06/src/main/resources/templates/login.html @@ -7,7 +7,7 @@
Add new users
-
+
@@ -18,8 +18,11 @@
-
- +
+ L'utilisateur a été ajouté ! +
+
+ Le pseudo existe déjà !
-- libgit2 0.21.2