From: Kai Moritz Date: Sun, 12 Jul 2020 11:55:38 +0000 (+0200) Subject: WIP X-Git-Tag: wip-initial~13 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=4079eda81d0c8f94f3b1ded78056b52256f0d519;p=demos%2Fkafka%2Foutbox WIP --- diff --git a/src/main/java/de/trion/kafka/outbox/OutboxController.java b/src/main/java/de/trion/kafka/outbox/OutboxController.java index e1f8919..86ff44f 100644 --- a/src/main/java/de/trion/kafka/outbox/OutboxController.java +++ b/src/main/java/de/trion/kafka/outbox/OutboxController.java @@ -31,15 +31,32 @@ public class OutboxController { public ResponseEntity getVorgang( ServletUriComponentsBuilder builder, @RequestBody String username) { - String cleaned = username.trim().toLowerCase(); - User user = new User(cleaned, LocalDateTime.now(), false); + String sanitizedUsername = OutboxController.sanitize(username); + User user = new User(sanitizedUsername, LocalDateTime.now(), false); repository.save(user); // TODO: Not-Unique Fehler auslösen UriComponents uri = - builder - .fromCurrentRequest() - .path("{username}") - .buildAndExpand(cleaned); + builder + .fromCurrentRequest() + .path("{username}") + .buildAndExpand(sanitizedUsername); return ResponseEntity.created(uri.toUri()).build(); } + + @GetMapping("{username}") + public ResponseEntity getUser(@PathVariable String username) { + User user = repository.findByUsername(OutboxController.sanitize(username)); + + if (user == null) + return ResponseEntity.notFound().build(); + + return ResponseEntity.ok(user); + } + + private static String sanitize(String string) { + if (string == null) + return ""; + + return string.trim().toLowerCase(); + } } diff --git a/src/main/java/de/trion/kafka/outbox/UserRepository.java b/src/main/java/de/trion/kafka/outbox/UserRepository.java index 6791180..3437051 100644 --- a/src/main/java/de/trion/kafka/outbox/UserRepository.java +++ b/src/main/java/de/trion/kafka/outbox/UserRepository.java @@ -7,5 +7,5 @@ import org.springframework.jdbc.core.JdbcTemplate; public interface UserRepository extends CrudRepository { @Query("select * from User u where u.username = :username") - User findByUsername(@Param("email") String username); + User findByUsername(@Param("username") String username); }