import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
@RequestBody String username) {
String sanitizedUsername = UserController.sanitize(username);
User user = new User(sanitizedUsername, LocalDateTime.now(), false);
+
+ // Triggering a unique-error for username prevents persistence
repository.save(user);
publisher.publishEvent(new UserEvent(this, CREATED, sanitizedUsername));
+ user = repository.findByUsername(sanitizedUsername);
+
UriComponents uri =
builder
.fromCurrentRequest()
return string.trim().toLowerCase();
}
+
+ @ExceptionHandler
+ public ResponseEntity<?> incorrectResultSizeDataAccessException(
+ IncorrectResultSizeDataAccessException e
+ )
+ {
+ LOG.info("User already exists!");
+ return ResponseEntity.badRequest().build();
+ }
}