-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexController.java
More file actions
105 lines (90 loc) · 3.14 KB
/
IndexController.java
File metadata and controls
105 lines (90 loc) · 3.14 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package edu.psu.matchwarketplace.controller;
import edu.psu.matchwarketplace.model.Watch;
import edu.psu.matchwarketplace.service.WatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class IndexController {
/*
spring managed service
*/
@Autowired
private WatchService watchService;
//
// instance data
// private final WatchService watchService;
//
// public IndexController() {
// this.watchService = new WatchServiceImpl(); // developer managed
//
// }
@GetMapping(value = "/")
public String index(Model model) {
List<Watch> watches = watchService.getWatches();
model.addAttribute("watches", watches);
return "index";
}
@PostMapping("/search")
public String indexFiltered(Model model, @RequestParam String searchParam) {
List<Watch> watches = watchService.getFilteredWatches(searchParam);
model.addAttribute("watches", watches);
return "index";
}
/*
add watch
*/
@GetMapping("/add-watch")
public String addWatchPage(Model model) {
return "addWatch";
}
@PostMapping("/add-watch")
public String submitAddWatch(Model model, @RequestParam String brand, @RequestParam String watchModel, @RequestParam String cost) {
watchService.addWatch(brand, watchModel, cost);
List<Watch> watches = watchService.getWatches();
model.addAttribute("watches", watches);
return "index";
}
/*
delete watch
*/
@GetMapping("/delete-watch/{watchId}")
public String deleteWatch(Model model, @PathVariable Long watchId) {
watchService.deleteWatch(watchId);
List<Watch> watches = watchService.getWatches();
model.addAttribute("watches", watches);
return "index";
}
/*
edit watch
*/
@GetMapping("/edit-watch")
public String editWatchPage(Model model) {
return "editWatch";
}
@GetMapping("/edit-watch/{watchId}")
public String editWatch(Model model, @PathVariable Long watchId) {
//
// get the watch from the db
Watch watch = watchService.getWatchById(watchId);
model.addAttribute("watch", watch);
return "editWatch";
}
@PostMapping("/edit-watch")
public String submitEditWatch(Model model
, @RequestParam String brand
, @RequestParam String watchModel
, @RequestParam String cost
, @RequestParam Long watchId
) {
watchService.editWatch(brand, watchModel, cost, watchId);
List<Watch> watches = watchService.getWatches();
model.addAttribute("watches", watches);
return "index";
}
}