source: src/main/java/com/example/autopartz/controller/HomeController.java@ 60de3eb

main
Last change on this file since 60de3eb was 60de3eb, checked in by andrejtodorovski <82031894+andrejtodorovski@…>, 18 months ago

Added master template and added filtering parts by car and category

  • Property mode set to 100644
File size: 3.1 KB
Line 
1package com.example.autopartz.controller;
2
3import com.example.autopartz.model.User;
4import com.example.autopartz.repository.PartsForCarTypeAndCategoryRepository;
5import com.example.autopartz.service.CarService;
6import com.example.autopartz.service.CategoryService;
7import com.example.autopartz.service.LoginService;
8import com.example.autopartz.service.PartService;
9import org.springframework.stereotype.Controller;
10import org.springframework.ui.Model;
11import org.springframework.web.bind.annotation.GetMapping;
12import org.springframework.web.bind.annotation.PostMapping;
13import org.springframework.web.bind.annotation.RequestMapping;
14import org.springframework.web.bind.annotation.RequestParam;
15
16@Controller
17@RequestMapping("/")
18public class HomeController {
19 private final LoginService loginService;
20 private final PartService partService;
21 private final PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository;
22 private final CarService carService;
23 private final CategoryService categoryService;
24
25 public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService) {
26 this.loginService = loginService;
27 this.partService = partService;
28 this.partsForCarTypeAndCategoryRepository = partsForCarTypeAndCategoryRepository;
29 this.carService = carService;
30 this.categoryService = categoryService;
31 }
32
33 @GetMapping()
34 public String getHomePage(Model model){
35 model.addAttribute("parts",partService.findAll());
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";
46 }
47 @GetMapping("/login")
48 public String getLoginPage(Model model){
49 model.addAttribute("bodyContent","login");
50 return "master-template";
51 }
52 @GetMapping("/register")
53 public String getRegisterPage(Model model){
54 model.addAttribute("bodyContent","register");
55 return "master-template";
56 }
57 @PostMapping("/login")
58 public void handleLogin(@RequestParam String username, @RequestParam String password){
59 User u = loginService.login(username,password);
60 System.out.println(u.getName_user());
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}
Note: See TracBrowser for help on using the repository browser.