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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@ Commit your code regularly and meaningfully. This helps both you (in case you ev
Be prepared to demonstrate your understanding of this week's concepts by answering questions on the following topics. You might prepare by writing down your own answers before hand.

1. Can you explain exception handling in your application?

Exception can be handled manually if we know what might happen. For example, searched resource doesn't exist or request user is not authorized. A custom error class is created to be thrown when exception happens.

2. Can you explain your user authentication flow?

User requests login with credentials. If request credentials match with database, a token is sent back to the client side which can be passed on in header for future requests.

3. Can you show me your unit tests and describe how they work?

For testing services, units can be as simple as "assertEquals(expected, actual)". We implement the tested method and find a unique value from that method to compare with our expected value.
For testing controllers, a test data need to be setup before. We use Mockmvc to create a mock web request and then use mock data to simulate HTTP requests. Finally, we compare the response data with expected data.

4. Can you show how you deployed your application to a cloud service with a persistent database?

We can choose Postgresql as our RDMS for data persistence. Create a heroku app and add necessary environment variables. Then use maven heroku plugin to deploy onto heroku.


## Instructions

### Task 1: Project Set Up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ <h2 title="Class SeedData" class="title">Class SeedData</h2>
<li>java.lang.Object</li>
<li>
<ul class="inheritance">
<li>com.lambdaschool.foundation.SeedData</li>
<li>com.lambdaschool.foundation.SeedData_user</li>
</ul>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion foundation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
</dependencies>

<build>
<finalName>jrmmba-foundation</finalName>
<finalName>jren-foundation-sprint</finalName>

<plugins>
<plugin>
Expand Down
97 changes: 83 additions & 14 deletions foundation/src/main/java/com/lambdaschool/foundation/SeedData.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
import com.github.javafaker.Faker;
import com.github.javafaker.service.FakeValuesService;
import com.github.javafaker.service.RandomService;
import com.lambdaschool.foundation.models.Role;
import com.lambdaschool.foundation.models.User;
import com.lambdaschool.foundation.models.UserRoles;
import com.lambdaschool.foundation.models.Useremail;
import com.lambdaschool.foundation.services.RoleService;
import com.lambdaschool.foundation.services.UserService;
import com.lambdaschool.foundation.models.*;
import com.lambdaschool.foundation.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
Expand All @@ -40,6 +37,15 @@ public class SeedData
@Autowired
UserService userService;

@Autowired
AuthorService authorService;

@Autowired
BookService bookService;

@Autowired
SectionService sectionService;

/**
* Generates test, seed data for our application
* First a set of known data is seeded into our database.
Expand All @@ -53,8 +59,10 @@ public class SeedData
@Override
public void run(String[] args)
throws
Exception
{
Exception {
/************
* Seed Users
************/
Role r1 = new Role("admin");
Role r2 = new Role("user");
Role r3 = new Role("data");
Expand Down Expand Up @@ -153,15 +161,76 @@ public void run(String[] args)
users.add(new UserRoles(new User(),
r2));
fakeUser = new User(nameFaker.name()
.username(),
"password",
nameFaker.internet()
.emailAddress(),
users);
.username(),
"password",
nameFaker.internet()
.emailAddress(),
users);
fakeUser.getUseremails()
.add(new Useremail(fakeUser,
fakeValuesService.bothify("????##@gmail.com")));
fakeValuesService.bothify("????##@gmail.com")));
userService.save(fakeUser);
}

