package com example sweater Controller import com example sweater doma

 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
package com.example.sweater.Controller;
import com.example.sweater.domain.Message;
import com.example.sweater.repos.MessageRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
@Controller
public class MainController {
@Autowired
private MessageRepo messageRepo;
@GetMapping("/main")
public String main(Map<String, Object> model) {
Iterable<Message> messages = messageRepo.findAll();
model.put("messages", messages);
return "main";
}
@GetMapping("/")
public String greeting(Map<String, Object> model) {
return "greeting";
}
@PostMapping("/main")
public String add(@RequestParam String text, @RequestParam String tag, Map<String, Object> model) {
Message message = new Message(text, tag);
messageRepo.save(message);
Iterable<Message> messages = messageRepo.findAll();
model.put("messages", messages);
return "main";
}
@PostMapping("filter")
public String filter(@RequestParam(value = "filter", required = false) String filter, Map<String, Object> model) {
Iterable<Message> messages;
if (filter != null && !filter.isEmpty()) {
messages = messageRepo.findByTag(filter);
} else {
messages = messageRepo.findAll();
}
model.put("messages", messages);
return "main";
}
}