Skip to content

Commit 9da4eb7

Browse files
authored
Merge pull request #4 from AlbertProfe/master
Actu
2 parents 1725aef + 5c8a6e4 commit 9da4eb7

File tree

11 files changed

+245
-24
lines changed

11 files changed

+245
-24
lines changed

library4/src/main/java/io/company/library/model/Book.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@Getter @Setter @NoArgsConstructor @AllArgsConstructor
1111
@ToString
1212
//book class is NOT an entity, it is a PÒJO
13-
public class Book {
13+
public abstract class Book {
1414

1515
@Id
1616
@GeneratedValue(strategy = GenerationType.AUTO)

library5/src/main/java/io/company/library/Book.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public Book(String title, int pages, int publishedYear, String isbn) {
3232
this.isbn = isbn;
3333
}
3434

35+
public Book(String title) {
36+
this.title = title;
37+
}
38+
3539
@Override
3640
public String toString() {
3741
return "Book{" +

library5/src/main/java/io/company/library/BookService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public Book updateBook (Book book){
4848
return bookRepository.save(book);
4949
}
5050

51+
public void deleteBook(Book book) {
52+
53+
bookRepository.delete(book);
54+
}
55+
5156

5257

5358

library5/src/main/java/io/company/library/LibraryWebController.java

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.PostMapping;
68
import org.springframework.web.bind.annotation.RequestMapping;
79
import org.springframework.web.bind.annotation.RequestParam;
10+
import java.util.Optional;
811

912
@Controller
1013
@RequestMapping("/library")
@@ -14,33 +17,77 @@ public class LibraryWebController {
1417
BookService bookService;
1518

1619
@RequestMapping("/books")
17-
public String getWeb (Model containerToView) {
18-
20+
public String getWeb(Model containerToView) {
21+
//
1922
containerToView.addAttribute("booksfromController",
2023
bookService.getAllBooks());
21-
24+
containerToView.addAttribute("aeatName", "https://sede.agenciatributaria.gob.es/");
25+
containerToView.addAttribute("aeat", -150);
26+
containerToView.addAttribute("changeInLove", false);
2227
return "showBooks";
2328
}
24-
@RequestMapping("/newBook")
25-
public String newBook (){
2629

30+
@RequestMapping("/newBook")
31+
public String newBook() {
2732
return "newBook";
2833
}
2934

35+
@RequestMapping("/insertBook")
36+
public String insertBook(Book book) {
37+
//
38+
bookService.createBook(book);
39+
return "redirect:books";
40+
}
3041

3142
@RequestMapping("/deleteBook")
32-
public String deleteBook(@RequestParam String titleFromView){
43+
public String deleteBook(@RequestParam("bookIdFromView") Long id) {
44+
//
45+
bookService.deleteBookById(id);
46+
return "redirect:books";
47+
}
3348

34-
System.out.println("titleFromView" + titleFromView);
35-
bookService.deleteBookByTitle(titleFromView);
49+
@PostMapping("/replaceBook/{idFromView}")
50+
public String replaceBook(@PathVariable("idFromView") Long id, Book book){
51+
//
52+
Optional<Book> bookFound = bookService.findBookById(id);
53+
if (bookFound.isPresent()) {
54+
bookService.createBook(book);
55+
return "redirect:/library/books";
56+
} else return "notfound.html";
3657

37-
return "showBooks";
58+
}
3859

60+
@RequestMapping("/updateBook")
61+
public String updateBook(@RequestParam("bookIdFromView") Long id, Model model) {
62+
//examples of addAtrribute
63+
Optional<Book> bookFound = bookService.findBookById(id);
64+
if (bookFound.isPresent()) {
65+
model.addAttribute("bookfromController", bookFound.get());
66+
return "updateBook.html";
67+
} else return "notfound.html";
68+
}
69+
70+
@RequestMapping("/javafaker")
71+
public String createFakeBooks( @RequestParam("qtyBooks") int qty ){
72+
73+
//method while to create books
74+
//bookservice.save(book)
75+
76+
System.out.println("we are going to create 100 books");
77+
78+
return "redirect:books";
79+
80+
}
3981

82+
@RequestMapping("/detailBook")
83+
public String detailBook(@RequestParam("bookIdFromView") Long id) {
84+
//
85+
//bookService.updateBookById(id);
86+
return "detailBook.html";
4087
}
4188

89+
}
4290

4391

4492

4593

46-
}
10.7 KB
Loading
21.4 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
load all the date from book
9+
</body>
10+
</html>
Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,64 @@
11
<!DOCTYPE html>
2-
<html lang="en">
2+
<html xmlns:th="http://www.thymeleaf.org">
33
<head>
4-
<meta charset="UTF-8">
5-
<title>Title</title>
4+
<meta name="viewport" content="width=device-width, initial-scale=1">
5+
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
66
</head>
77
<body>
8-
new book
8+
9+
<!--https://www.quanzhanketang.com/w3css/tryw3css_input_top.html?filename=tryw3css_input_top
10+
https://www.w3schools.com/w3css/w3css_padding.asp
11+
https://www.w3schools.com/w3css/w3css_color_material.asp
12+
https://www.w3schools.com/w3css/w3css_display.asp
13+
https://www.flaticon.com/
14+
https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_cards_buttons2 -->
15+
16+
<div class="w3-container">
17+
18+
<div class="w3-container w3-sand">
19+
<h1>Spring Boot Books Example .....</h1>
20+
<h5>form to create new book sending to @Controller @Service @Repository and H2 DB</h5>
21+
</div>
22+
<Br>
23+
24+
<div class="w3-card-4" style="width:50%;">
25+
<header class="w3-container w3-light-grey">
26+
<img src="/images/open-book.png" alt="open-book" class="w3-right w3-padding-16 w3-margin-right" style="width:40px">
27+
<p>Add new book</p>
28+
</header>
29+
30+
<div class="w3-container">
31+
<form action="insertBook" >
32+
<p>
33+
<label for="title">Title</label>
34+
<input type="text" name="title" class="w3-input" id="title"></p>
35+
<p>
36+
<label for="isbn">ISBN</label>
37+
<input type="text" name="isbn" class="w3-input" id="isbn"></p>
38+
<p>
39+
<label for="pages">Pages</label>
40+
<input type="number" name="pages" class="w3-input" id="pages" ></p>
41+
<p>
42+
<label for="author">Author</label>
43+
<input type="text" name="author" class="w3-input" id="author" ></p>
44+
<p>
45+
<label for="publishedYear">Published Year</label>
46+
<input type="number" name="publishedYear" class="w3-input" id="publishedYear" ></p>
47+
<div class="w3-container w3-padding-16">
48+
<input type="submit" value="Add new book" class="w3-button w3-padding-16 w3-light-grey w3-right w3-margin-right"/>
49+
</div>
50+
</form>
51+
</div>
52+
53+
<footer class="w3-container w3-dark-grey">
54+
<p>Library</p>
55+
</footer>
56+
</div>
57+
58+
<div >
59+
<p><a th:href="@{/library/books}" class="w3-button w3-black">Back books</a></p>
60+
</div>
61+
62+
</div>
963
</body>
1064
</html>

library5/src/main/resources/templates/bookdeleted.html renamed to library5/src/main/resources/templates/notfound.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<title>Title</title>
66
</head>
77
<body>
8-
book deleted
8+
book not found
99
</body>
1010
</html>
Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
<!DOCTYPE html>
22
<html xmlns:th="http://www.thymeleaf.org">
3-
43
<meta name="viewport" content="width=device-width, initial-scale=1">
54
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
6-
75
<body>
8-
<div class="w3-panel w3-padding-32">
6+
7+
<div class="w3-container">
8+
9+
<div class="w3-container w3-sand">
910
<h1>Spring Boot Books Example .....</h1>
10-
<h3>table of books from @Controller @Service @Repository and H2 DB</h3>
11+
<h5>table of books from @Controller @Service @Repository and H2 DB</h5>
12+
</div>
13+
<Br>
14+
15+
<p >Aeat Link</p>
16+
<p th:text ="${aeatName}">Aeat Link</p>
17+
<p >Aeat</p>
18+
<p th:text ="${aeat}">Aeat</p>
19+
<p >Never</p>
20+
<p th:text ="${changeInLove}">Never</p>
21+
<div >
22+
<p><a th:href="@{/library/javafaker(qtyBooks=100)}" class="w3-button w3-cyan">Create fake books on H2 -100-</a></p>
23+
</div>
24+
25+
<div >
26+
<p><a th:href="@{/library/javafaker}" class="w3-button w3-blue">Create fake books on H2 -500-</a></p>
27+
</div>
1128

1229
<table class="w3-table-all w3-hoverable" >
30+
<tr>
31+
<td>BookId</td>
32+
<td>Title</td>
33+
<td>ISBN</td>
34+
<td>Pages</td>
35+
<td>Published Year</td>
36+
<td>Author</td>
37+
<td>Delete</td>
38+
<td>Update</td>
39+
<td>Detail</td>
40+
</tr>
1341
<tr th:each="book:${booksfromController}">
1442

1543
<td th:text="${book.bookId}"></td>
@@ -19,11 +47,14 @@ <h3>table of books from @Controller @Service @Repository and H2 DB</h3>
1947
<td th:text="${book.publishedYear}"></td>
2048
<td th:text="${book.author}"></td>
2149

22-
<td> <a th:href ="@{deleteBook(titleFromView=${book.title})}"
23-
class="w3-button w3-red"> Delete this book </a> </td>
50+
<td> <a th:href ="@{deleteBook(bookIdFromView=${book.bookId})}"
51+
class="w3-button w3-red">Delete this book</a> </td>
52+
53+
<td> <a th:href ="@{updateBook(bookIdFromView=${book.bookId})}"
54+
class="w3-button w3-cyan">Update this book</a> </td>
2455

25-
<td> <a th:href ="@{updateBook(titleFromView=${book.title})}"
26-
class="w3-button w3-cyan"> Update this book </a> </td>
56+
<td> <a th:href ="@{detailBook(bookIdFromView=${book.bookId})}"
57+
class="w3-button w3-black">Detail book</a> </td>
2758

2859
</tr>
2960
</table>
@@ -32,6 +63,8 @@ <h3>table of books from @Controller @Service @Repository and H2 DB</h3>
3263
<a th:href="@{/library/newBook}" class="w3-button w3-blue">Add new book</a>
3364
</p>
3465

66+
3567
</div>
68+
3669
</body>
3770
</html>

0 commit comments

Comments
 (0)