/************
* 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");

a1 = authorService.save(a1);
a2 = authorService.save(a2);
a3 = authorService.save(a3);
a4 = authorService.save(a4);
a5 = authorService.save(a5);
a6 = authorService.save(a6);

Section s1 = new Section("Fiction");
Section s2 = new Section("Technology");
Section s3 = new Section("Travel");
Section s4 = new Section("Business");
Section s5 = new Section("Religion");

s1 = sectionService.save(s1);
s2 = sectionService.save(s2);
s3 = sectionService.save(s3);
s4 = sectionService.save(s4);
s5 = sectionService.save(s5);

List<Wrote> wrote = new ArrayList<>();
wrote.add(new Wrote(a6, new Book()));
Book b1 = new Book("Flatterland", "9780738206752", 2001, s1);
b1.setWrotes(wrote);
b1 = bookService.save(b1);

wrote = new ArrayList<>();
wrote.add(new Wrote(a2, new Book()));
Book b2 = new Book("Digital Fortess", "9788489367012", 2007, s1);
b2.setWrotes(wrote);
b2 = bookService.save(b2);

wrote = new ArrayList<>();
wrote.add(new Wrote(a2, new Book()));
Book b3 = new Book("The Da Vinci Code", "9780307474278", 2009, s1);
b3.setWrotes(wrote);
b3 = bookService.save(b3);

wrote = new ArrayList<>();
wrote.add(new Wrote(a5, new Book()));
wrote.add(new Wrote(a3, new Book()));
Book b4 = new Book("Essentials of Finance", "1314241651234", 0, s4);
b4.setWrotes(wrote);
b4 = bookService.save(b4);

wrote = new ArrayList<>();
wrote.add(new Wrote(a4, new Book()));
Book b5 = new Book("Calling Texas Home", "1885171382134", 2000, s3);
b5.setWrotes(wrote);
b5 = bookService.save(b5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,21 @@ public void configure(HttpSecurity http)
"/users/**")
.hasAnyRole("ADMIN")
.antMatchers(HttpMethod.PUT,
"/users/**")
"/users/**")
.hasAnyRole("ADMIN")
.antMatchers("/users/**",
"/useremails/**",
"/oauth/revoke-token",
"/logout")
"/useremails/**",
"/oauth/revoke-token",
"/logout")
.authenticated()
.antMatchers("/roles/**")
.hasAnyRole("ADMIN")
.antMatchers("/books/**")
.authenticated()
.antMatchers("/authors/**")
.authenticated()
.antMatchers("/data/**")
.authenticated()
.and()
.exceptionHandling()
.accessDeniedHandler(new OAuth2AccessDeniedHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Docket api()
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors
.basePackage("com.lambdaschool.usermodel"))
.basePackage("com.lambdaschool.foundation"))
.paths(PathSelectors.regex("/.*"))
.build()
.useDefaultResponseMessages(false) // Allows only my exception responses
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lambdaschool.foundation.controllers;

import com.lambdaschool.foundation.models.Author;
import com.lambdaschool.foundation.services.AuthorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/authors")
public class AuthorController {

@Autowired
private AuthorService authorService;

@ApiOperation(value = "returns all Authors",
response = Author.class,
responseContainer = "List")
@GetMapping(value = "/authors", produces = {"application/json"})
public ResponseEntity<?> findAllAuthors() {
List<Author> authorList = authorService.findAllAuthors();
return new ResponseEntity<>(authorList, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.lambdaschool.foundation.controllers;

import com.lambdaschool.foundation.models.Book;
import com.lambdaschool.foundation.models.ErrorDetail;
import com.lambdaschool.foundation.services.BookService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

@Autowired
private BookService bookService;

@ApiOperation(value = "returns all Books",
response = Book.class,
responseContainer = "List")
@GetMapping(value = "/books", produces = {"application/json"})
public ResponseEntity<?> findAllBooks() {
List<Book> bookList = bookService.findAllBooks();
return new ResponseEntity<>(bookList, HttpStatus.OK);
}

@ApiOperation(value = "Retrieve a book based of off book id",
response = Book.class)
@ApiResponses(value = {@ApiResponse(code = 200,
message = "Book Found",
response = Book.class), @ApiResponse(code = 404,
message = "Book Not Found",
response = ErrorDetail.class)})
@GetMapping(value = "/book/{bookid}", produces = {"application/json"})
public ResponseEntity<?> findBookById(@PathVariable long bookid) {
Book book = bookService.findBookById(bookid);
return new ResponseEntity<>(book, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.lambdaschool.foundation.controllers;

import com.lambdaschool.foundation.models.Book;
import com.lambdaschool.foundation.models.ErrorDetail;
import com.lambdaschool.foundation.services.BookService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/data")
public class DataController {

@Autowired
private BookService bookService;

@ApiOperation(value = "delete a book and the book author combinations (does not delete the author records)",
response = Void.class)
@ApiResponses(value = {@ApiResponse(code = 200,
message = "Book Found",
response = Book.class), @ApiResponse(code = 404,
message = "Book Not Found",
response = ErrorDetail.class)})
@DeleteMapping("/books/{bookid}")
public ResponseEntity<?> deleteBookById(@PathVariable long bookid) {
bookService.delete(bookid);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.lambdaschool.foundation.models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "authors")
public class Author extends Auditable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long authorid;

private String lastname;

private String firstname;

@OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
@JsonIgnoreProperties(value = "author", allowSetters = true)
private List<Wrote> books = new ArrayList<>();

public Author() {
}

public Author(String lastname, String firstname) {
this.lastname = lastname;
this.firstname = firstname;
}

public long getAuthorid() {
return authorid;
}

public void setAuthorid(long authorid) {
this.authorid = authorid;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public List<Wrote> getBooks() {
return books;
}

public void setBooks(List<Wrote> books) {
this.books = books;
}
}
Loading