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

104 lines
3.9 KiB
Java
Raw Normal View History

2017-08-08 10:01:06 +07:00
package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException;
import io.spring.application.user.UserQueryService;
2017-08-14 10:09:11 +07:00
import io.spring.core.user.EncryptService;
2017-08-08 10:01:06 +07:00
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
2017-08-14 10:09:11 +07:00
import java.util.Optional;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
2017-08-08 10:01:06 +07:00
@RestController
public class UsersApi {
private UserRepository userRepository;
private UserQueryService userQueryService;
private String defaultImage;
2017-08-14 10:09:11 +07:00
private EncryptService encryptService;
2017-08-08 10:01:06 +07:00
@Autowired
2017-08-08 20:14:14 +07:00
public UsersApi(UserRepository userRepository,
UserQueryService userQueryService,
2017-08-14 10:09:11 +07:00
EncryptService encryptService,
2017-08-08 20:14:14 +07:00
@Value("${image.default}") String defaultImage) {
2017-08-08 10:01:06 +07:00
this.userRepository = userRepository;
this.userQueryService = userQueryService;
2017-08-14 10:09:11 +07:00
this.encryptService = encryptService;
2017-08-08 10:01:06 +07:00
this.defaultImage = defaultImage;
}
2017-08-14 10:09:11 +07:00
@RequestMapping(path = "/users", method = POST)
2017-08-08 20:14:14 +07:00
public ResponseEntity createUser(@Valid @RequestBody RegisterParam registerParam, BindingResult bindingResult) {
2017-08-08 10:01:06 +07:00
if (bindingResult.hasErrors()) {
throw new InvalidRequestException(bindingResult);
}
if (userRepository.findByUsername(registerParam.getUsername()).isPresent()) {
bindingResult.rejectValue("username", "DUPLICATED", "duplicated username");
throw new InvalidRequestException(bindingResult);
}
if (userRepository.findByEmail(registerParam.getEmail()).isPresent()) {
bindingResult.rejectValue("email", "DUPLICATED", "duplicated email");
throw new InvalidRequestException(bindingResult);
}
User user = new User(
registerParam.getEmail(),
registerParam.getUsername(),
2017-08-14 10:09:11 +07:00
encryptService.encrypt(registerParam.getPassword()),
2017-08-08 10:01:06 +07:00
"",
defaultImage);
userRepository.save(user);
2017-08-14 10:09:11 +07:00
return ResponseEntity.status(201).body(userQueryService.fetchNewAuthenticatedUser(user.getUsername()));
}
@RequestMapping(path = "/users/login", method = POST)
public ResponseEntity userLogin(@Valid @RequestBody LoginParam loginParam, BindingResult bindingResult) {
Optional<User> optional = userRepository.findByEmail(loginParam.getEmail());
if (optional.isPresent() && encryptService.check(loginParam.getPassword(), optional.get().getPassword())) {
return ResponseEntity.ok(userQueryService.fetchNewAuthenticatedUser(optional.get().getUsername()));
} else {
bindingResult.rejectValue("password", "INVALID", "invalid email or password");
throw new InvalidRequestException(bindingResult);
}
2017-08-08 10:01:06 +07:00
}
}
2017-08-14 10:09:11 +07:00
@Getter
@JsonRootName("user")
@NoArgsConstructor
class LoginParam {
@NotBlank(message = "can't be empty")
@Email(message = "should be an email")
private String email;
@NotBlank(message = "can't be empty")
private String password;
}
2017-08-08 10:01:06 +07:00
@Getter
@JsonRootName("user")
@NoArgsConstructor
class RegisterParam {
@NotBlank(message = "can't be empty")
@Email(message = "should be an email")
private String email;
@NotBlank(message = "can't be empty")
private String username;
@NotBlank(message = "can't be empty")
private String password;
}