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
41 changes: 26 additions & 15 deletions bookstore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,101 @@
<version>0.0.1-SNAPSHOT</version>
<name>bookstore</name>
<description>Bookstore project for Spring Boot</description>

<properties>
<java.version>14</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- <scope>runtime</scope>-->
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Swagger Dependencies Start -->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger Dependencies End -->

<!-- Security Dependencies Start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
<!-- Security Dependencies End -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>

<build>
<finalName>mjschuette4-bookstore</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.heroku.sdk</groupId>
<artifactId>heroku-maven-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<appName>${project.build.finalName}</appName>
<includeTarget>false</includeTarget>
<includes>
<include>${project.build.directory}/${project.build.finalName}.jar</include>
</includes>
<jdkVersion>${java.version}</jdkVersion>
<processTypes>
<web>java $JAVA_OPTS -Dserver.port=$PORT -jar target/${project.build.finalName}.jar</web>
</processTypes>
</configuration>
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.lambdaschool.bookstore.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig
{
@Value("${spring.datasource.url}")
private String dbUrl;

@Bean
public DataSource dataSource()
{
HikariConfig config = new HikariConfig();
config.setJdbcUrl(dbUrl);
return new HikariDataSource(config);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lambdaschool.bookstore.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
Expand Down Expand Up @@ -59,6 +60,14 @@ public void configure(HttpSecurity http)
.authenticated()
.antMatchers("/roles/**")
.hasAnyRole("ADMIN", "DATA")
.antMatchers(HttpMethod.GET,"/books/books")
.hasAnyRole("ADMIN", "DATA")
.antMatchers(HttpMethod.POST, "/books/book")
.hasAnyRole("ADMIN")
.antMatchers(HttpMethod.PUT, "/books/book")
.hasAnyRole("ADMIN")
.antMatchers(HttpMethod.DELETE, "/books/book")
.hasAnyRole("ADMIN")
.and()
.exceptionHandling()
.accessDeniedHandler(new OAuth2AccessDeniedHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -29,6 +30,7 @@ public class BookController
@Autowired
BookService bookService;

@PreAuthorize("hasAnyRole('ADMIN','DATA')")
// http://localhost:2019/books/books
@GetMapping(value = "/books",
produces = {"application/json"})
Expand All @@ -40,6 +42,7 @@ public ResponseEntity<?> listAllBooks(HttpServletRequest request)
}

// http://localhost:2019/books/book/{bookId}
@PreAuthorize("hasAnyRole('ADMIN', 'DATA')")
@GetMapping(value = "/book/{bookId}",
produces = {"application/json"})
public ResponseEntity<?> getBookById(HttpServletRequest request,
Expand All @@ -52,6 +55,7 @@ public ResponseEntity<?> getBookById(HttpServletRequest request,
}

// POST http://localhost:2019/books/book
@PreAuthorize("hasAnyRole('ADMIN')")
@PostMapping(value = "/book", consumes = "application/json")
public ResponseEntity<?> addNewBook(@Valid @RequestBody Book newBook) throws
URISyntaxException
Expand All @@ -73,6 +77,7 @@ public ResponseEntity<?> addNewBook(@Valid @RequestBody Book newBook) throws
}

// PUT http://localhost:2019/books/book/1
@PreAuthorize("hasAnyRole('ADMIN')")
@PutMapping(value = "/book/{bookid}",
consumes = "application/json")
public ResponseEntity<?> updateFullBook(
Expand All @@ -89,6 +94,7 @@ public ResponseEntity<?> updateFullBook(
}

// DELETE http://localhost:2019/books/book/1
@PreAuthorize("hasAnyRole('ADMIN')")
@DeleteMapping(value = "/book/{id}")
public ResponseEntity<?> deleteBookById(
@PathVariable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public RestExceptionHandler()
}

/**
* Our custom handling of ResourceNotFoundExceptions. This gets thrown manually by our application.
* Our custom handling of ResourceNotFoundException(s. This gets thrown manually by our application.
*
* @param rnfe All the information about the exception that is thrown.
* @return The error details for displaying to the client plus the status Not Found.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lambdaschool.bookstore.services;

import com.lambdaschool.bookstore.exceptions.ResourceNotFoundException;
import com.lambdaschool.bookstore.models.Author;
import com.lambdaschool.bookstore.models.Book;
import com.lambdaschool.bookstore.models.Wrote;
Expand Down Expand Up @@ -44,7 +45,7 @@ public List<Book> findAll()
public Book findBookById(long id)
{
return bookrepos.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Book with id " + id + " Not Found!"));
.orElseThrow(() -> new ResourceNotFoundException("Book with id " + id + " Not Found!"));
}

@Transactional
Expand All @@ -57,7 +58,7 @@ public void delete(long id)
bookrepos.deleteById(id);
} else
{
throw new EntityNotFoundException("Book with id " + id + " Not Found!");
throw new ResourceNotFoundException("Book with id " + id + " Not Found!");
}
}

Expand All @@ -70,12 +71,14 @@ public Book save(Book book)
if (book.getBookid() != 0)
{
bookrepos.findById(book.getBookid())
.orElseThrow(() -> new EntityNotFoundException("Book id " + book.getBookid() + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Book id " + book.getBookid() + " not found!"));
}

newBook.setTitle(book.getTitle());
newBook.setIsbn(book.getIsbn());
newBook.setCopy(book.getCopy());
newBook.hasvalueforcopy = false;

if (book.getSection() != null)
{
newBook.setSection(sectionService.findSectionById(book.getSection()
Expand All @@ -88,7 +91,7 @@ public Book save(Book book)
{
Author addAuthor = authorrepos.findById(w.getAuthor()
.getAuthorid())
.orElseThrow(() -> new EntityNotFoundException("Author Id " + w.getAuthor()
.orElseThrow(() -> new ResourceNotFoundException("Author Id " + w.getAuthor()
.getAuthorid() + " Not Found!"));
newBook.getWrotes()
.add(new Wrote(addAuthor, newBook));
Expand Down Expand Up @@ -133,7 +136,7 @@ public Book update(Book book,
{
Author addAuthor = authorrepos.findById(w.getAuthor()
.getAuthorid())
.orElseThrow(() -> new EntityNotFoundException("Author Id " + w.getAuthor()
.orElseThrow(() -> new ResourceNotFoundException("Author Id " + w.getAuthor()
.getAuthorid() + " Not Found!"));
currentBook.getWrotes()
.add(new Wrote(addAuthor, currentBook));
Expand Down
11 changes: 11 additions & 0 deletions bookstore/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Which db to run
# local.run.db=H2
local.run.db=POSTGRESQL
#
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true
#
# Configurations useful for working with H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
Expand Down
32 changes: 16 additions & 16 deletions bookstore/src/test/java/com/lambdaschool/bookstore/SeedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void run(String[] args) throws
r3 = roleService.save(r3);

// admin, data, user
User u1 = new User("admin",
User u1 = new User("test_admin",
"password",
"admin@lambdaschool.local");
u1.getRoles()
Expand All @@ -98,7 +98,7 @@ public void run(String[] args) throws
userService.save(u1);

// data, user
User u2 = new User("cinnamon",
User u2 = new User("test_cinnamon",
"1234567",
"cinnamon@lambdaschool.local");
u2.getRoles()
Expand All @@ -117,7 +117,7 @@ public void run(String[] args) throws
userService.save(u2);

// user
User u3 = new User("barnbarn",
User u3 = new User("test_barnbarn",
"ILuvM4th!",
"barnbarn@lambdaschool.local");
u3.getRoles()
Expand All @@ -127,14 +127,14 @@ public void run(String[] args) throws
"barnbarn@email.local"));
userService.save(u3);

User u4 = new User("puttat",
User u4 = new User("test_puttat",
"password",
"puttat@school.lambda");
u4.getRoles()
.add(new UserRoles(u4, r2));
userService.save(u4);

User u5 = new User("misskitty",
User u5 = new User("test_misskitty",
"password",
"misskitty@school.lambda");
u5.getRoles()
Expand All @@ -145,12 +145,12 @@ public void run(String[] args) throws
* Seed Books
************/

Author a1 = new Author("John", "Mitchell");
Author a2 = new Author("Dan", "Brown");
Author a3 = new Author("Jerry", "Poe");
Author a4 = new Author("Wells", "Teague");
Author a5 = new Author("George", "Gallinger");
Author a6 = new Author("Ian", "Stewart");
Author a1 = new Author("test_John", "Mitchell");
Author a2 = new Author("test_Dan", "Brown");
Author a3 = new Author("test_Jerry", "Poe");
Author a4 = new Author("test_Wells", "Teague");
Author a5 = new Author("test_George", "Gallinger");
Author a6 = new Author("test_Ian", "Stewart");

a1 = authorService.save(a1);
a2 = authorService.save(a2);
Expand All @@ -171,29 +171,29 @@ public void run(String[] args) throws
s4 = sectionService.save(s4);
s5 = sectionService.save(s5);

Book b1 = new Book("Flatterland", "9780738206752", 2001, s1);
Book b1 = new Book("test_Flatterland", "9780738206752", 2001, s1);
b1.getWrotes()
.add(new Wrote(a6, new Book()));
b1 = bookService.save(b1);

Book b2 = new Book("Digital Fortess", "9788489367012", 2007, s1);
Book b2 = new Book("test_Digital Fortess", "9788489367012", 2007, s1);
b2.getWrotes()
.add(new Wrote(a2, new Book()));
b2 = bookService.save(b2);

Book b3 = new Book("The Da Vinci Code", "9780307474278", 2009, s1);
Book b3 = new Book("test_The Da Vinci Code", "9780307474278", 2009, s1);
b3.getWrotes()
.add(new Wrote(a2, new Book()));
b3 = bookService.save(b3);

Book b4 = new Book("Essentials of Finance", "1314241651234", 0, s4);
Book b4 = new Book("test_Essentials of Finance", "1314241651234", 0, s4);
b4.getWrotes()
.add(new Wrote(a3, new Book()));
b4.getWrotes()
.add(new Wrote(a5, new Book()));
b4 = bookService.save(b4);

Book b5 = new Book("Calling Texas Home", "1885171382134", 2000, s3);
Book b5 = new Book("test_Calling Texas Home", "1885171382134", 2000, s3);
b5.getWrotes()
.add(new Wrote(a4, new Book()));
b5 = bookService.save(b5);
Expand Down
Loading