[feffc2f] | 1 | package com.example.autopartz.controller;
|
---|
| 2 |
|
---|
| 3 | import com.example.autopartz.model.User;
|
---|
[60de3eb] | 4 | import com.example.autopartz.repository.PartsForCarTypeAndCategoryRepository;
|
---|
[6832924] | 5 | import com.example.autopartz.repository.RepairShopReviewSummaryRepository;
|
---|
[60de3eb] | 6 | import com.example.autopartz.service.CarService;
|
---|
| 7 | import com.example.autopartz.service.CategoryService;
|
---|
[feffc2f] | 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 |
|
---|
[ae042f4] | 17 | import javax.servlet.http.HttpServletRequest;
|
---|
| 18 |
|
---|
[feffc2f] | 19 | @Controller
|
---|
| 20 | @RequestMapping("/")
|
---|
| 21 | public class HomeController {
|
---|
| 22 | private final LoginService loginService;
|
---|
| 23 | private final PartService partService;
|
---|
[60de3eb] | 24 | private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
|
---|
| 25 | private final CarService carService;
|
---|
| 26 | private final CategoryService categoryService;
|
---|
[6832924] | 27 | private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository;
|
---|
[feffc2f] | 28 |
|
---|
[6832924] | 29 | public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository) {
|
---|
[feffc2f] | 30 | this.loginService = loginService;
|
---|
| 31 | this.partService = partService;
|
---|
[60de3eb] | 32 | this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
|
---|
| 33 | this.carService = carService;
|
---|
| 34 | this.categoryService = categoryService;
|
---|
[6832924] | 35 | this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository;
|
---|
[feffc2f] | 36 | }
|
---|
| 37 |
|
---|
| 38 | @GetMapping()
|
---|
[ae042f4] | 39 | public String getHomePage(Model model, HttpServletRequest request){
|
---|
[6832924] | 40 | model.addAttribute("bodyContent","home");
|
---|
[ae042f4] | 41 | model.addAttribute("user",request.getRemoteUser());
|
---|
[6832924] | 42 | return "master-template";
|
---|
| 43 | }
|
---|
| 44 | @GetMapping("/products")
|
---|
| 45 | public String getProducts(Model model){
|
---|
[feffc2f] | 46 | model.addAttribute("parts",partService.findAll());
|
---|
[60de3eb] | 47 | model.addAttribute("cars",carService.findAll());
|
---|
| 48 | model.addAttribute("categories",categoryService.findAll());
|
---|
[6832924] | 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");
|
---|
[60de3eb] | 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";
|
---|
[feffc2f] | 63 | }
|
---|
| 64 | @GetMapping("/login")
|
---|
[60de3eb] | 65 | public String getLoginPage(Model model){
|
---|
| 66 | model.addAttribute("bodyContent","login");
|
---|
| 67 | return "master-template";
|
---|
[feffc2f] | 68 | }
|
---|
| 69 | @GetMapping("/register")
|
---|
[60de3eb] | 70 | public String getRegisterPage(Model model){
|
---|
| 71 | model.addAttribute("bodyContent","register");
|
---|
| 72 | return "master-template";
|
---|
[feffc2f] | 73 | }
|
---|
| 74 | @PostMapping("/login")
|
---|
| 75 | public void handleLogin(@RequestParam String username, @RequestParam String password){
|
---|
| 76 | User u = loginService.login(username,password);
|
---|
[60de3eb] | 77 | System.out.println(u.getName_user());
|
---|
[feffc2f] | 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 | }
|
---|