Advertisement
damesova

Controller BandRegister [Mimi]

Apr 10th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. package softuni.bandregister.controllers;
  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.PathVariable;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import softuni.bandregister.bindingModels.BandBindingModel;
  10. import softuni.bandregister.entities.Band;
  11. import softuni.bandregister.repositories.BandRepository;
  12.  
  13. import java.util.List;
  14.  
  15. @Controller
  16. public class BandController {
  17.     private final BandRepository bandRepository;
  18.  
  19.     @Autowired
  20.     public BandController(BandRepository bandRepository) {
  21.         this.bandRepository = bandRepository;
  22.     }
  23.  
  24.  
  25.     @GetMapping("/")
  26.     public ModelAndView index(ModelAndView modelAndView) {
  27.         List<Band> bands = this.bandRepository.findAll();
  28.         modelAndView.setViewName("base-layout");    
  29.         modelAndView.addObject("view", "band/index");
  30.         modelAndView.addObject("bands", bands);
  31.  
  32.         return modelAndView;
  33.     }
  34.  
  35.     @GetMapping("/create")
  36.     public ModelAndView create(ModelAndView modelAndView) {
  37.         modelAndView.setViewName("base-layout");
  38.         modelAndView.addObject("view", "band/create");
  39.  
  40.         return modelAndView;
  41.     }
  42.  
  43.     @PostMapping("/create")
  44.     public String create(Band band) {
  45.         this.bandRepository.saveAndFlush(band);
  46.  
  47.         return ("redirect:/");
  48.     }
  49.  
  50.     @GetMapping("/edit/{id}")
  51.     public ModelAndView edit(ModelAndView modelAndView,
  52.                              @PathVariable(value = "id") Integer id) {
  53.         Band band = this.bandRepository.findById(id).get();
  54.  
  55.         modelAndView.setViewName("base-layout");
  56.         modelAndView.addObject("view", "band/edit");
  57.         modelAndView.addObject("band", band);
  58.  
  59.         return modelAndView;
  60.     }
  61.  
  62.     @PostMapping("/edit/{id}")
  63.     public String edit(@PathVariable(value = "id") Integer id, BandBindingModel bandBindingModel) {
  64.         Band bandEdit = this.bandRepository.findById(id).get();
  65.         bandEdit.setName(bandBindingModel.getName());
  66.         bandEdit.setMembers(bandBindingModel.getMembers());
  67.         bandEdit.setHonorarium(bandBindingModel.getHonorarium());
  68.         bandEdit.setGenre(bandBindingModel.getGenre());
  69.  
  70.         this.bandRepository.saveAndFlush(bandEdit);
  71.  
  72.         return "redirect:/";
  73.     }
  74.  
  75.     @GetMapping("/delete/{id}")
  76.     public ModelAndView delete(ModelAndView modelAndView,
  77.                              @PathVariable(value = "id") Integer id) {
  78.         Band band = this.bandRepository.findById(id).get();
  79.  
  80.         modelAndView.setViewName("base-layout");
  81.         modelAndView.addObject("view", "band/delete");
  82.         modelAndView.addObject("band", band);
  83.  
  84.         return modelAndView;
  85.     }
  86.  
  87.     @PostMapping("/delete/{id}")
  88.     public String delete(Band band) {
  89.         this.bandRepository.delete(band);
  90.         return "redirect:/";
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement