Skip to content

Commit

Permalink
Merge pull request #1 from azermirzayev/lesson/lesson_21_configuring_…
Browse files Browse the repository at this point in the history
…mwc_controller

Add parent on pom
  • Loading branch information
azermirzayev authored Oct 31, 2022
2 parents f094047 + 16427f8 commit da84416
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 54 deletions.
61 changes: 9 additions & 52 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<groupId>guru.springframework</groupId>
<artifactId>spring5webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring5webapp</name>
<description>Demo project for Spring Boot</description>

<description>Example Spring 5 Web Application</description>

<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -19,24 +19,14 @@
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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-web</artifactId>
Expand All @@ -51,6 +41,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Expand All @@ -63,43 +59,4 @@
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

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;

Expand All @@ -12,32 +14,54 @@ public class BootStrapData implements CommandLineRunner {

private final AuthorRepository authorRepository;
private final BookRepository bookRepository;
private final PublisherRepository publisherRepository;

public BootStrapData(AuthorRepository authorRepository, BookRepository bookRepository) {
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");

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 j2ee = new Book("J2EE Development without EJB","395454589");
rod.getBooks().add(j2ee);
j2ee.getAuthors().add(rod);

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

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

System.out.println("Started in bootstrap: "+ bookRepository.count());
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,23 @@
package guru.springframework.spring5webapp.controller;

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";
}
}
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

0 comments on commit da84416

Please sign in to comment.