Skip to content

first commit #17

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 7 commits into
base: master
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,37 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

</dependencies>

<build>
<finalName>lsteinm-web40-spring-challenge</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>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
* Configures which database we are using based on a property in application.properties
*/
@Configuration
public class DataSourceConfig
{
/**
* The property from application properties. Defaults to H2
*/
@Value("${local.run.db:h2}")
private String dbValue;

/**
* A config var for the database link - defaults to nothing
*/
@Value("${spring.datasource.url:}")
private String dbURL;

/**
* The actual datasource configuration
*
* @return the datasource to use
*/
@Bean
public DataSource dataSource()
{
if (dbValue.equalsIgnoreCase("POSTGRESQL"))
{
// Assume Heroku
HikariConfig config = new HikariConfig();
config.setDriverClassName("org.postgresql.Driver");
config.setJdbcUrl(dbURL);
return new HikariDataSource(config);
} else
{
// Assume H2
String myURLString = "jdbc:h2:mem:testdb";
String myDriverClass = "org.h2.Driver";
String myDBUser = "sa";
String myDBPassword = "";

return DataSourceBuilder.create()
.username(myDBUser)
.password(myDBPassword)
.url(myURLString)
.driverClassName(myDriverClass)
.build();
}
}
}
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 @@ -57,6 +58,9 @@ public void configure(HttpSecurity http)
"/oauth/revoke-token",
"/logout")
.authenticated()
.antMatchers("/books/books",
"/books/book/**")
.hasAnyRole("ADMIN", "DATA", "USER")
.antMatchers("/roles/**")
.hasAnyRole("ADMIN", "DATA")
.anyRequest().denyAll()
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 @@ -30,6 +31,7 @@ public class BookController
BookService bookService;

// http://localhost:2019/books/books

@GetMapping(value = "/books",
produces = {"application/json"})
public ResponseEntity<?> listAllBooks(HttpServletRequest request)
Expand All @@ -40,6 +42,7 @@ public ResponseEntity<?> listAllBooks(HttpServletRequest request)
}

// http://localhost:2019/books/book/{bookId}

@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
@@ -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 @@ -45,7 +46,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 Down Expand Up @@ -134,7 +135,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
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Which DB to run
#local.run.db=H2
local.run.db=POSTGRESQL
# Configurations useful for working with H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
Expand All @@ -18,7 +21,7 @@ spring.jpa.open-in-view=true
# What do with the schema
# drop n create table again, good for testing
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always
spring.datasource.initialization-mode=never
#
# To turn off loading of SeedData. Default is true
# command.line.runner.enabled=false
Expand Down
Loading