Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-h2console</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package guru.springframework.spring7webapp.bootstrap;

import guru.springframework.spring7webapp.domain.Author;
import guru.springframework.spring7webapp.domain.Book;
import guru.springframework.spring7webapp.domain.Publisher;
import guru.springframework.spring7webapp.repositories.AuthorRepository;
import guru.springframework.spring7webapp.repositories.BookRepository;
import guru.springframework.spring7webapp.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 joop = new Author();
joop.setFirstName("Joop");
joop.setLastName("Appelstroop");

Book ddd = new Book();
ddd.setTitle("DomainDrivenDesign");
ddd.setIsbn("123128384");

Author joopSaved = authorRepository.save(joop);
Book dddSaved = bookRepository.save(ddd);

Author piet = new Author();
piet.setFirstName("Piet");
piet.setLastName("Vergeetmijniet");

Book edd = new Book();
edd.setTitle("Event Driven Design");
edd.setIsbn("01293434597");

Author pietSaved = authorRepository.save(piet);
Book eddSaved = bookRepository.save(edd);

Publisher publisher = new Publisher();
publisher.setAddress("teststreet 26");
publisher.setCity("New York");
publisher.setState("New York");
publisher.setPublisherName("El publicity");

Publisher savedPublisher = publisherRepository.save(publisher);

dddSaved.setPublisher(savedPublisher);
eddSaved.setPublisher(savedPublisher);

Book test = bookRepository.save(eddSaved);
Book test2 = bookRepository.save(dddSaved);
authorRepository.save(joopSaved);
authorRepository.save(pietSaved);

System.out.println("Authors count: " + authorRepository.count());
System.out.println("Book count: " + bookRepository.count());
System.out.println("Publisher count: " + publisherRepository.count());

System.out.println("Publisher count: " + test.getPublisher().getPublisherName());
System.out.println("Publisher count: " + test2.getPublisher().getPublisherName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.spring7webapp.controllers;

import guru.springframework.spring7webapp.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(path = "/authors")
public String authors(Model model) {
model.addAttribute("authors", this.authorService.findAll());

return "authors";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package guru.springframework.spring7webapp.controllers;

import guru.springframework.spring7webapp.domain.Book;
import guru.springframework.spring7webapp.services.Bookservice;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BookController {

private final Bookservice bookservice;

public BookController(Bookservice bookservice) {
this.bookservice = bookservice;
}

@RequestMapping(path = "/books")
public String getBooks(Model model) {
model.addAttribute("books", this.bookservice.findAll());

return "books";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package guru.springframework.spring7webapp.domain;

import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false)
private Long id;

private String firstName;
private String lastName;

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

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

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

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

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Author author)) return false;

return Objects.equals(getId(), author.getId());
}

@Override
public int hashCode() {
return Objects.hashCode(getId());
}
}
93 changes: 93 additions & 0 deletions src/main/java/guru/springframework/spring7webapp/domain/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package guru.springframework.spring7webapp.domain;

import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false)
private Long id;

private String title;
private String isbn;

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

@ManyToOne
@JoinColumn(name = "publisher_id")
private 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 = new HashSet<>();
}

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

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

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Book book)) return false;

return Objects.equals(getId(), book.getId());
}

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

public Publisher getPublisher() {
return publisher;
}

public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}
}
Loading