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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
@EnableJpaAuditing
@SpringBootApplication
@PropertySource(value = "file:/Users/lambdajohn/foundationconf.properties", ignoreResourceNotFound = true)
//@PropertySource(value = "file:/Users/lambdajohn/foundationconf.properties", ignoreResourceNotFound = true)
public class FoundationApplication
{
/**
Expand Down
117 changes: 27 additions & 90 deletions foundation/src/main/java/com/lambdaschool/foundation/SeedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
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.models.*;
import com.lambdaschool.foundation.services.ProductService;
import com.lambdaschool.foundation.services.RoleService;
import com.lambdaschool.foundation.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -45,6 +43,9 @@ public class SeedData
@Autowired
UserService userService;

@Autowired
ProductService productService;

/**
* Generates test, seed data for our application
* First a set of known data is seeded into our database.
Expand All @@ -61,113 +62,49 @@ public void run(String[] args) throws
{
userService.deleteAll();
roleService.deleteAll();
Role r1 = new Role("admin");
Role r2 = new Role("user");
Role r3 = new Role("data");
Role r1 = new Role("owner");
Role r2 = new Role("renter");

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




// admin, data, user
User u1 = new User("admin",
"password",
"admin@lambdaschool.local");
User u1 = new User("Johnnie","John","Malango",
"admin@lambdaschool.local","password");
u1.getRoles()
.add(new UserRoles(u1,
r1));
u1.getRoles()
.add(new UserRoles(u1,
r2));
u1.getRoles()
.add(new UserRoles(u1,
r3));
u1.getUseremails()
.add(new Useremail(u1,
"admin@email.local"));
u1.getUseremails()
.add(new Useremail(u1,
"admin@mymail.local"));

userService.save(u1);

u1 = userService.save(u1);

// data, user
User u2 = new User("cinnamon",
"1234567",
"cinnamon@lambdaschool.local");
User u2 = new User("Marley","Marley","Copper",
"cinnamon@lambdaschool.local", "1234567"
);
u2.getRoles()
.add(new UserRoles(u2,
r2));
u2.getRoles()
.add(new UserRoles(u2,
r3));
u2.getUseremails()
.add(new Useremail(u2,
"cinnamon@mymail.local"));
u2.getUseremails()
.add(new Useremail(u2,
"hops@mymail.local"));
u2.getUseremails()
.add(new Useremail(u2,
"bunny@email.local"));
userService.save(u2);

// user
User u3 = new User("barnbarn",
"ILuvM4th!",
"barnbarn@lambdaschool.local");
u3.getRoles()
.add(new UserRoles(u3,
r2));
u3.getUseremails()
.add(new Useremail(u3,
"barnbarn@email.local"));
userService.save(u3);

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

User u5 = new User("misskitty",
"password",
"misskitty@school.lambda");
u5.getRoles()
.add(new UserRoles(u5,
r2));
userService.save(u5);
u2 = userService.save(u2);

Product p1 = new Product("hp laptop");
Product p2 = new Product ("digital camera");
p1.getUsers().add(new UserProduct(u1,p1));
p1 = productService.save(p1);
p2.getUsers().add(new UserProduct(u2,p2));
p2 = productService.save(p2);

if (false)
{
// using JavaFaker create a bunch of regular users
// 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());
Faker nameFaker = new Faker(new Locale("en-US"));

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

fakeUser = new User(nameFaker.name()
.username(),
"password",
nameFaker.internet()
.emailAddress());
fakeUser.getRoles()
.add(new UserRoles(fakeUser,
r2));
fakeUser.getUseremails()
.add(new Useremail(fakeUser,
fakeValuesService.bothify("????##@gmail.com")));
userService.save(fakeUser);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,23 @@ public void configure(HttpSecurity http)
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**",
"/createnewuser")
"/createnewuser",
"/users/login/"
)
.permitAll()
.antMatchers(HttpMethod.POST,
"/users/**")
.hasAnyRole("ADMIN")
.antMatchers(HttpMethod.DELETE,
"/users/**")
.hasAnyRole("ADMIN")
.antMatchers(HttpMethod.PUT,
"/users/**")
.hasAnyRole("ADMIN")
.antMatchers("/users/**",
.antMatchers(
"/useremails/**",
"/oauth/revoke-token",
"/logout")
.authenticated()
.antMatchers("/roles/**")
.hasAnyRole("ADMIN")
.anyRequest()
.denyAll()
.and()
.exceptionHandling()
.accessDeniedHandler(new OAuth2AccessDeniedHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class OpenController
* @return The token access and other relevent data to token access. Status of CREATED. The location header to look up the new user.
* @throws URISyntaxException we create some URIs during this method. If anything goes wrong with that creation, an exception is thrown.
*/
@PostMapping(value = "/createnewuser",
@PostMapping(value = "/users/register",
consumes = {"application/json"},
produces = {"application/json"})
public ResponseEntity<?> addSelf(
Expand All @@ -66,15 +66,16 @@ public ResponseEntity<?> addSelf(
{
// Create the user
User newuser = new User();

newuser.setUsername(newminuser.getUsername());
newuser.setFirstname(newminuser.getFirstname());
newuser.setLastname((newminuser.getLastname()));
newuser.setEmail(newminuser.getEmail());
newuser.setPassword(newminuser.getPassword());
newuser.setPrimaryemail(newminuser.getPrimaryemail());


// add the default role of user
Set<UserRoles> newRoles = new HashSet<>();
newRoles.add(new UserRoles(newuser,
roleService.findByName("user")));
roleService.findByName("renter")));
newuser.setRoles(newRoles);

newuser = userService.save(newuser);
Expand Down Expand Up @@ -107,7 +108,7 @@ public ResponseEntity<?> addSelf(
map.add("scope",
"read write trust");
map.add("username",
newminuser.getUsername());
newminuser.getEmail());
map.add("password",
newminuser.getPassword());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.lambdaschool.foundation.controllers;

import com.lambdaschool.foundation.models.Product;
import com.lambdaschool.foundation.models.User;
import com.lambdaschool.foundation.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService productService;

@GetMapping(value = "/products",
produces = "application/json")
public ResponseEntity<?> listAllProducts() {
List<Product> myProducts= productService.findAllProducts();
return new ResponseEntity<>(myProducts,
HttpStatus.OK);
}
@GetMapping(value = "/product/{productId}",
produces = "application/json")
public ResponseEntity<?> getProductById(
@PathVariable
Long productId) {
Product p = productService.findProductById(productId);
return new ResponseEntity<>(p,
HttpStatus.OK);



}
@PutMapping(value = "/product/{productid}",
consumes = "application/json")
public ResponseEntity<?> updateProduct(
@Valid
@RequestBody
Product updateProduct,
@PathVariable
long productid)
{
updateProduct.setProductid(productid);
productService.save(updateProduct);

return new ResponseEntity<>(HttpStatus.OK);
}

@DeleteMapping(value = "/product/{id}")
public ResponseEntity<?> deleteProductById(
@PathVariable
long id)
{
productService.delete(id);
return new ResponseEntity<>(HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class UserController
* @return JSON list of all users with a status of OK
* @see UserService#findAll() UserService.findAll()
*/
@PreAuthorize("hasAnyRole('ADMIN')")
// @PreAuthorize("hasAnyRole('owner','renter')")
@GetMapping(value = "/users",
produces = "application/json")
public ResponseEntity<?> listAllUsers()
Expand Down Expand Up @@ -116,7 +116,7 @@ public ResponseEntity<?> getUserLikeName(
* @throws URISyntaxException Exception if something does not work in creating the location header
* @see UserService#save(User) UserService.save(User)
*/
@PostMapping(value = "/user",
@PostMapping(value = "/user/register",
consumes = "application/json")
public ResponseEntity<?> addNewUser(
@Valid
Expand Down
Loading