MainController.java
3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.PFE.ServerManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MainController {
@Autowired
CustomerRepository customerRepository;
@Autowired
RoleRepository roleRepository;
@RequestMapping(value="/")
public String home(){
return "home";
}
@GetMapping(path="/registration")
public String registration() {
return "registration";//fait le lien automatiquement avec le page html du même nom //return "redirect:/....";
}
@PostMapping(path="/registration")
public ModelAndView addNewUser(@RequestParam String pseudo, @RequestParam String password) {
//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.setPseudo(pseudo);
n.setPassword(password);
Customer temp = customerRepository.findByPseudo(pseudo);
/*Role nRole = roleRepository.findByRole("ADMIN");
n.setRoles(new HashSet<Role>(Arrays.asList(nRole)));*/
Role role = new Role(); // l'utilisation d'un role au lieu d'un tableau semble valide, ormis la première ligne de la table qui n'est pas utilisé
role.setRole("ADMIN");
n.setRole(role);
if(temp != null) {
modelAndView.addObject("ok", "l'utilisateur existe déjà");
//return "login?fail";
}
else {
modelAndView.addObject("ok", "l'utilisateur a bien été ajouté");
customerRepository.save(n);
}
modelAndView.setViewName("registration");
return modelAndView;
}
@GetMapping(path="/login")
public ModelAndView login(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
//////// Ne fonctionne pas /////////
/*public String login() {
return "login"; //return "redirect:/...."; //to send a request to redirect the current page
}*/
/*
@PostMapping(path="/login")
public ModelAndView connexion(@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, this is the same way for ModelAndView
ModelAndView modelAndView = new ModelAndView();
Customer temp = customerRepository.findByPseudo(pseudo);
if(temp != null) {
modelAndView.addObject("error", "vous etes autorisé à être sur cette page");
}
else{
modelAndView.addObject("error", "vous n'etes pas autorisé à être sur cette page");
}
modelAndView.setViewName("login");
return modelAndView;
}*/
//////////////////////////
@GetMapping(path="/all")
public @ResponseBody Iterable<Customer> getAllUsers() {
return customerRepository.findAll();
}
@RequestMapping(value="/success")
public String success(){
return "success";
}
}