From f8ff1584c823163d9c009e1a0f7565405ed19ae8 Mon Sep 17 00:00:00 2001 From: sfeutrie Date: Wed, 23 Jan 2019 17:52:29 +0100 Subject: [PATCH] ajout de l'interface pour ajouter des groupes --- PFE06/src/main/java/com/PFE/ServerManager/MainController.java | 66 +++++++++++++++++++++++++++++++++++++----------------------------- PFE06/src/main/java/com/PFE/ServerManager/TeamRepository.java | 2 ++ PFE06/src/main/resources/templates/login.html | 4 ++-- PFE06/src/main/resources/templates/registration.html | 62 ++++++++++++++++++++++++++++++++++++++++++-------------------- 4 files changed, 83 insertions(+), 51 deletions(-) diff --git a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java index 8d3484d..dc2867e 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java @@ -1,6 +1,7 @@ package com.PFE.ServerManager; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.repository.query.Param; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -102,12 +103,16 @@ public class MainController { } @GetMapping(path="/registration") - public ModelAndView registration() { + public ModelAndView registration(@RequestParam(required=false) String message, @RequestParam(required=false) Integer succeed) { ModelAndView modelAndView = new ModelAndView(); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Customer customer = customerRepository.findByEmail(auth.getName()); modelAndView.addObject("customerName", customer.getEmail().split("@")[0]); modelAndView.addObject("customerRole", customer.getRole()); + modelAndView.addObject("message", message); + modelAndView.addObject("succeed", succeed); + System.out.println("all teams : " + teamRepository.findAll()); + modelAndView.addObject("allTeams", teamRepository.findAll()); modelAndView.setViewName("registration"); return modelAndView; } @@ -117,40 +122,43 @@ public class MainController { return "denied"; } - @PostMapping(path="/registration") - public ModelAndView addNewUser(@RequestParam String email, @RequestParam String password, @RequestParam String role) { - //Model map, ModelAndView ou l'utilisation direct comme dans la méthode précédente sont 3 méthodes qui permettent d'envoyer des informations et donc de changer l'apparence d'une page - ModelAndView modelAndView = new ModelAndView(); // il n'est peut être pas utile d'utiliser ModelAndView - Customer n = new Customer(); - n.setEmail(email); - n.setPassword(bCryptPasswordEncoder.encode(password)); - n.setCustomerId((int)(customerRepository.count() + 1)); - n.setActive(1); - Customer temp = customerRepository.findByEmail(email); - Role userRole = roleRepository.findByRole(role); - n.addRole(userRole); - - //utilisé uniquement pour continuer à afficher l'utilisateur connecté// - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - Customer customer = customerRepository.findByEmail(auth.getName()); - modelAndView.addObject("customerName", customer.getEmail().split("@")[0]); - modelAndView.addObject("customerRole", customer.getRole()); - modelAndView.setViewName("registration"); + @PostMapping(path="/addTeam") + public String addNewTeam(@RequestParam String teamName){ - List list = customerRepository.findAll(); // attention : la méthode findAll() de JpaRepository retourne une liste alors que celle de CrudRepository retourne un itérable - modelAndView.addObject("list", list); + if(teamRepository.findByTeam(teamName) != null) { + return "redirect:/registration?message=Le groupe&succeed=-1"; + } + else { + Team t = new Team(); + t.setTeam(teamName); + t.setTeamId((int)(teamRepository.count()+1)); + teamRepository.save(t); + return "redirect:/registration?message=Le groupe&succeed=1"; + } + } + + @PostMapping(path="/addUser") + public String addNewUser(@RequestParam String email, @RequestParam String password, @RequestParam String role, @RequestParam String team) { - if(temp != null) { - modelAndView.addObject("message", "L'utilisateur existe déjà !"); - modelAndView.addObject("fail", true); + if(customerRepository.findByEmail(email) != null) { + return "redirect:/registration?message=L'utilisateur&succeed=-1"; } else { - modelAndView.addObject("message", "L'utilisateur a bien été ajouté !"); - modelAndView.addObject("ok", true); + Customer n = new Customer(); + n.setEmail(email); + n.setPassword(bCryptPasswordEncoder.encode(password)); + n.setCustomerId((int)(customerRepository.count() + 1)); + n.setActive(1); + Role userRole = roleRepository.findByRole(role); + n.addRole(userRole); customerRepository.save(n); + System.out.println("team found : " + team); + Team temp = teamRepository.findByTeam(team); + temp.addCustomer(n); + teamRepository.save(temp); + return "redirect:/registration?message=L'utilisateur&succeed=1"; } - modelAndView.setViewName("registration"); - return modelAndView; + } @RequestMapping(value = "/file", method = RequestMethod.POST) diff --git a/PFE06/src/main/java/com/PFE/ServerManager/TeamRepository.java b/PFE06/src/main/java/com/PFE/ServerManager/TeamRepository.java index f035ac3..f68043d 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/TeamRepository.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/TeamRepository.java @@ -9,6 +9,8 @@ import java.util.List; @Repository public interface TeamRepository extends JpaRepository { + Team findByTeam(String team); + Team findByCustomersContaining(Customer customer); @Override diff --git a/PFE06/src/main/resources/templates/login.html b/PFE06/src/main/resources/templates/login.html index 325acad..92467d2 100644 --- a/PFE06/src/main/resources/templates/login.html +++ b/PFE06/src/main/resources/templates/login.html @@ -29,11 +29,11 @@
- +

- +

diff --git a/PFE06/src/main/resources/templates/registration.html b/PFE06/src/main/resources/templates/registration.html index 881235f..9667fe3 100644 --- a/PFE06/src/main/resources/templates/registration.html +++ b/PFE06/src/main/resources/templates/registration.html @@ -38,32 +38,19 @@ +
+

Formulaire d'ajout d'un utilisateur

+

Merci d'entrer le login et le mot de passe du nouvel utilisateur

+
+
+
+ +
+ +
+ +
+
+
+

Formulaire d'ajout d'un groupe de travail

+

Merci d'entrer le nom du nouveau groupe

+
+ + -- libgit2 0.21.2