diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/AuthorController.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/AuthorController.java new file mode 100644 index 0000000000..b328db9170 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/AuthorController.java @@ -0,0 +1,20 @@ +package guru.springframework.spring5webapp.bootstrap; + +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class AuthorController { + private final AuthorRepository authorRepository; + + public AuthorController(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + @RequestMapping("/authors") + public String getAuthors(Model model){ + model.addAttribute("authors",authorRepository.findAll()); + return"authors/authorsList"; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java new file mode 100644 index 0000000000..293c21c88a --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootStrapData.java @@ -0,0 +1,61 @@ +package guru.springframework.spring5webapp.bootstrap; + +import guru.springframework.spring5webapp.model.Author; +import guru.springframework.spring5webapp.model.Book; +import guru.springframework.spring5webapp.model.Publisher; +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import guru.springframework.spring5webapp.repositories.BookRepository; +import guru.springframework.spring5webapp.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 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 author1 = new Author("Ahmed", "Yamir"); + Book book1 = new Book("Spring boot", "12QS"); + + author1.getBooks().add(book1); + book1.getAuthors().add(author1); + + authorRepository.save(author1); + bookRepository.save(book1); + + Author author2 = new Author("Taha", "Salhi"); + Book book2 = new Book("C++", "AZ321GFF"); + + author2.getBooks().add(book2); + book2.getAuthors().add(author2); + + authorRepository.save(author2); + bookRepository.save(book2); + + + + Publisher publisger=new Publisher("Publisger 1","AddressL1","Ottawa","Ontario","K3E 6T9"); + book1.setPublisher(publisger); + publisger.getBooks().add(book1); + + book2.setPublisher(publisger); + publisger.getBooks().add(book2); + + publisherRepository.save(publisger); + + System.out.println("Number of book here: " + bookRepository.count()); + System.out.println("Number of author here: " + authorRepository.count()); + System.out.println("Number of publisher here: " + publisherRepository.count()); + + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/controllers/BookControllers.java b/src/main/java/guru/springframework/spring5webapp/controllers/BookControllers.java new file mode 100644 index 0000000000..38ad4276f8 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/controllers/BookControllers.java @@ -0,0 +1,22 @@ +package guru.springframework.spring5webapp.controllers; + +import guru.springframework.spring5webapp.repositories.BookRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class BookControllers { + + private final BookRepository bookRepository; + + public BookControllers(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @RequestMapping("/books") + public String getBooks(Model model){ + model.addAttribute("books",bookRepository.findAll()); + return "books/list"; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/model/Author.java b/src/main/java/guru/springframework/spring5webapp/model/Author.java new file mode 100644 index 0000000000..5fa268c97e --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Author.java @@ -0,0 +1,80 @@ +package guru.springframework.spring5webapp.model; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; +@Entity +public class Author { + @Id + @GeneratedValue(strategy= GenerationType.AUTO) + private Long id; + private String firstName; + private String lastName; + @ManyToMany(mappedBy ="authors") + private Set books=new HashSet<>(); + + public Author() { + } + + public Author(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Author author = (Author) o; + return Objects.equals(id, author.id) && Objects.equals(firstName, author.firstName) && Objects.equals(lastName, author.lastName) && Objects.equals(books, author.books); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } +} + diff --git a/src/main/java/guru/springframework/spring5webapp/model/Book.java b/src/main/java/guru/springframework/spring5webapp/model/Book.java new file mode 100644 index 0000000000..d985b3c77e --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Book.java @@ -0,0 +1,92 @@ +package guru.springframework.spring5webapp.model; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy= GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + @ManyToOne + private Publisher publisher; + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + + @ManyToMany + @JoinTable(name="author_book",joinColumns = @JoinColumn(name="book_id"), inverseJoinColumns =@JoinColumn(name="author_id")) + private Set authors=new HashSet<>();; + + public Book() { + } + + public Book(String title, String isbn) { + this.title = title; + this.isbn = isbn; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Book book = (Book) o; + return Objects.equals(id, book.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/model/Publisher.java b/src/main/java/guru/springframework/spring5webapp/model/Publisher.java new file mode 100644 index 0000000000..fdf92a3653 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/model/Publisher.java @@ -0,0 +1,113 @@ +package guru.springframework.spring5webapp.model; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +public class Publisher { + @Id + @GeneratedValue(strategy= GenerationType.AUTO) + private Long id; + + private String name; + private String AddressLine1; + private String city; + private String state; + private String zip; + @OneToMany + @JoinColumn(name="publisher_id") + private Set books=new HashSet<>(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + public Publisher() { + } + + public Publisher(String name, String addressLine1, String city, String state, String zip) { + this.name = name; + AddressLine1 = addressLine1; + this.city = city; + this.state = state; + this.zip = zip; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddressLine1() { + return AddressLine1; + } + + public void setAddressLine1(String addressLine1) { + AddressLine1 = addressLine1; + } + + 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 boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Publisher publisher = (Publisher) o; + return Objects.equals(name, publisher.name) && Objects.equals(AddressLine1, publisher.AddressLine1) && Objects.equals(city, publisher.city) && Objects.equals(state, publisher.state) && Objects.equals(zip, publisher.zip); + } + + @Override + public int hashCode() { + return Objects.hash(name, AddressLine1, city, state, zip); + } + + @Override + public String toString() { + return "Puplisher{" + + "name='" + name + '\'' + + ", AddressLine1='" + AddressLine1 + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + '}'; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java new file mode 100644 index 0000000000..364f1cd8fb --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.model.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java new file mode 100644 index 0000000000..9c21f0270f --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.model.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java new file mode 100644 index 0000000000..332f7f01b5 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.model.Publisher; +import org.springframework.data.repository.CrudRepository; + +public interface PublisherRepository extends CrudRepository { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb2..69b89983cb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.h2.console.enabled=true \ No newline at end of file diff --git a/src/main/resources/templates/authors/authorsList.html b/src/main/resources/templates/authors/authorsList.html new file mode 100644 index 0000000000..43c37e4e54 --- /dev/null +++ b/src/main/resources/templates/authors/authorsList.html @@ -0,0 +1,22 @@ + + + + + Title + + +

List of authors

+ + + + + + + + + + + +
IDFirstNameLastName
+ + \ No newline at end of file diff --git a/src/main/resources/templates/books/list.html b/src/main/resources/templates/books/list.html new file mode 100644 index 0000000000..5ff460e95c --- /dev/null +++ b/src/main/resources/templates/books/list.html @@ -0,0 +1,22 @@ + + + + + Title + + +

List of book

+ + + + + + + + + + + +
IDTitlePublisher
+ + \ No newline at end of file