1 package de.juplo.boot.data.jdbc;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.context.ApplicationEventPublisher;
6 import org.springframework.dao.IncorrectResultSizeDataAccessException;
7 import org.springframework.http.ResponseEntity;
8 import org.springframework.transaction.annotation.Transactional;
9 import org.springframework.util.StreamUtils;
10 import org.springframework.web.bind.annotation.*;
11 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12 import org.springframework.web.util.UriComponents;
14 import javax.servlet.http.HttpServletRequest;
15 import java.io.IOException;
16 import java.nio.charset.Charset;
17 import java.time.LocalDateTime;
19 import static de.juplo.boot.data.jdbc.UserEvent.Type.CREATED;
20 import static de.juplo.boot.data.jdbc.UserEvent.Type.DELETED;
24 @RequestMapping("/users")
25 public class UserController {
27 private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
30 private final UserRepository repository;
31 private final ApplicationEventPublisher publisher;
34 public UserController(
35 UserRepository repository,
36 ApplicationEventPublisher publisher)
38 this.repository = repository;
39 this.publisher = publisher;
44 public ResponseEntity<Void> createUser(
45 ServletUriComponentsBuilder builder,
46 @RequestBody String username) {
47 String sanitizedUsername = UserController.sanitize(username);
48 User user = new User(sanitizedUsername, LocalDateTime.now(), false);
50 // Triggering a unique-error for username prevents persistence
51 repository.save(user);
52 publisher.publishEvent(new UserEvent(this, CREATED, sanitizedUsername));
53 user = repository.findByUsername(sanitizedUsername);
58 .pathSegment("{username}")
59 .buildAndExpand(sanitizedUsername);
60 return ResponseEntity.created(uri.toUri()).build();
63 @GetMapping("{username}")
64 public ResponseEntity<User> getUser(@PathVariable String username) {
65 User user = repository.findByUsername(UserController.sanitize(username));
68 return ResponseEntity.notFound().build();
70 return ResponseEntity.ok(user);
73 @DeleteMapping("{username}")
74 public ResponseEntity<User> removeUser(@PathVariable String username) {
75 User user = repository.findByUsername(UserController.sanitize(username));
78 return ResponseEntity.notFound().build();
80 repository.delete(user);
81 publisher.publishEvent(new UserEvent(this, DELETED, username));
83 return ResponseEntity.ok(user);
87 public ResponseEntity<Iterable<User>> getUsers() {
88 return ResponseEntity.ok(repository.findAll());
92 private static String sanitize(String string) {
96 return string.trim().toLowerCase();
100 public ResponseEntity<?> incorrectResultSizeDataAccessException(
101 HttpServletRequest request,
102 IncorrectResultSizeDataAccessException e
107 username = StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset());
109 catch (IOException ioe)
111 username = e.getMessage() + " -> " + ioe.getMessage();
113 LOG.info("User {} already exists!", username);
114 return ResponseEntity.badRequest().build();