UserController refactor (#1450)
* Refactor UserController * Refactor UserController
This commit is contained in:
parent
0b78cc9e4c
commit
3accbf8815
|
@ -1,48 +1,44 @@
|
|||
package com.baeldung.springmvcforms.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import com.baeldung.springmvcforms.domain.User;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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
|
||||
public class UserController {
|
||||
|
||||
List<User> users = new ArrayList<User>() {
|
||||
{
|
||||
add(new User("ana@yahoo.com", "pass", "Ana", 20));
|
||||
add(new User("bob@yahoo.com", "pass", "Bob", 30));
|
||||
add(new User("john@yahoo.com", "pass", "John", 40));
|
||||
add(new User("mary@yahoo.com", "pass", "Mary", 30));
|
||||
}
|
||||
};
|
||||
private List<User> users = Arrays.asList(
|
||||
new User("ana@yahoo.com", "pass", "Ana", 20),
|
||||
new User("bob@yahoo.com", "pass", "Bob", 30),
|
||||
new User("john@yahoo.com", "pass", "John", 40),
|
||||
new User("mary@yahoo.com", "pass", "Mary", 30));
|
||||
|
||||
@GetMapping("/userPage")
|
||||
public String getUserProfilePage() {
|
||||
return "user";
|
||||
}
|
||||
|
||||
@PostMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
@PostMapping("/user")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> saveUser(@Valid User user, BindingResult result, Model model) {
|
||||
if (result.hasErrors()) {
|
||||
List<String> errors = new ArrayList<>();
|
||||
result.getAllErrors().forEach(item->{
|
||||
errors.add(item.getDefaultMessage());
|
||||
});
|
||||
final List<String> errors = result.getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return new ResponseEntity<>(errors, HttpStatus.OK);
|
||||
} else {
|
||||
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
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
|
|
Loading…
Reference in New Issue