Merge pull request #10260 from hdlopez/patch-1

Fixed Spring Boot CRUD Application example
This commit is contained in:
Loredana Crusoveanu 2020-12-26 09:18:03 +02:00 committed by GitHub
commit f7c4f13f8d
2 changed files with 16 additions and 5 deletions

View File

@ -23,6 +23,12 @@ public class UserController {
this.userRepository = userRepository; this.userRepository = userRepository;
} }
@GetMapping("/index")
public String showUserList(Model model) {
model.addAttribute("users", userRepository.findAll());
return "index";
}
@GetMapping("/signup") @GetMapping("/signup")
public String showSignUpForm(User user) { public String showSignUpForm(User user) {
return "add-user"; return "add-user";
@ -35,7 +41,6 @@ public class UserController {
} }
userRepository.save(user); userRepository.save(user);
model.addAttribute("users", userRepository.findAll());
return "redirect:/index"; return "redirect:/index";
} }
@ -43,6 +48,7 @@ public class UserController {
public String showUpdateForm(@PathVariable("id") long id, Model model) { public String showUpdateForm(@PathVariable("id") long id, Model model) {
User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("user", user); model.addAttribute("user", user);
return "update-user"; return "update-user";
} }
@ -54,7 +60,7 @@ public class UserController {
} }
userRepository.save(user); userRepository.save(user);
model.addAttribute("users", userRepository.findAll());
return "redirect:/index"; return "redirect:/index";
} }
@ -62,7 +68,7 @@ public class UserController {
public String deleteUser(@PathVariable("id") long id, Model model) { public String deleteUser(@PathVariable("id") long id, Model model) {
User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
userRepository.delete(user); userRepository.delete(user);
model.addAttribute("users", userRepository.findAll());
return "index"; return "redirect:/index";
} }
} }

View File

@ -28,6 +28,11 @@ public class UserControllerUnitTest {
userController = new UserController(mockedUserRepository); userController = new UserController(mockedUserRepository);
} }
@Test
public void whenCalledIndex_thenCorrect() {
assertThat(userController.showUserList(mockedModel)).isEqualTo("index");
}
@Test @Test
public void whenCalledshowSignUpForm_thenCorrect() { public void whenCalledshowSignUpForm_thenCorrect() {
User user = new User("John", "john@domain.com"); User user = new User("John", "john@domain.com");
@ -78,6 +83,6 @@ public class UserControllerUnitTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void whenCalleddeleteUser_thenIllegalArgumentException() { public void whenCalleddeleteUser_thenIllegalArgumentException() {
assertThat(userController.deleteUser(1l, mockedModel)).isEqualTo("index"); assertThat(userController.deleteUser(1l, mockedModel)).isEqualTo("redirect:/index");
} }
} }