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