|
|
|
@ -32,150 +32,153 @@ import java.util.stream.Collectors;
|
|
|
|
|
@RequestMapping("api/users")
|
|
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
|
|
private UserService userService;
|
|
|
|
|
private PasswordEncryptionService passwordService;
|
|
|
|
|
private final ConfigProperties configProperties;
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
|
|
|
|
|
private UserService userService;
|
|
|
|
|
private PasswordEncryptionService passwordService;
|
|
|
|
|
private final ConfigProperties configProperties;
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
public UserController(ConfigProperties configProperties, UserService userService,
|
|
|
|
|
PasswordEncryptionService passwordService) {
|
|
|
|
|
this.configProperties = configProperties;
|
|
|
|
|
System.out.println("UserController.UserController: cross origin: " + configProperties.getOrigin());
|
|
|
|
|
// Logging in the constructor
|
|
|
|
|
logger.info("UserController initialized: " + configProperties.getOrigin());
|
|
|
|
|
logger.debug("UserController.UserController: Cross Origin Config: {}", configProperties.getOrigin());
|
|
|
|
|
this.userService = userService;
|
|
|
|
|
this.passwordService = passwordService;
|
|
|
|
|
}
|
|
|
|
|
@Autowired
|
|
|
|
|
public UserController(ConfigProperties configProperties, UserService userService,
|
|
|
|
|
PasswordEncryptionService passwordService) {
|
|
|
|
|
this.configProperties = configProperties;
|
|
|
|
|
System.out.println("UserController.UserController: cross origin: " + configProperties.getOrigin());
|
|
|
|
|
// Logging in the constructor
|
|
|
|
|
logger.info("UserController initialized: " + configProperties.getOrigin());
|
|
|
|
|
logger.debug("UserController.UserController: Cross Origin Config: {}", configProperties.getOrigin());
|
|
|
|
|
this.userService = userService;
|
|
|
|
|
this.passwordService = passwordService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build create User REST API
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@PostMapping
|
|
|
|
|
public ResponseEntity<String> createUser(@Valid @RequestBody RegisterUser registerUser, BindingResult bindingResult) {
|
|
|
|
|
//captcha
|
|
|
|
|
//todo ergänzen
|
|
|
|
|
// build create User REST API
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@PostMapping
|
|
|
|
|
public ResponseEntity<String> createUser(@Valid @RequestBody RegisterUser registerUser, BindingResult bindingResult) {
|
|
|
|
|
//captcha
|
|
|
|
|
//todo ergänzen
|
|
|
|
|
|
|
|
|
|
System.out.println("UserController.createUser: captcha passed.");
|
|
|
|
|
System.out.println("UserController.createUser: captcha passed.");
|
|
|
|
|
|
|
|
|
|
//input validation
|
|
|
|
|
if (bindingResult.hasErrors()) {
|
|
|
|
|
List<String> errors = bindingResult.getFieldErrors().stream()
|
|
|
|
|
.map(fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage())
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
System.out.println("UserController.createUser " + errors);
|
|
|
|
|
//input validation
|
|
|
|
|
if (bindingResult.hasErrors()) {
|
|
|
|
|
List<String> errors = bindingResult.getFieldErrors().stream()
|
|
|
|
|
.map(fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage())
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
System.out.println("UserController.createUser " + errors);
|
|
|
|
|
|
|
|
|
|
JsonArray arr = new JsonArray();
|
|
|
|
|
errors.forEach(arr::add);
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.add("message", arr);
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
JsonArray arr = new JsonArray();
|
|
|
|
|
errors.forEach(arr::add);
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.add("message", arr);
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
|
|
|
|
|
System.out.println("UserController.createUser, validation fails: " + json);
|
|
|
|
|
return ResponseEntity.badRequest().body(json);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("UserController.createUser: input validation passed");
|
|
|
|
|
System.out.println("UserController.createUser, validation fails: " + json);
|
|
|
|
|
return ResponseEntity.badRequest().body(json);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("UserController.createUser: input validation passed");
|
|
|
|
|
|
|
|
|
|
//password validation
|
|
|
|
|
//todo ergänzen
|
|
|
|
|
System.out.println("UserController.createUser, password validation passed");
|
|
|
|
|
//password validation
|
|
|
|
|
//todo ergänzen
|
|
|
|
|
System.out.println("UserController.createUser, password validation passed");
|
|
|
|
|
|
|
|
|
|
//transform registerUser to user
|
|
|
|
|
User user = new User(
|
|
|
|
|
null,
|
|
|
|
|
registerUser.getFirstName(),
|
|
|
|
|
registerUser.getLastName(),
|
|
|
|
|
registerUser.getEmail(),
|
|
|
|
|
passwordService.hashPassword(registerUser.getPassword())
|
|
|
|
|
);
|
|
|
|
|
//transform registerUser to user
|
|
|
|
|
String salt = PasswordEncryptionService.generateSalt();
|
|
|
|
|
|
|
|
|
|
User savedUser = userService.createUser(user);
|
|
|
|
|
System.out.println("UserController.createUser, user saved in db");
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.addProperty("answer", "User Saved");
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
System.out.println("UserController.createUser " + json);
|
|
|
|
|
return ResponseEntity.accepted().body(json);
|
|
|
|
|
}
|
|
|
|
|
User user = new User(
|
|
|
|
|
null,
|
|
|
|
|
registerUser.getFirstName(),
|
|
|
|
|
registerUser.getLastName(),
|
|
|
|
|
registerUser.getEmail(),
|
|
|
|
|
passwordService.hashPassword(registerUser.getPassword(), salt),
|
|
|
|
|
salt
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// build get user by id REST API
|
|
|
|
|
// http://localhost:8080/api/users/1
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@GetMapping("{id}")
|
|
|
|
|
public ResponseEntity<User> getUserById(@PathVariable("id") Long userId) {
|
|
|
|
|
User user = userService.getUserById(userId);
|
|
|
|
|
return new ResponseEntity<>(user, HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
User savedUser = userService.createUser(user);
|
|
|
|
|
System.out.println("UserController.createUser, user saved in db");
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.addProperty("answer", "User Saved");
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
System.out.println("UserController.createUser " + json);
|
|
|
|
|
return ResponseEntity.accepted().body(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build Get All Users REST API
|
|
|
|
|
// http://localhost:8080/api/users
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@GetMapping
|
|
|
|
|
public ResponseEntity<List<User>> getAllUsers() {
|
|
|
|
|
List<User> users = userService.getAllUsers();
|
|
|
|
|
return new ResponseEntity<>(users, HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
// build get user by id REST API
|
|
|
|
|
// http://localhost:8080/api/users/1
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@GetMapping("{id}")
|
|
|
|
|
public ResponseEntity<User> getUserById(@PathVariable("id") Long userId) {
|
|
|
|
|
User user = userService.getUserById(userId);
|
|
|
|
|
return new ResponseEntity<>(user, HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build Update User REST API
|
|
|
|
|
// http://localhost:8080/api/users/1
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@PutMapping("{id}")
|
|
|
|
|
public ResponseEntity<User> updateUser(@PathVariable("id") Long userId,
|
|
|
|
|
@RequestBody User user) {
|
|
|
|
|
user.setId(userId);
|
|
|
|
|
User updatedUser = userService.updateUser(user);
|
|
|
|
|
return new ResponseEntity<>(updatedUser, HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
// Build Get All Users REST API
|
|
|
|
|
// http://localhost:8080/api/users
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@GetMapping
|
|
|
|
|
public ResponseEntity<List<User>> getAllUsers() {
|
|
|
|
|
List<User> users = userService.getAllUsers();
|
|
|
|
|
return new ResponseEntity<>(users, HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build Delete User REST API
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@DeleteMapping("{id}")
|
|
|
|
|
public ResponseEntity<String> deleteUser(@PathVariable("id") Long userId) {
|
|
|
|
|
userService.deleteUser(userId);
|
|
|
|
|
return new ResponseEntity<>("User successfully deleted!", HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
// Build Update User REST API
|
|
|
|
|
// http://localhost:8080/api/users/1
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@PutMapping("{id}")
|
|
|
|
|
public ResponseEntity<User> updateUser(@PathVariable("id") Long userId,
|
|
|
|
|
@RequestBody User user) {
|
|
|
|
|
user.setId(userId);
|
|
|
|
|
User updatedUser = userService.updateUser(user);
|
|
|
|
|
return new ResponseEntity<>(updatedUser, HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build Delete User REST API
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@DeleteMapping("{id}")
|
|
|
|
|
public ResponseEntity<String> deleteUser(@PathVariable("id") Long userId) {
|
|
|
|
|
userService.deleteUser(userId);
|
|
|
|
|
return new ResponseEntity<>("User successfully deleted!", HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get user id by email
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@PostMapping("/byemail")
|
|
|
|
|
public ResponseEntity<String> getUserIdByEmail(@RequestBody EmailAdress email, BindingResult bindingResult) {
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail: " + email);
|
|
|
|
|
//input validation
|
|
|
|
|
if (bindingResult.hasErrors()) {
|
|
|
|
|
List<String> errors = bindingResult.getFieldErrors().stream()
|
|
|
|
|
.map(fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage())
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
System.out.println("UserController.createUser " + errors);
|
|
|
|
|
// get user id by email
|
|
|
|
|
@CrossOrigin(origins = "${CROSS_ORIGIN}")
|
|
|
|
|
@PostMapping("/byemail")
|
|
|
|
|
public ResponseEntity<String> getUserIdByEmail(@RequestBody EmailAdress email, BindingResult bindingResult) {
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail: " + email);
|
|
|
|
|
//input validation
|
|
|
|
|
if (bindingResult.hasErrors()) {
|
|
|
|
|
List<String> errors = bindingResult.getFieldErrors().stream()
|
|
|
|
|
.map(fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage())
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
System.out.println("UserController.createUser " + errors);
|
|
|
|
|
|
|
|
|
|
JsonArray arr = new JsonArray();
|
|
|
|
|
errors.forEach(arr::add);
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.add("message", arr);
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
JsonArray arr = new JsonArray();
|
|
|
|
|
errors.forEach(arr::add);
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.add("message", arr);
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
|
|
|
|
|
System.out.println("UserController.createUser, validation fails: " + json);
|
|
|
|
|
return ResponseEntity.badRequest().body(json);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("UserController.createUser, validation fails: " + json);
|
|
|
|
|
return ResponseEntity.badRequest().body(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail: input validation passed");
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail: input validation passed");
|
|
|
|
|
|
|
|
|
|
User user = userService.findByEmail(email.getEmail());
|
|
|
|
|
if (user == null) {
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail, no user found with email: " + email);
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.addProperty("message", "No user found with this email");
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
User user = userService.findByEmail(email.getEmail());
|
|
|
|
|
if (user == null) {
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail, no user found with email: " + email);
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.addProperty("message", "No user found with this email");
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail, fails: " + json);
|
|
|
|
|
return ResponseEntity.badRequest().body(json);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail, user find by email");
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.addProperty("answer", user.getId());
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail " + json);
|
|
|
|
|
return ResponseEntity.accepted().body(json);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail, fails: " + json);
|
|
|
|
|
return ResponseEntity.badRequest().body(json);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail, user find by email");
|
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
obj.addProperty("answer", user.getId());
|
|
|
|
|
String json = new Gson().toJson(obj);
|
|
|
|
|
System.out.println("UserController.getUserIdByEmail " + json);
|
|
|
|
|
return ResponseEntity.accepted().body(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|