Advertisement
Guest User

task controller

a guest
Apr 8th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package meisterTask.controllers;
  2.  
  3. import meisterTask.bindingModel.TaskBindingModel;
  4. import meisterTask.entities.Task;
  5. import meisterTask.repositories.TaskRepository;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.servlet.ModelAndView;
  12.  
  13. import java.util.List;
  14. import java.util.stream.Collectors;
  15.  
  16. @Controller
  17. public class TaskController {
  18.     private final TaskRepository taskRepository;
  19.  
  20.     @Autowired
  21.     public TaskController(TaskRepository taskRepository) {
  22.         this.taskRepository = taskRepository;
  23.     }
  24.  
  25.     @GetMapping("/")
  26.     public ModelAndView index(ModelAndView modelAndView) {
  27.         List<Task> openTasks = this.taskRepository.findAll().stream().
  28.                 filter(e -> e.getStatus().equals("Open")).collect(Collectors.toList());
  29.  
  30.         List<Task> inProgressTasks = this.taskRepository.findAll().stream().
  31.                 filter(e -> e.getStatus().equals("In progress")).collect(Collectors.toList());
  32.  
  33.         List<Task> finishedTasks = this.taskRepository.findAll().stream().
  34.                 filter(e -> e.getStatus().equals("Finished")).collect(Collectors.toList());
  35.  
  36.         modelAndView.setViewName("base-layout");
  37.         modelAndView.addObject("view", "task/index");
  38.  
  39.         modelAndView.addObject("openTasks", openTasks);
  40.         modelAndView.addObject("inProgressTasks", inProgressTasks);
  41.         modelAndView.addObject("finishedTasks", finishedTasks);
  42.  
  43.         return modelAndView;
  44.     }
  45.  
  46.     @GetMapping("/create")
  47.     public ModelAndView create(ModelAndView modelAndView) {
  48.         modelAndView.setViewName("base-layout");
  49.         modelAndView.addObject("view", "task/create");
  50.         return modelAndView;
  51.     }
  52.  
  53.     @PostMapping("/create")
  54.     public String create(Task task) {
  55.         this.taskRepository.saveAndFlush(task);
  56.         return "redirect:/";
  57.     }
  58.  
  59.     @GetMapping("/edit/{id}")
  60.     public ModelAndView edit(ModelAndView modelAndView, @PathVariable(value = "id") Integer id) {
  61.  
  62.         Task task = this.taskRepository.findById(id).get();
  63.  
  64.         modelAndView.setViewName("base-layout");
  65.         modelAndView.addObject("view", "task/edit");
  66.         modelAndView.addObject("task", task);
  67.         return modelAndView;
  68.     }
  69.  
  70.     @PostMapping("/edit/{id}")
  71.     public String edit(TaskBindingModel taskBindingModel, @PathVariable(value = "id") Integer id) {
  72.  
  73.         Task taskToEdit = this.taskRepository.findById(id).get();
  74.  
  75.         taskToEdit.setTitle(taskBindingModel.getTitle());
  76.         taskToEdit.setStatus(taskBindingModel.getStatus());
  77.  
  78.         this.taskRepository.saveAndFlush(taskToEdit);
  79.         return "redirect:/";
  80.     }
  81.  
  82.     @GetMapping("/delete/{id}")
  83.     public ModelAndView delete(ModelAndView modelAndView, @PathVariable(value = "id") Integer id) {
  84.  
  85.         Task task = this.taskRepository.findById(id).get();
  86.  
  87.         modelAndView.setViewName("base-layout");
  88.         modelAndView.addObject("view", "task/delete");
  89.         modelAndView.addObject("task", task);
  90.  
  91.         return modelAndView;
  92.     }
  93.  
  94.     @PostMapping("/delete/{id}")
  95.     public String delete(@PathVariable(value = "id") Integer id) {
  96.         this.taskRepository.deleteById(id);
  97.  
  98.         return "redirect:/";
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement