diff --git a/pom.xml b/pom.xml index dcbb6cb4..cbe5d93e 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-thymeleaf + com.h2database h2 @@ -37,6 +41,7 @@ spring-boot-starter-test test + diff --git a/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java new file mode 100644 index 00000000..143295f6 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/bootstrap/BootstrapData.java @@ -0,0 +1,72 @@ +package guru.springframework.spring6webapp.bootstrap; + +import guru.springframework.spring6webapp.domain.Author; +import guru.springframework.spring6webapp.domain.Book; +import guru.springframework.spring6webapp.domain.Publisher; +import guru.springframework.spring6webapp.repositories.AuthorRepository; +import guru.springframework.spring6webapp.repositories.BookRepository; +import guru.springframework.spring6webapp.repositories.PublisherRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class BootstrapData implements CommandLineRunner { + private final AuthorRepository authorRepository; + private final BookRepository bookRepository; + private final PublisherRepository publisherRepository; + + public BootstrapData(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; + } + + @Override + public void run(String... args) throws Exception { + Author eric = new Author(); + eric.setFirstName("Eric"); + eric.setLastName("Evans"); + + Book ddd = new Book(); + ddd.setTitle("Domain Driven Design"); + ddd.setIsbn("123456"); + + Author ericSaved = authorRepository.save(eric); + Book dddSaved = bookRepository.save(ddd); + + Author rod = new Author(); + rod.setFirstName("Rod"); + rod.setLastName("Johnson"); + + Book noEJB = new Book(); + noEJB.setTitle("J2EE Development without EJB"); + noEJB.setIsbn("54757585"); + + Author rodSaved = authorRepository.save(rod); + Book noEJBSaved = bookRepository.save(noEJB); + + ericSaved.getBooks().add(dddSaved); + rodSaved.getBooks().add(noEJBSaved); + dddSaved.getAuthors().add(ericSaved); + noEJBSaved.getAuthors().add(rodSaved); + + // my Task: + Publisher john = new Publisher(); + john.setPublisherName("John"); + john.setAddress("Warynskiego 6"); + Publisher savedPublisher = publisherRepository.save(john); + + dddSaved.setPublisher(savedPublisher); + noEJB.setPublisher(savedPublisher); + + authorRepository.save(ericSaved); + authorRepository.save(rodSaved); + bookRepository.save(dddSaved); + bookRepository.save(noEJBSaved); + + System.out.println("In Bootstrap"); + System.out.println("Author Count: "+ authorRepository.count()); + System.out.println("Book Count: "+ bookRepository.count()); + System.out.println("Publisher Count: "+ publisherRepository.count()); + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/controllers/AuthorController.java b/src/main/java/guru/springframework/spring6webapp/controllers/AuthorController.java new file mode 100644 index 00000000..8fc98ecb --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/controllers/AuthorController.java @@ -0,0 +1,23 @@ +package guru.springframework.spring6webapp.controllers; + +import guru.springframework.spring6webapp.services.AuthorService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + + +@Controller +public class AuthorController { + private final AuthorService authorService; + + public AuthorController(AuthorService authorService) { + this.authorService = authorService; + } + @RequestMapping("/authors") + public String getAuthors(Model model){ + + model.addAttribute("authors", authorService.findAll()); + return "authors"; + } + +} diff --git a/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java b/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java new file mode 100644 index 00000000..f2d7dfb3 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/controllers/BookController.java @@ -0,0 +1,23 @@ +package guru.springframework.spring6webapp.controllers; + +import guru.springframework.spring6webapp.services.BookService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class BookController { + private final BookService bookService; + + public BookController(BookService bookService) { + this.bookService = bookService; + } + + @RequestMapping("/books") + public String getBooks(Model model){ + + model.addAttribute("books", bookService.findAll()); + + return "books"; + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Author.java b/src/main/java/guru/springframework/spring6webapp/domain/Author.java index 8085f82e..1ce11859 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Author.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Author.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; +import java.util.HashSet; import java.util.Set; /** @@ -17,7 +18,7 @@ public class Author { private String lastName; @ManyToMany(mappedBy = "authors") - private Set books; + private Set books = new HashSet<>(); public Set getBooks() { return books; @@ -50,4 +51,29 @@ public String getLastName() { public void setLastName(String lastName) { this.lastName = lastName; } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Author)) return false; + + Author author = (Author) o; + + return getId() != null ? getId().equals(author.getId()) : author.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } } diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Book.java b/src/main/java/guru/springframework/spring6webapp/domain/Book.java index cd3f6a3b..4de55676 100644 --- a/src/main/java/guru/springframework/spring6webapp/domain/Book.java +++ b/src/main/java/guru/springframework/spring6webapp/domain/Book.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; +import java.util.HashSet; import java.util.Set; /** @@ -19,7 +20,18 @@ public class Book { @ManyToMany @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) - private Set authors; + private Set authors = new HashSet<>(); + + @ManyToOne + private Publisher publisher; + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } public Set getAuthors() { return authors; @@ -52,4 +64,29 @@ public String getIsbn() { public void setIsbn(String isbn) { this.isbn = isbn; } -} + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Book)) return false; + + Book book = (Book) o; + + return getId() != null ? getId().equals(book.getId()) : book.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } +} \ No newline at end of file diff --git a/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java new file mode 100644 index 00000000..a1674068 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/domain/Publisher.java @@ -0,0 +1,96 @@ +package guru.springframework.spring6webapp.domain; + +import jakarta.persistence.*; + +import java.util.Set; + +@Entity +public class Publisher { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String publisherName; + private String address; + private String city; + private String state; + private String zip; + + @OneToMany(mappedBy = "publisher") + private Set books; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getPublisherName() { + return publisherName; + } + + public void setPublisherName(String publisherName) { + this.publisherName = publisherName; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", publisherName='" + publisherName + '\'' + + ", address='" + address + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Publisher)) return false; + + Publisher publisher = (Publisher) o; + + return getId() != null ? getId().equals(publisher.getId()) : publisher.getId() == null; + } + + @Override + public int hashCode() { + return getId() != null ? getId().hashCode() : 0; + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java new file mode 100644 index 00000000..5ff4c917 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/AuthorRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java new file mode 100644 index 00000000..5b3dc62d --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/BookRepository.java @@ -0,0 +1,8 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { + +} diff --git a/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java b/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java new file mode 100644 index 00000000..c0b3218c --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/repositories/PublisherRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.repositories; + +import guru.springframework.spring6webapp.domain.Publisher; +import org.springframework.data.repository.CrudRepository; + +public interface PublisherRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring6webapp/services/AuthorService.java b/src/main/java/guru/springframework/spring6webapp/services/AuthorService.java new file mode 100644 index 00000000..3f6ade1a --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/AuthorService.java @@ -0,0 +1,7 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Author; + +public interface AuthorService { + Iterable findAll(); +} diff --git a/src/main/java/guru/springframework/spring6webapp/services/AuthorServiceImpl.java b/src/main/java/guru/springframework/spring6webapp/services/AuthorServiceImpl.java new file mode 100644 index 00000000..77dbe6a2 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/AuthorServiceImpl.java @@ -0,0 +1,19 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Author; +import guru.springframework.spring6webapp.repositories.AuthorRepository; +import org.springframework.stereotype.Service; + +@Service +public class AuthorServiceImpl implements AuthorService{ + private final AuthorRepository authorRepository; + + public AuthorServiceImpl(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @Override + public Iterable findAll() { + return authorRepository.findAll(); + } +} diff --git a/src/main/java/guru/springframework/spring6webapp/services/BookService.java b/src/main/java/guru/springframework/spring6webapp/services/BookService.java new file mode 100644 index 00000000..1cfef45f --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/BookService.java @@ -0,0 +1,8 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Book; + +public interface BookService { + + Iterable findAll(); +} diff --git a/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java b/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java new file mode 100644 index 00000000..d1b2b067 --- /dev/null +++ b/src/main/java/guru/springframework/spring6webapp/services/BookServiceImpl.java @@ -0,0 +1,20 @@ +package guru.springframework.spring6webapp.services; + +import guru.springframework.spring6webapp.domain.Book; +import guru.springframework.spring6webapp.repositories.BookRepository; +import org.springframework.stereotype.Service; + +@Service +public class BookServiceImpl implements BookService { + + private final BookRepository bookRepository; + + public BookServiceImpl(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @Override + public Iterable findAll() { + return bookRepository.findAll(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b137891..88bee100 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ - +spring.h2.console.enabled= true \ No newline at end of file diff --git a/src/main/resources/templates/authors.html b/src/main/resources/templates/authors.html new file mode 100644 index 00000000..0aaabaef --- /dev/null +++ b/src/main/resources/templates/authors.html @@ -0,0 +1,22 @@ + + + + + Spring Framework Guru + + +

Author list

+ + + + + + + + + + + +
IDTitlePublisher
123Spring in Action Wrox
+ + \ No newline at end of file diff --git a/src/main/resources/templates/books.html b/src/main/resources/templates/books.html new file mode 100644 index 00000000..bdc56407 --- /dev/null +++ b/src/main/resources/templates/books.html @@ -0,0 +1,22 @@ + + + + + Spring Framework Guru + + +

Book list

+ + + + + + + + + + + +
IDTitlePublisher
123Spring in Action Wrox
+ + \ No newline at end of file