[feffc2f] | 1 | package com.example.autopartz.controller;
|
---|
| 2 |
|
---|
| 3 | import com.example.autopartz.model.User;
|
---|
[60de3eb] | 4 | import com.example.autopartz.repository.PartsForCarTypeAndCategoryRepository;
|
---|
| 5 | import com.example.autopartz.service.CarService;
|
---|
| 6 | import com.example.autopartz.service.CategoryService;
|
---|
[feffc2f] | 7 | import com.example.autopartz.service.LoginService;
|
---|
| 8 | import com.example.autopartz.service.PartService;
|
---|
| 9 | import org.springframework.stereotype.Controller;
|
---|
| 10 | import org.springframework.ui.Model;
|
---|
| 11 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
| 12 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
| 13 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
| 14 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 15 |
|
---|
| 16 | @Controller
|
---|
| 17 | @RequestMapping("/")
|
---|
| 18 | public class HomeController {
|
---|
| 19 | private final LoginService loginService;
|
---|
| 20 | private final PartService partService;
|
---|
[60de3eb] | 21 | private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
|
---|
| 22 | private final CarService carService;
|
---|
| 23 | private final CategoryService categoryService;
|
---|
[feffc2f] | 24 |
|
---|
[60de3eb] | 25 | public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService) {
|
---|
[feffc2f] | 26 | this.loginService = loginService;
|
---|
| 27 | this.partService = partService;
|
---|
[60de3eb] | 28 | this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
|
---|
| 29 | this.carService = carService;
|
---|
| 30 | this.categoryService = categoryService;
|
---|
[feffc2f] | 31 | }
|
---|
| 32 |
|
---|
| 33 | @GetMapping()
|
---|
| 34 | public String getHomePage(Model model){
|
---|
| 35 | model.addAttribute("parts",partService.findAll());
|
---|
[60de3eb] | 36 | model.addAttribute("cars",carService.findAll());
|
---|
| 37 | model.addAttribute("categories",categoryService.findAll());
|
---|
| 38 | model.addAttribute("bodyContent","homepage");
|
---|
| 39 | return "master-template";
|
---|
| 40 | }
|
---|
| 41 | @GetMapping("/filtered")
|
---|
| 42 | public String getPartsForCarTypeAndCategory(@RequestParam String cartype, @RequestParam String category, Model model){
|
---|
| 43 | model.addAttribute("filtered", partsForCarTypeAndCategoryRepository.findAllByCartypeAndCategory(cartype,category));
|
---|
| 44 | model.addAttribute("bodyContent","filteredParts");
|
---|
| 45 | return "master-template";
|
---|
[feffc2f] | 46 | }
|
---|
| 47 | @GetMapping("/login")
|
---|
[60de3eb] | 48 | public String getLoginPage(Model model){
|
---|
| 49 | model.addAttribute("bodyContent","login");
|
---|
| 50 | return "master-template";
|
---|
[feffc2f] | 51 | }
|
---|
| 52 | @GetMapping("/register")
|
---|
[60de3eb] | 53 | public String getRegisterPage(Model model){
|
---|
| 54 | model.addAttribute("bodyContent","register");
|
---|
| 55 | return "master-template";
|
---|
[feffc2f] | 56 | }
|
---|
| 57 | @PostMapping("/login")
|
---|
| 58 | public void handleLogin(@RequestParam String username, @RequestParam String password){
|
---|
| 59 | User u = loginService.login(username,password);
|
---|
[60de3eb] | 60 | System.out.println(u.getName_user());
|
---|
[feffc2f] | 61 | }
|
---|
| 62 | @PostMapping("/register")
|
---|
| 63 | public void handleRegister(@RequestParam String username, @RequestParam String name,
|
---|
| 64 | @RequestParam String password, @RequestParam String email,
|
---|
| 65 | @RequestParam String number){
|
---|
| 66 | User u = loginService.register(name,username,email,number,password);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|