Advertisement
damesova

Controller Book Library [Mimi]

Apr 6th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. package library.controllers;
  2.  
  3. import library.bindingModels.BookBindingModel;
  4. import library.entities.Book;
  5. import library.repositories.BookRepository;
  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.  
  15. @Controller
  16. public class BookController {
  17.     private final BookRepository bookRepository;
  18.  
  19.     @Autowired
  20.     public BookController(BookRepository bookRepository) {
  21.         this.bookRepository = bookRepository;
  22.     }
  23.  
  24.  
  25.     @GetMapping("/")
  26.     public ModelAndView index(ModelAndView modelAndView) {
  27.         List<Book> books = this.bookRepository.findAll();
  28.         modelAndView.setViewName("base-layout");    
  29.         modelAndView.addObject("view", "book/index");
  30.         modelAndView.addObject("books", books);
  31.  
  32.         return modelAndView;
  33.     }
  34.  
  35.     @GetMapping("/create")
  36.     public ModelAndView create(ModelAndView modelAndView) {
  37.         modelAndView.setViewName("base-layout");
  38.         modelAndView.addObject("view", "book/create");
  39.  
  40.         return modelAndView;
  41.     }
  42.  
  43.     @PostMapping("/create")
  44.     public String create(Book book) {
  45.         this.bookRepository.saveAndFlush(book);
  46.  
  47.         return ("redirect:/");
  48.     }
  49.  
  50.     @GetMapping("/edit/{id}")
  51.     public ModelAndView edit(@PathVariable(value = "id") Integer id, ModelAndView modelAndView) {
  52.         Book book = this.bookRepository.findById(id).get();
  53.  
  54.         modelAndView.setViewName("base-layout");
  55.         modelAndView.addObject("view", "book/edit");
  56.         modelAndView.addObject("book", book);
  57.  
  58.         return modelAndView;
  59.     }
  60.  
  61.     @PostMapping("edit/{id}")
  62.     public String edit(@PathVariable(value = "id") Integer id, BookBindingModel bookBindingModel){
  63.         Book bookEdit = this.bookRepository.findById(id).get();
  64.         bookEdit.setTitle(bookBindingModel.getTitle());
  65.         bookEdit.setAuthor(bookBindingModel.getAuthor());
  66.         bookEdit.setPrice(bookBindingModel.getPrice());
  67.  
  68.         this.bookRepository.saveAndFlush(bookEdit);
  69.  
  70.         return "redirect:/";
  71.     }
  72.  
  73.     @GetMapping("/delete/{id}")
  74.     public ModelAndView remove(@PathVariable(value = "id")Integer id, ModelAndView modelAndView){
  75.         Book book = this.bookRepository.findById(id).get();
  76.  
  77.         modelAndView.setViewName("base-layout");
  78.         modelAndView.addObject("view", "book/remove");
  79.         modelAndView.addObject("book", book);
  80.  
  81.         return modelAndView;
  82.     }
  83.  
  84.     @PostMapping("/delete/{id}")
  85.     public String remove(Book book){
  86.         this.bookRepository.delete(book);
  87.  
  88.         return "redirect:/";
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement