Skip to content

weird error but everything built, redone version #10

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 1 commit 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
175 changes: 105 additions & 70 deletions foundation/src/main/java/com/lambdaschool/foundation/SeedData.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
package com.lambdaschool.foundation;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
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;

/**
* SeedData puts both known and random data into the database. It implements CommandLineRunner.
* <p>
* CoomandLineRunner: Spring Boot automatically runs the run method once and only once
* after the application context has been loaded.
*/
@Transactional
@Component
public class SeedData
implements CommandLineRunner
{
implements CommandLineRunner {
/**
* Connects the Role Service to this process
*/
@Autowired
RoleService roleService;

/**
* Connects the user service to this process
*/
@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,115 +47,156 @@ 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");

r1 = roleService.save(r1);
r2 = roleService.save(r2);
r3 = roleService.save(r3);

// admin, data, user
ArrayList<UserRoles> admins = new ArrayList<>();
admins.add(new UserRoles(new User(),
r1));
r1));
admins.add(new UserRoles(new User(),
r2));
r2));
admins.add(new UserRoles(new User(),
r3));
r3));
User u1 = new User("admin",
"password",
"[email protected]",
admins);
"password",
"[email protected]",
admins);
u1.getUseremails()
.add(new Useremail(u1,
"[email protected]"));
"[email protected]"));
u1.getUseremails()
.add(new Useremail(u1,
"[email protected]"));

"[email protected]"));
userService.save(u1);

// data, user
ArrayList<UserRoles> datas = new ArrayList<>();
datas.add(new UserRoles(new User(),
r3));
r3));
datas.add(new UserRoles(new User(),
r2));
r2));
User u2 = new User("cinnamon",
"1234567",
"[email protected]",
datas);
"1234567",
"[email protected]",
datas);
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
"[email protected]"));
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
"[email protected]"));
u2.getUseremails()
.add(new Useremail(u2,
"[email protected]"));
"[email protected]"));
userService.save(u2);

// user
ArrayList<UserRoles> users = new ArrayList<>();
users.add(new UserRoles(new User(),
r2));
r2));
User u3 = new User("barnbarn",
"ILuvM4th!",
"[email protected]",
users);
"ILuvM4th!",
"[email protected]",
users);
u3.getUseremails()
.add(new Useremail(u3,
"[email protected]"));
"[email protected]"));
userService.save(u3);

users = new ArrayList<>();
users.add(new UserRoles(new User(),
r2));
r2));
User u4 = new User("puttat",
"password",
"[email protected]",
users);
"password",
"[email protected]",
users);
userService.save(u4);

users = new ArrayList<>();
users.add(new UserRoles(new User(),
r2));
r2));
User u5 = new User("misskitty",
"password",
"[email protected]",
users);
"password",
"[email protected]",
users);
userService.save(u5);

// using JavaFaker create a bunch of regular users
// https://www.baeldung.com/java-faker
// https://www.baeldung.com/regular-expressions-java

FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-US"),
new RandomService());
new RandomService());
Faker nameFaker = new Faker(new Locale("en-US"));

for (int i = 0; i < 25; i++)
{
for (int i = 0; i < 25; i++) {
new User();
User fakeUser;

users = new ArrayList<>();
users.add(new UserRoles(new User(),
r2));
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 @@ -22,13 +22,11 @@ public class AuthorizationServerConfig
/**
* Client Id is the user name for the client application. It is read from the environment variable OAUTHCLIENTID
*/
static final String CLIENT_ID = System.getenv("OAUTHCLIENTID");

static final String CLIENT_ID = "lambda-client"; //System.getenv("OAUTHCLIENTID");
/**
* Client secret is the password for the client application. It is read from the environment variable OAUTHCLIENTSECRET
*/
static final String CLIENT_SECRET = System.getenv("OAUTHCLIENTSECRET"); // read from environment variable

static final String CLIENT_SECRET = "lambda-secret"; //System.getenv("OAUTHCLIENTSECRET");
/**
* We are using username and password to authenticate a user
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lambdaschool.foundation.controllers;


import com.lambdaschool.foundation.models.Author;
import com.lambdaschool.foundation.services.AuthorService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthorController
{
@Autowired
private AuthorService authorService;


// GET /authors/authors - returns a JSON object list of all the authors, their books, and the book's section.
@GetMapping(value = "/authors/authors",
produces = {"application/json"})
public ResponseEntity<?> listAllAuthors(@PageableDefault(page = 0, size = 3) Pageable pageable)
{
return new ResponseEntity<>(authorService.findAll(pageable), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("books")
public class BookController
{
@Autowired
private BookService bookService;

@ApiOperation(value = "list all books",
response = Book.class,
responseContainer = "List")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", dataType = "integer", paramType = "query",
value = "Results page you want to retrieve (1..N)"),
@ApiImplicitParam(name = "size", dataType = "integer", paramType = "query",
value = "Number of records per page."),
@ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
value = "Sorting criteria in the format: property(,asc|desc). " +
"Default sort order is ascending. " +
"Multiple sort criteria are supported.")})
// GET /books/books - returns a JSON object list of all the books, their sections, and their authors.
@PreAuthorize("hasAnyRole('ADMIN')")
@GetMapping(value = "/books", produces = {"application/json"})
public ResponseEntity<?> listAllBooks()
{
List<Book> myBooks = bookService.findAll();
return new ResponseEntity<>(myBooks, HttpStatus.OK);
}




@ApiOperation(value = "delete a book using book id")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Book Deleted"), @ApiResponse(code = 404, message = "Book Not Found",
response = ErrorDetail.class)})
// DELETE /data/books/{id} - deletes a book and the book author combinations - but does not delete the author records.
@DeleteMapping(value = "/data/books/{id}")
public ResponseEntity<?> deleteBook(@PathVariable long id)
{
bookService.delete(id);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Loading