Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. // BookBindingModel:
  2. package library.bindingModels;
  3.  
  4. public class BookBindingModel {
  5. private String title;
  6. private String author;
  7. private double price;
  8.  
  9. public String getTitle() {
  10. return title;
  11. }
  12.  
  13. public void setTitle(String title) {
  14. this.title = title;
  15. }
  16.  
  17. public String getAuthor() {
  18. return author;
  19. }
  20.  
  21. public void setAuthor(String author) {
  22. this.author = author;
  23. }
  24.  
  25. public Double getPrice() {
  26. return price;
  27. }
  28.  
  29. public void setPrice(Double price) {
  30. this.price = price;
  31. }
  32. }
  33. //BookController:
  34. package library.controllers;
  35.  
  36. import library.bindingModels.BookBindingModel;
  37. import library.entities.Book;
  38. import library.repositories.BookRepository;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.stereotype.Controller;
  41. import org.springframework.web.bind.annotation.GetMapping;
  42. import org.springframework.web.bind.annotation.PathVariable;
  43. import org.springframework.web.bind.annotation.PostMapping;
  44. import org.springframework.web.servlet.ModelAndView;
  45.  
  46. import java.util.List;
  47.  
  48. @Controller
  49. public class BookController {
  50. private final BookRepository bookRepository;
  51.  
  52. @Autowired
  53. public BookController(BookRepository bookRepository) {
  54. this.bookRepository = bookRepository;
  55. }
  56.  
  57.  
  58. @GetMapping("/")
  59. public ModelAndView index(ModelAndView modelAndView) {
  60. List<Book> books = this.bookRepository.findAll();
  61. modelAndView.setViewName("base-layout");
  62. modelAndView.addObject("view", "book/index");
  63. modelAndView.addObject("books", books);
  64. return modelAndView;
  65. }
  66.  
  67. @GetMapping("/create")
  68. public ModelAndView create(ModelAndView modelAndView) {
  69. modelAndView.setViewName("base-layout");
  70. modelAndView.addObject("view", "book/create");
  71. return modelAndView;
  72. }
  73.  
  74. @PostMapping("/create")
  75. public String create(Book book) {
  76. this.bookRepository.saveAndFlush(book);
  77. return "redirect:/";
  78. }
  79.  
  80. @GetMapping("/edit/{id}")
  81. public ModelAndView edit(@PathVariable(value = "id") Integer id, ModelAndView modelAndView) {
  82. Book book = this.bookRepository.findById(id).get();
  83. modelAndView.setViewName("base-layout");
  84. modelAndView.addObject("view", "book/edit");
  85. modelAndView.addObject("book", book);
  86. return modelAndView;
  87. }
  88.  
  89. @PostMapping("edit/{id}")
  90. public String edit(@PathVariable(value = "id") Integer id, BookBindingModel bookBindingModel){
  91. Book bookEdit = this.bookRepository.getOne(id);
  92. bookEdit.setTitle(bookBindingModel.getTitle());
  93. bookEdit.setAuthor(bookBindingModel.getAuthor());
  94. bookEdit.setPrice(bookBindingModel.getPrice());
  95.  
  96. this.bookRepository.saveAndFlush(bookEdit);
  97. return "redirect:/";
  98. }
  99.  
  100. @GetMapping("/delete/{id}")
  101. public ModelAndView remove(@PathVariable(value = "id")Integer id, ModelAndView modelAndView){
  102. Book book = this.bookRepository.findById(id).get();
  103. modelAndView.setViewName("base-layout");
  104. modelAndView.addObject("view", "book/remove");
  105. modelAndView.addObject("book", book);
  106. return modelAndView;
  107. }
  108.  
  109. @PostMapping("/delete/{id}")
  110. public String remove(Book book){
  111. this.bookRepository.delete(book);
  112. return "redirect:/";
  113. }
  114. }
  115. //class Book:
  116. package library.entities;
  117.  
  118. import javax.persistence.*;
  119. import javax.validation.constraints.Min;
  120.  
  121. @Entity
  122. @Table(name = "books")
  123. public class Book {
  124. private Integer id;
  125. private String title;
  126. private String author;
  127. private Double price;
  128.  
  129. public Book() {
  130. }
  131.  
  132. public Book(String title, String author, Double price) {
  133. this.title = title;
  134. this.author = author;
  135. this.price = price;
  136. }
  137.  
  138. @Id
  139. @GeneratedValue(strategy = GenerationType.IDENTITY)
  140. public Integer getId() {
  141. return id;
  142. }
  143.  
  144. public void setId(Integer id) {
  145. this.id = id;
  146. }
  147. @Column(name = "title", nullable = false)
  148. public String getTitle() {
  149. return title;
  150. }
  151.  
  152. public void setTitle(String title) {
  153. this.title = title;
  154. }
  155. @Column(name = "author", nullable = false)
  156. public String getAuthor() {
  157. return author;
  158. }
  159.  
  160. public void setAuthor(String author) {
  161. this.author = author;
  162. }
  163.  
  164. @Column(name = "price")
  165. @Min(value = 1)
  166. public Double getPrice() {
  167. return price;
  168. }
  169.  
  170. public void setPrice(Double price) {
  171. this.price = price;
  172. }
  173. }
  174. //interface BookRepository:
  175. package library.repositories;
  176.  
  177. import library.entities.Book;
  178. import org.springframework.data.jpa.repository.JpaRepository;
  179. import org.springframework.stereotype.Repository;
  180.  
  181. @Repository
  182. public interface BookRepository extends JpaRepository<Book,Integer> {
  183.  
  184. }
  185. //application properties:
  186. #Database Properties
  187. spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
  188. spring.datasource.url = jdbc:mysql://localhost:3306/book_library?useSSL=false&createDatabaseIfNotExist=true
  189. spring.datasource.username = root
  190. spring.datasource.password =
  191.  
  192. #JPA Properties
  193. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  194. spring.jpa.properties.hibernate.format_sql=TRUE
  195. spring.jpa.hibernate.ddl-auto=update
  196.  
  197. #Server Properties
  198. server.port=8080
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement