From 067d1684e11eb54142168f9b1902676276658c7f Mon Sep 17 00:00:00 2001 From: jalajiiitd Date: Mon, 10 Oct 2022 08:15:25 +0530 Subject: [PATCH 1/2] completed assignments --- pom.xml | 1 + src/main/resources/application.properties | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/pom.xml b/pom.xml index f34c1936df..372e473594 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,7 @@ h2 runtime + org.springframework.boot spring-boot-starter-test diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb2..c462d358a6 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1,7 @@ +server.port=8083 +spring.h2.console.enabled=true +spring.jpa.open-in-view=true +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password= \ No newline at end of file From 74b768afd209b875c40f117a0e4868700e1e59a4 Mon Sep 17 00:00:00 2001 From: jalajiiitd Date: Mon, 10 Oct 2022 08:20:41 +0530 Subject: [PATCH 2/2] Completed assignments --- .../bootstrap/BootstrapData.java | 65 +++++++++ .../controllers/AuthorController.java | 25 ++++ .../controllers/BookController.java | 26 ++++ .../spring5webapp/domain/Author.java | 92 +++++++++++++ .../spring5webapp/domain/Book.java | 106 +++++++++++++++ .../spring5webapp/domain/Publisher.java | 124 ++++++++++++++++++ .../repositories/AuthorRepository.java | 9 ++ .../repositories/BookRepository.java | 9 ++ .../repositories/PublisherRepository.java | 9 ++ .../resources/templates/authors/list.html | 26 ++++ src/main/resources/templates/books/list.html | 26 ++++ 11 files changed, 517 insertions(+) create mode 100644 src/main/java/guru/springframework/spring5webapp/bootstrap/BootstrapData.java create mode 100644 src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java create mode 100644 src/main/java/guru/springframework/spring5webapp/controllers/BookController.java create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Author.java create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Book.java create mode 100644 src/main/java/guru/springframework/spring5webapp/domain/Publisher.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java create mode 100644 src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java create mode 100644 src/main/resources/templates/authors/list.html create mode 100644 src/main/resources/templates/books/list.html 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..dd46817ae1 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootstrapData.java @@ -0,0 +1,65 @@ +package guru.springframework.spring5webapp.bootstrap; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import guru.springframework.spring5webapp.domain.Author; +import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.domain.Publisher; +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import guru.springframework.spring5webapp.repositories.BookRepository; +import guru.springframework.spring5webapp.repositories.PublisherRepository; + +@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 { + // TODO Auto-generated method stub + + Author eric = new Author("Eric", "Evans"); + Book ddd = new Book("Domain Driven Design","4215123"); + Publisher pearson = new Publisher("Pearson", "214 Ching Street", "Kansas", "MA", 2088012L) ; + + + eric.getBooks().add(ddd); + ddd.getAuthors().add(eric); + + authorRepository.save(eric); + bookRepository.save(ddd); + ddd.setPublisher(pearson); + pearson.getBooks().add(ddd); + + Author rod = new Author("Rod","Jonson"); + Book notJb = new Book("J2EE without EJ8","1253124"); + + + rod.getBooks().add(notJb); + notJb.getAuthors().add(rod); + + authorRepository.save(rod); + bookRepository.save(notJb); + publisherRepository.save(pearson); + pearson.getBooks().add(notJb); + + + notJb.setPublisher(pearson); + + System.out.println("Started in bootstrap"); + System.out.println("Number of books: "+bookRepository.count()); + System.out.println("Number of publishers: "+publisherRepository.count()); + } + +} diff --git a/src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java b/src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java new file mode 100644 index 0000000000..c9db596908 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/controllers/AuthorController.java @@ -0,0 +1,25 @@ +package guru.springframework.spring5webapp.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import guru.springframework.spring5webapp.repositories.AuthorRepository; + +@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/list"; + } + +} diff --git a/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java b/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java new file mode 100644 index 0000000000..3d21c78dce --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/controllers/BookController.java @@ -0,0 +1,26 @@ +package guru.springframework.spring5webapp.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import guru.springframework.spring5webapp.repositories.BookRepository; + +@Controller +public class BookController { + + private final BookRepository bookRepository; + + public BookController(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/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java new file mode 100644 index 0000000000..1f42dda238 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -0,0 +1,92 @@ +package guru.springframework.spring5webapp.domain; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToMany; + + +@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 int hashCode() { + return Objects.hash(id); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Author other = (Author) obj; + return Objects.equals(id, other.id); + } + + @Override + public String toString() { + return "Author [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName +"]"; + } + + + +} diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java new file mode 100644 index 0000000000..87cd67ce0c --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,106 @@ +package guru.springframework.spring5webapp.domain; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; + +@Entity +public class Book { + + private String title; + private String isbn; + + @ManyToOne + private Publisher publisher; + + @ManyToMany + @JoinTable(name="author_book", joinColumns = @JoinColumn(name="book_id"), + inverseJoinColumns = @JoinColumn(name="author_id")) + private Set authors = new HashSet<>(); + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + public Book() {} + + public Book(String title, String isbn) { + this.title = title; + this.isbn = isbn; + } + + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + + 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 int hashCode() { + return Objects.hash(id); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Book other = (Book) obj; + return Objects.equals(id, other.id); + } + + @Override + public String toString() { + return "Book [title=" + title + ", isbn=" + isbn + ", id=" + id + "]"; + } + + + +} diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java new file mode 100644 index 0000000000..1628714d4f --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java @@ -0,0 +1,124 @@ +package guru.springframework.spring5webapp.domain; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; + +@Entity +public class Publisher { + + private String name; + private String address; + private String city; + private String state; + private Long zip; + + @OneToMany + @JoinColumn(name="pubId") + private Set books = new HashSet<>(); + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long pubId; + + public Publisher() { + } + + public Publisher(String name, String address, String city, String state, Long zip) { + super(); + this.name = name; + this.address = address; + this.city = city; + this.state = state; + this.zip = zip; + } + + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + 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 Long getZip() { + return zip; + } + + public void setZip(Long zip) { + this.zip = zip; + } + + public Long getPubId() { + return pubId; + } + + public void setPubId(Long pubId) { + this.pubId = pubId; + } + + @Override + public int hashCode() { + return Objects.hash(pubId); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Publisher other = (Publisher) obj; + return Objects.equals(pubId, other.pubId); + } + + @Override + public String toString() { + return "Publisher [name=" + name + ", address=" + address + ", city=" + city + ", state=" + state + ", zip=" + + zip + ", pubId=" + pubId + "]"; + } + + +} 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..accebe1831 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java @@ -0,0 +1,9 @@ +package guru.springframework.spring5webapp.repositories; + +import org.springframework.data.repository.CrudRepository; + +import guru.springframework.spring5webapp.domain.Author; + +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..ecaa4265ef --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java @@ -0,0 +1,9 @@ +package guru.springframework.spring5webapp.repositories; + +import org.springframework.data.repository.CrudRepository; + +import guru.springframework.spring5webapp.domain.Book; + +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..546e868a0c --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java @@ -0,0 +1,9 @@ +package guru.springframework.spring5webapp.repositories; + +import org.springframework.data.repository.CrudRepository; + +import guru.springframework.spring5webapp.domain.Publisher; + +public interface PublisherRepository extends CrudRepository { + +} diff --git a/src/main/resources/templates/authors/list.html b/src/main/resources/templates/authors/list.html new file mode 100644 index 0000000000..9db09c4686 --- /dev/null +++ b/src/main/resources/templates/authors/list.html @@ -0,0 +1,26 @@ + + + + + + Spring Framework Guru + + + +

Authors List

+ + + + + + + + + + + + +
IDFirst NameLast Name
123Spring in ActionWrox
+ + + \ 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..bc9909449e --- /dev/null +++ b/src/main/resources/templates/books/list.html @@ -0,0 +1,26 @@ + + + + + + Spring Framework Guru + + + +

Book List

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