UserController refactor (#1450)

* Refactor UserController

* Refactor UserController
This commit is contained in:
Grzegorz Piwowarek 2017-03-19 22:31:11 +01:00 committed by GitHub
parent 0b78cc9e4c
commit 3accbf8815

View File

@ -1,48 +1,44 @@
package com.baeldung.springmvcforms.controller; package com.baeldung.springmvcforms.controller;
import java.util.ArrayList; import com.baeldung.springmvcforms.domain.User;
import java.util.Collections; import org.springframework.context.support.DefaultMessageSourceResolvable;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import com.baeldung.springmvcforms.domain.User; import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Controller @Controller
public class UserController { public class UserController {
List<User> users = new ArrayList<User>() { private List<User> users = Arrays.asList(
{ new User("ana@yahoo.com", "pass", "Ana", 20),
add(new User("ana@yahoo.com", "pass", "Ana", 20)); new User("bob@yahoo.com", "pass", "Bob", 30),
add(new User("bob@yahoo.com", "pass", "Bob", 30)); new User("john@yahoo.com", "pass", "John", 40),
add(new User("john@yahoo.com", "pass", "John", 40)); new User("mary@yahoo.com", "pass", "Mary", 30));
add(new User("mary@yahoo.com", "pass", "Mary", 30));
}
};
@GetMapping("/userPage") @GetMapping("/userPage")
public String getUserProfilePage() { public String getUserProfilePage() {
return "user"; return "user";
} }
@PostMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE) @PostMapping("/user")
@ResponseBody @ResponseBody
public ResponseEntity<Object> saveUser(@Valid User user, BindingResult result, Model model) { public ResponseEntity<Object> saveUser(@Valid User user, BindingResult result, Model model) {
if (result.hasErrors()) { if (result.hasErrors()) {
List<String> errors = new ArrayList<>(); final List<String> errors = result.getAllErrors().stream()
result.getAllErrors().forEach(item->{ .map(DefaultMessageSourceResolvable::getDefaultMessage)
errors.add(item.getDefaultMessage()); .collect(Collectors.toList());
});
return new ResponseEntity<>(errors, HttpStatus.OK); return new ResponseEntity<>(errors, HttpStatus.OK);
} else { } else {
if (users.stream().anyMatch(it -> user.getEmail().equals(it.getEmail()))) { if (users.stream().anyMatch(it -> user.getEmail().equals(it.getEmail()))) {
@ -54,7 +50,7 @@ public class UserController {
} }
} }
@GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping("/users")
@ResponseBody @ResponseBody
public List<User> getUsers() { public List<User> getUsers() {
return users; return users;