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
Binary file modified .DS_Store
Binary file not shown.
Empty file added .idea/.gitignore
Empty file.
9 changes: 9 additions & 0 deletions .idea/java-testingusermodel.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion usermodel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<description>Demo project for Spring Boot</description>

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

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.lambdaschool.usermodel.models.User;
import com.lambdaschool.usermodel.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -87,15 +88,15 @@ public ResponseEntity<?> getUserByName(
*
* @param userName Substring of the username for which you seek
* @return A JSON list of users you seek
* @see UserService#findByNameContaining(String) UserService.findByNameContaining(String)
* @see UserService#findByNameContaining(String, Pageable) UserService.findByNameContaining(String)
*/
@GetMapping(value = "/user/name/like/{userName}",
produces = {"application/json"})
public ResponseEntity<?> getUserLikeName(
@PathVariable
String userName)
String userName, Pageable pageable)
{
List<User> u = userService.findByNameContaining(userName);
List<User> u = userService.findByNameContaining(userName, pageable);
return new ResponseEntity<>(u,
HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.lambdaschool.usermodel.models.User;
import com.lambdaschool.usermodel.views.JustTheCount;
import com.lambdaschool.usermodel.views.UserNameCountEmails;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
Expand All @@ -29,7 +30,7 @@ public interface UserRepository extends CrudRepository<User, Long>
* @param name the substring of the names (String) you seek
* @return List of users whose name contain the given substring ignoring case
*/
List<User> findByUsernameContainingIgnoreCase(String name);
List<User> findByUsernameContainingIgnoreCase(String name, Pageable pageable);

/*
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.lambdaschool.usermodel.models.User;
import com.lambdaschool.usermodel.views.UserNameCountEmails;
import org.springframework.data.domain.Pageable;

import java.util.List;

Expand All @@ -27,7 +28,7 @@ public interface UserService
* @param username The substring (String) of the username of the Users you seek
* @return List of users whose username contains the given substring
*/
List<User> findByNameContaining(String username);
List<User> findByNameContaining(String username, Pageable pageable);

/**
* Returns the user with the given primary key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.lambdaschool.usermodel.repository.UserRepository;
import com.lambdaschool.usermodel.views.UserNameCountEmails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -47,9 +48,9 @@ public User findUserById(long id) throws EntityNotFoundException
}

@Override
public List<User> findByNameContaining(String username)
public List<User> findByNameContaining(String username, Pageable pageable)
{
return userrepos.findByUsernameContainingIgnoreCase(username.toLowerCase());
return userrepos.findByUsernameContainingIgnoreCase(username.toLowerCase(), pageable);
}

@Override
Expand Down
143 changes: 143 additions & 0 deletions usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package com.lambdaschool.usermodel;

import com.github.javafaker.Faker;
import com.github.javafaker.service.FakeValuesService;
import com.github.javafaker.service.RandomService;
import com.lambdaschool.usermodel.models.Role;
import com.lambdaschool.usermodel.models.User;
import com.lambdaschool.usermodel.models.UserRoles;
import com.lambdaschool.usermodel.models.Useremail;
import com.lambdaschool.usermodel.services.RoleService;
import com.lambdaschool.usermodel.services.UserService;
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.Locale;

@Transactional
@Component
public class SeedData implements CommandLineRunner
{
@Autowired
RoleService roleService;

@Autowired
UserService userService;

@Transactional
@Override
public void run(String[] args) throws Exception
{
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));
admins.add(new UserRoles(new User(),
r2));
admins.add(new UserRoles(new User(),
r3));
User u1 = new User("test admin",
"password",
"admin@lambdaschool.local",
admins);
u1.getUseremails()
.add(new Useremail(u1,
"admin@email.local"));
u1.getUseremails()
.add(new Useremail(u1,
"admin@mymail.local"));

userService.save(u1);

// data, user
ArrayList<UserRoles> datas = new ArrayList<>();
datas.add(new UserRoles(new User(),
r3));
datas.add(new UserRoles(new User(),
r2));
User u2 = new User("test cinnamon",
"1234567",
"cinnamon@lambdaschool.local",
datas);
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
ArrayList<UserRoles> users = new ArrayList<>();
users.add(new UserRoles(new User(),
r2));
User u3 = new User("test barnbarn",
"ILuvM4th!",
"barnbarn@lambdaschool.local",
users);
u3.getUseremails()
.add(new Useremail(u3,
"barnbarn@email.local"));
userService.save(u3);

users = new ArrayList<>();
users.add(new UserRoles(new User(),
r2));
User u4 = new User("test puttat",
"password",
"puttat@school.lambda",
users);
userService.save(u4);

users = new ArrayList<>();
users.add(new UserRoles(new User(),
r2));
User u5 = new User("test misskitty",
"password",
"misskitty@school.lambda",
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());
Faker nameFaker = new Faker(new Locale("en-US"));

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

users = new ArrayList<>();
users.add(new UserRoles(new User(),
r2));
fakeUser = new User("test " + nameFaker.name()
.username(),
"password",
nameFaker.internet()
.emailAddress(),
users);
fakeUser.getUseremails()
.add(new Useremail(fakeUser,
fakeValuesService.bothify("????##@gmail.com")));
userService.save(fakeUser);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.lambdaschool.usermodel;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

/**
* Main class to start the application.
*/
// @EnableJpaAuditing
@SpringBootApplication
public class UserModelApplication
{

/**
* Main method to start the application.
*
* @param args Not used in this application.
*/
public static void main(String[] args)
{
SpringApplication.run(UserModelApplication.class,
args);
}
}
Loading