Advertisement
Bear13th

MainController

Apr 2nd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package ru.example.testnew.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import ru.example.testnew.domain.Message;
  9. import ru.example.testnew.repos.MessageRepo;
  10. import java.util.Map;
  11.  
  12. @Controller
  13. public class MainController {
  14.     @Autowired
  15.     private MessageRepo messageRepo;
  16.  
  17.     @GetMapping("/")
  18.     public String greeting(Map<String, Object> model) {
  19.         return "greeting";
  20.     }
  21.  
  22.     @GetMapping("/main")
  23.     public String main(Map<String, Object> model) {
  24.         Iterable<Message> messages = messageRepo.findAll();
  25.         model.put("messages", messages);
  26.         return "main";
  27.     }
  28.  
  29.     @PostMapping("/main")
  30.     public String add(@RequestParam String text, @RequestParam String tag, Map<String, Object> model) {
  31.         Message message = new Message(text, tag);
  32.         messageRepo.save(message);
  33.  
  34.         Iterable<Message> messages = messageRepo.findAll();
  35.         model.put("messages", messages);
  36.  
  37.         return "main";
  38.     }
  39.  
  40.     @PostMapping("filter")
  41.     public String filter(@RequestParam String filter, Map<String, Object> model) {
  42.         Iterable<Message> messages;
  43.  
  44.         if (filter != null && !filter.isEmpty()) {
  45.             messages = messageRepo.findByTag(filter);
  46.         } else {
  47.             messages = messageRepo.findAll();
  48.         }
  49.  
  50.         model.put("messages", messages);
  51.  
  52.         return "main";
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement