Skip to content

firstCommit #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package guru.springframework.spring5webapp.bootstrap;

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;
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 {
System.out.println("Started in Bootstrap");

Publisher publisher = new Publisher();
publisher.setName("SFG Publishing");
publisher.setCity("St. Petersburg");
publisher.setState("FL");
publisher.setZip("456987");

publisherRepository.save(publisher);

System.out.println("Publisher Count: " + publisherRepository.count());

Author eric = new Author("Eric", "Evans");
Book ddd = new Book("Domain Driven Design", "123123");
eric.getBooks().add(ddd);
ddd.getAuthors().add(eric);

ddd.setPublisher(publisher);
publisher.getBooks().add(ddd);

authorRepository.save(eric);
bookRepository.save(ddd);
publisherRepository.save(publisher);

Author rod = new Author("Rod", "Johnson");
Book noEJB = new Book("J2EE Development without EJB", "3939459459");
rod.getBooks().add(noEJB);
noEJB.getAuthors().add(rod);
noEJB.setPublisher(publisher);
publisher.getBooks().add(noEJB);

authorRepository.save(rod);
bookRepository.save(noEJB);
publisherRepository.save(publisher);


System.out.println("Number of Books: " + bookRepository.count());
System.out.println("Publisher number of books: " + publisher.getBooks().size());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package guru.springframework.spring5webapp.controllers;

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/list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package guru.springframework.spring5webapp.domain;

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 firsName;
private String lastName;

@ManyToMany(mappedBy = "authors")
private Set<Book> books = new HashSet<>();

public Author() {
}

public Author(String firsName, String lastName) {
this.firsName = firsName;
this.lastName = lastName;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirsName() {
return firsName;
}

public void setFirsName(String firsName) {
this.firsName = firsName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Set<Book> getBooks() {
return books;
}

public void setBooks(Set<Book> books) {
this.books = books;
}

@Override
public String toString() {
return "Author{" +
"id=" + id +
", firsName='" + firsName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}

@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);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

}
96 changes: 96 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/domain/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package guru.springframework.spring5webapp.domain;

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;

@ManyToMany
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns =
@JoinColumn(name = "author_id"))
private Set<Author> authors = new HashSet<>();


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<Author> getAuthors() {
return authors;
}

public void setAuthors(Set<Author> authors) {
this.authors = authors;
}

@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", isbn='" + isbn + '\'' +
'}';
}

@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);
}
}
117 changes: 117 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/domain/Publisher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package guru.springframework.spring5webapp.domain;

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<Book> books = new HashSet<>();


public Publisher() {
}

@Override
public String toString() {
return "Publisher{" +
"id=" + id +
", name='" + name + '\'' +
", addressLine1='" + addressLine1 + '\'' +
", city='" + city + '\'' +
", state='" + state + '\'' +
", 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(id, publisher.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
public Publisher(String name, String addressLine1, String city, String state, String zip) {
this.name = name;
this.addressLine1 = addressLine1;
this.city = city;
this.state = state;
this.zip = zip;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Set<Book> getBooks() {
return books;
}

public void setBooks(Set<Book> books) {
this.books = books;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddressLine1() {
return addressLine1;
}

public void setAddressLine1(String addressLine1) {
this.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;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package guru.springframework.spring5webapp.domain;

public class Set<T> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.domain.Author;
import org.springframework.data.repository.CrudRepository;

public interface AuthorRepository extends CrudRepository<Author, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.domain.Book;
import org.springframework.data.repository.CrudRepository;

public interface BookRepository extends CrudRepository<Book, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.domain.Publisher;
import org.springframework.data.repository.CrudRepository;

public interface PublisherRepository extends CrudRepository<Publisher, Long> {
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.h2.console.enabled=true
23 changes: 23 additions & 0 deletions src/main/resources/templates/authors/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Author List</h1>

<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr th:each="author: ${authors}">
<td th:text="${author.id}">123</td>
<td th:text="${author.firsName}">Joe</td>
<td th:text="${author.lastName}">Smith</td>
</tr>
</table>
</body>
</html>
23 changes: 23 additions & 0 deletions src/main/resources/templates/books/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Book List</h1>

<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Publisher</th>
</tr>
<tr th:each="book: ${books}">
<td th:text="${book.id}">123</td>
<td th:text="${book.title}">Spring in Action</td>
<td th:text="${book.publisher.name}">Wrox</td>
</tr>
</table>
</body>
</html>