Refactor some code in Java8 and transform properties file to yaml file.

This commit is contained in:
2019-07-29 22:10:45 +02:00
parent 54e15ffa59
commit 126126b28e
15 changed files with 304 additions and 202 deletions
@@ -29,11 +29,21 @@ import com.fasterxml.jackson.annotation.JsonView;
@RestController
@RequestMapping("/api/account")
public class AccountController {
@Autowired
/** Account service. */
private AccountService accountService;
@Autowired
/** User service. */
private UserService userService;
/**
* Constructor.
* @param accountService Account service.
* @param userService User service.
*/
public AccountController(AccountService accountService, UserService userService) {
this.accountService = accountService;
this.userService = userService;
}
@JsonView(View.UserDTO.class)
@PostMapping("/login")
public User login(@RequestBody final User pUser) throws BadCredentialsException {
@@ -54,8 +64,6 @@ public class AccountController {
* @param pPasswordWrapper
* The object which contains the old password for verification and
* the new password to set to the user.
* @param pRequest
* The request injected by Spring.
* @param pResponse
* The reponse injected by Spring.
* @throws IOException
@@ -64,15 +72,19 @@ public class AccountController {
*/
@PutMapping("/changePassword")
public void changePassword(@RequestBody final PasswordWrapperDTO pPasswordWrapper,
final HttpServletRequest pRequest,
final HttpServletResponse pResponse,
final Principal pPrincipal) throws IOException {
final Optional<User> connectedUser = userService.getUserByPrincipal(pPrincipal);
if(connectedUser.isPresent()) {
accountService.changePassword(connectedUser.get(), pPasswordWrapper, pResponse);
} else {
pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
final Principal pPrincipal) {
int httpResponseCode = userService.getUserByPrincipal(pPrincipal)
.map(user -> {
try {
return accountService.changePassword(user, pPasswordWrapper);
} catch (IllegalArgumentException e) {
return HttpServletResponse.SC_BAD_REQUEST;
}
})
.orElse(HttpServletResponse.SC_UNAUTHORIZED);
pResponse.setStatus(httpResponseCode);
}
@PostMapping("/signin")