From 6f03ebef27ed784b665d981126a55918e130b5fc Mon Sep 17 00:00:00 2001 From: Christian Trutz Date: Wed, 17 Apr 2024 17:30:29 +0200 Subject: [PATCH] fix: Person Controller --- .../trutzio/devopstools/EchoController.java | 29 ------------------- src/main/java/trutzio/devopstools/Person.java | 8 ++++- .../trutzio/devopstools/PersonController.java | 23 +++++++++++++++ .../trutzio/devopstools/PersonRepository.java | 2 +- 4 files changed, 31 insertions(+), 31 deletions(-) delete mode 100644 src/main/java/trutzio/devopstools/EchoController.java create mode 100644 src/main/java/trutzio/devopstools/PersonController.java diff --git a/src/main/java/trutzio/devopstools/EchoController.java b/src/main/java/trutzio/devopstools/EchoController.java deleted file mode 100644 index 01f4cf2..0000000 --- a/src/main/java/trutzio/devopstools/EchoController.java +++ /dev/null @@ -1,29 +0,0 @@ -package trutzio.devopstools; - -import java.util.UUID; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class EchoController { - - private final PersonRepository personRepository; - - public EchoController(PersonRepository personRepository) { - this.personRepository = personRepository; - } - - @GetMapping("/") - // @WithSpan(value = "test span") - public String echo() throws InterruptedException { - for (int i = 0; i < 10; i++) { - Person person = new Person(); - person.id = UUID.randomUUID().toString(); - person.name = "John Doe " + i; - personRepository.save(person); - } - return "OK"; - } - -} diff --git a/src/main/java/trutzio/devopstools/Person.java b/src/main/java/trutzio/devopstools/Person.java index b463e49..af52336 100644 --- a/src/main/java/trutzio/devopstools/Person.java +++ b/src/main/java/trutzio/devopstools/Person.java @@ -1,13 +1,19 @@ package trutzio.devopstools; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.SequenceGenerator; @Entity public class Person { @Id - String id; + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "person_id_seq") + @SequenceGenerator(name = "person_id_seq") + Long id; + String name; } \ No newline at end of file diff --git a/src/main/java/trutzio/devopstools/PersonController.java b/src/main/java/trutzio/devopstools/PersonController.java new file mode 100644 index 0000000..c4030e0 --- /dev/null +++ b/src/main/java/trutzio/devopstools/PersonController.java @@ -0,0 +1,23 @@ +package trutzio.devopstools; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class PersonController { + + private final PersonRepository personRepository; + + public PersonController(PersonRepository personRepository) { + this.personRepository = personRepository; + } + + @GetMapping("/") + // @WithSpan(value = "test span") + public String newPerson() throws InterruptedException { + Person person = new Person(); + person.name = "John Doe"; + return personRepository.save(person).id.toString(); + } + +} diff --git a/src/main/java/trutzio/devopstools/PersonRepository.java b/src/main/java/trutzio/devopstools/PersonRepository.java index 73da319..c110a3b 100644 --- a/src/main/java/trutzio/devopstools/PersonRepository.java +++ b/src/main/java/trutzio/devopstools/PersonRepository.java @@ -2,6 +2,6 @@ import org.springframework.data.jpa.repository.JpaRepository; -public interface PersonRepository extends JpaRepository { +public interface PersonRepository extends JpaRepository { }