MainController.java
1.63 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
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 java.util.Map;
@Controller // This means that this class is a Controller
public class MainController {
@Autowired // This means to get the bean called userRepository which is auto-generated by Spring, we will use it to handle the Customers
CustomerRepository customerRepository;
@RequestMapping(value="/")
public String login(){
return "login";
}
@GetMapping(path="/login") // Map ONLY GET Requests
public String addNewUser (@RequestParam String pseudo, @RequestParam String password, Map<String, Object> model) {
// @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);
customerRepository.save(n);
return "login"; //return "redirect:/...."; //to send a request to redirect the current page
}
@GetMapping(path="/all")
public @ResponseBody Iterable<Customer> getAllUsers() {
// This returns a JSON or XML with the users
return customerRepository.findAll();
}
}