spring-boot-realworld-examp.../src/main/java/io/spring/api/CurrentUserApi.java

111 lines
4.3 KiB
Java
Raw Normal View History

2017-08-08 20:14:14 +07:00
package io.spring.api;
2017-08-14 13:27:36 +07:00
import com.fasterxml.jackson.annotation.JsonRootName;
2017-08-18 11:09:07 +07:00
import io.spring.api.exception.InvalidRequestException;
2017-08-18 16:08:27 +07:00
import io.spring.application.UserQueryService;
2017-08-25 10:34:41 +07:00
import io.spring.application.data.UserWithToken;
2017-08-18 16:08:27 +07:00
import io.spring.application.data.UserData;
2017-08-08 20:14:14 +07:00
import io.spring.core.user.User;
2017-08-14 13:27:36 +07:00
import io.spring.core.user.UserRepository;
import lombok.Getter;
import lombok.NoArgsConstructor;
2018-05-10 13:50:44 +07:00
import javax.validation.constraints.Email;
2017-08-08 20:14:14 +07:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
2017-08-14 13:27:36 +07:00
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
2017-08-08 20:14:14 +07:00
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
2017-08-14 13:27:36 +07:00
import javax.validation.Valid;
2017-08-15 16:52:23 +07:00
import java.util.HashMap;
import java.util.Map;
2017-08-18 11:09:07 +07:00
import java.util.Optional;
2017-08-14 13:27:36 +07:00
2017-08-08 20:14:14 +07:00
@RestController
2017-08-14 13:27:36 +07:00
@RequestMapping(path = "/user")
2017-08-08 20:14:14 +07:00
public class CurrentUserApi {
private UserQueryService userQueryService;
2017-08-14 13:27:36 +07:00
private UserRepository userRepository;
2017-08-08 20:14:14 +07:00
@Autowired
2017-08-14 13:27:36 +07:00
public CurrentUserApi(UserQueryService userQueryService, UserRepository userRepository) {
2017-08-08 20:14:14 +07:00
this.userQueryService = userQueryService;
2017-08-14 13:27:36 +07:00
this.userRepository = userRepository;
2017-08-08 20:14:14 +07:00
}
2017-08-14 13:27:36 +07:00
@GetMapping
2017-08-08 20:14:14 +07:00
public ResponseEntity currentUser(@AuthenticationPrincipal User currentUser,
@RequestHeader(value = "Authorization") String authorization) {
2017-08-18 16:08:27 +07:00
UserData userData = userQueryService.findById(currentUser.getId()).get();
return ResponseEntity.ok(userResponse(
new UserWithToken(userData, authorization.split(" ")[1])
));
2017-08-08 20:14:14 +07:00
}
2017-08-14 13:27:36 +07:00
@PutMapping
public ResponseEntity updateProfile(@AuthenticationPrincipal User currentUser,
2017-08-18 16:08:27 +07:00
@RequestHeader("Authorization") String token,
2017-08-14 13:27:36 +07:00
@Valid @RequestBody UpdateUserParam updateUserParam,
BindingResult bindingResult) {
2017-08-18 11:09:07 +07:00
if (bindingResult.hasErrors()) {
throw new InvalidRequestException(bindingResult);
}
checkUniquenessOfUsernameAndEmail(currentUser, updateUserParam, bindingResult);
2017-08-14 13:27:36 +07:00
currentUser.update(
updateUserParam.getEmail(),
updateUserParam.getUsername(),
updateUserParam.getPassword(),
updateUserParam.getBio(),
updateUserParam.getImage());
userRepository.save(currentUser);
2017-08-18 16:08:27 +07:00
UserData userData = userQueryService.findById(currentUser.getId()).get();
return ResponseEntity.ok(userResponse(
new UserWithToken(userData, token.split(" ")[1])
));
2017-08-18 11:09:07 +07:00
}
private void checkUniquenessOfUsernameAndEmail(User currentUser, UpdateUserParam updateUserParam, BindingResult bindingResult) {
if (!"".equals(updateUserParam.getUsername())) {
Optional<User> byUsername = userRepository.findByUsername(updateUserParam.getUsername());
if (byUsername.isPresent() && !byUsername.get().equals(currentUser)) {
bindingResult.rejectValue("username", "DUPLICATED", "username already exist");
}
}
if (!"".equals(updateUserParam.getEmail())) {
Optional<User> byEmail = userRepository.findByEmail(updateUserParam.getEmail());
if (byEmail.isPresent() && !byEmail.get().equals(currentUser)) {
bindingResult.rejectValue("email", "DUPLICATED", "email already exist");
}
}
if (bindingResult.hasErrors()) {
throw new InvalidRequestException(bindingResult);
}
2017-08-15 16:52:23 +07:00
}
private Map<String, Object> userResponse(UserWithToken userWithToken) {
return new HashMap<String, Object>() {{
put("user", userWithToken);
}};
2017-08-14 13:27:36 +07:00
}
}
@Getter
@JsonRootName("user")
@NoArgsConstructor
class UpdateUserParam {
@Email(message = "should be an email")
private String email = "";
private String password = "";
private String username = "";
private String bio = "";
private String image = "";
2017-08-08 20:14:14 +07:00
}