From bce2823d39efa6e42d458d6c97d71f6904b2a606 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 19 Dec 2024 15:14:02 +0100 Subject: [PATCH] feat: add REST exercise --- REST/.gitignore | 4 ++ REST/Makefile | 3 + REST/pom.xml | 50 ++++++++++++++ .../main/java/me/srvstr/rest/Controller.java | 65 +++++++++++++++++++ REST/src/main/java/me/srvstr/rest/Main.java | 12 ++++ .../java/me/srvstr/rest/model/Student.java | 35 ++++++++++ .../verteilte-systeme-rest/Create Student.bru | 19 ++++++ REST/verteilte-systeme-rest/Get Student.bru | 11 ++++ REST/verteilte-systeme-rest/Hello World.bru | 11 ++++ REST/verteilte-systeme-rest/Student.bru | 11 ++++ REST/verteilte-systeme-rest/Students.bru | 11 ++++ REST/verteilte-systeme-rest/bruno.json | 9 +++ .../environments/Local.bru | 2 + 13 files changed, 243 insertions(+) create mode 100644 REST/.gitignore create mode 100644 REST/Makefile create mode 100644 REST/pom.xml create mode 100644 REST/src/main/java/me/srvstr/rest/Controller.java create mode 100644 REST/src/main/java/me/srvstr/rest/Main.java create mode 100644 REST/src/main/java/me/srvstr/rest/model/Student.java create mode 100644 REST/verteilte-systeme-rest/Create Student.bru create mode 100644 REST/verteilte-systeme-rest/Get Student.bru create mode 100644 REST/verteilte-systeme-rest/Hello World.bru create mode 100644 REST/verteilte-systeme-rest/Student.bru create mode 100644 REST/verteilte-systeme-rest/Students.bru create mode 100644 REST/verteilte-systeme-rest/bruno.json create mode 100644 REST/verteilte-systeme-rest/environments/Local.bru diff --git a/REST/.gitignore b/REST/.gitignore new file mode 100644 index 0000000..6cb1223 --- /dev/null +++ b/REST/.gitignore @@ -0,0 +1,4 @@ +/target/ +.project +.settings +.classpath diff --git a/REST/Makefile b/REST/Makefile new file mode 100644 index 0000000..1026388 --- /dev/null +++ b/REST/Makefile @@ -0,0 +1,3 @@ + +run: + mvn spring-boot:run diff --git a/REST/pom.xml b/REST/pom.xml new file mode 100644 index 0000000..d1eae18 --- /dev/null +++ b/REST/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + me.srvstr.rest + rest + 1.0-SNAPSHOT + rmi + + UTF-8 + 17 + + + + + + + + org.springframework.boot + spring-boot + 3.4.0 + + + org.springframework.boot + spring-boot-starter-web + 3.4.0 + + + com.google.code.gson + gson + 2.11.0 + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.2.0 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + me.srvstr.rest.Main + + + + + diff --git a/REST/src/main/java/me/srvstr/rest/Controller.java b/REST/src/main/java/me/srvstr/rest/Controller.java new file mode 100644 index 0000000..a849d2e --- /dev/null +++ b/REST/src/main/java/me/srvstr/rest/Controller.java @@ -0,0 +1,65 @@ +package me.srvstr.rest; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; + +import me.srvstr.rest.model.Student; + +@RestController +public class Controller { + + private static Map students = new HashMap(); + + @GetMapping("/api/v0/hello-world") + public String HelloWorld() { + return "Hello, World!"; + } + + @GetMapping("/api/v0/student") + public String getSomeStudent() { + return new Gson().toJson(new Student(1, "Klaus Peter", 123456)); + } + + @GetMapping("/api/v1/students") + public String getStudents() { + return new Gson().toJson(students); + } + + @PostMapping("/api/v1/student") + public ResponseEntity createStudent(@RequestBody String body) { + + try { + + Student student = new Gson().fromJson(body, Student.class); + + students.put(student.getId(), student); + + } catch (JsonSyntaxException e) { + return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST); + } + + return ResponseEntity.ok("Student created."); + } + + @GetMapping("/api/v1/student/{id}") + public ResponseEntity getStudent(@PathVariable(name = "id") String id) { + Student student = students.get(Integer.parseInt(id)); + + if (null == student) { + return new ResponseEntity("Student not found", HttpStatus.NOT_FOUND); + } else { + return ResponseEntity.ok(new Gson().toJson(student)); + } + } +} diff --git a/REST/src/main/java/me/srvstr/rest/Main.java b/REST/src/main/java/me/srvstr/rest/Main.java new file mode 100644 index 0000000..f131222 --- /dev/null +++ b/REST/src/main/java/me/srvstr/rest/Main.java @@ -0,0 +1,12 @@ +package me.srvstr.rest; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } +} diff --git a/REST/src/main/java/me/srvstr/rest/model/Student.java b/REST/src/main/java/me/srvstr/rest/model/Student.java new file mode 100644 index 0000000..ff0e511 --- /dev/null +++ b/REST/src/main/java/me/srvstr/rest/model/Student.java @@ -0,0 +1,35 @@ +package me.srvstr.rest.model; + +import java.util.Date; + +public class Student { + + private int id; + private int matriculationNumber; + private String name; + + // Set to current time. + private final Date creationDate = new Date(); + + public Student(int id, String name, int matriculationNumber) { + this.id = id; + this.name = name; + this.matriculationNumber = matriculationNumber; + } + + public int getId() { + return id; + } + + public int getMatriculationNumber() { + return matriculationNumber; + } + + public String getName() { + return name; + } + + public Date getCreationDate() { + return creationDate; + } +} diff --git a/REST/verteilte-systeme-rest/Create Student.bru b/REST/verteilte-systeme-rest/Create Student.bru new file mode 100644 index 0000000..099ff92 --- /dev/null +++ b/REST/verteilte-systeme-rest/Create Student.bru @@ -0,0 +1,19 @@ +meta { + name: Create Student + type: http + seq: 5 +} + +post { + url: http://localhost:8080/api/v1/student + body: json + auth: none +} + +body:json { + { + "id": 1, + "name": "Peter Maier", + "matriculationNumber": 123456 + } +} diff --git a/REST/verteilte-systeme-rest/Get Student.bru b/REST/verteilte-systeme-rest/Get Student.bru new file mode 100644 index 0000000..395bf2e --- /dev/null +++ b/REST/verteilte-systeme-rest/Get Student.bru @@ -0,0 +1,11 @@ +meta { + name: Get Student + type: http + seq: 6 +} + +get { + url: http://localhost:8080/api/v1/student/1 + body: none + auth: none +} diff --git a/REST/verteilte-systeme-rest/Hello World.bru b/REST/verteilte-systeme-rest/Hello World.bru new file mode 100644 index 0000000..397bdf7 --- /dev/null +++ b/REST/verteilte-systeme-rest/Hello World.bru @@ -0,0 +1,11 @@ +meta { + name: Hello World + type: http + seq: 2 +} + +get { + url: api/v0/hello-world + body: none + auth: none +} diff --git a/REST/verteilte-systeme-rest/Student.bru b/REST/verteilte-systeme-rest/Student.bru new file mode 100644 index 0000000..7580d61 --- /dev/null +++ b/REST/verteilte-systeme-rest/Student.bru @@ -0,0 +1,11 @@ +meta { + name: Student + type: http + seq: 3 +} + +get { + url: http://localhost:8080/api/v0/student + body: none + auth: none +} diff --git a/REST/verteilte-systeme-rest/Students.bru b/REST/verteilte-systeme-rest/Students.bru new file mode 100644 index 0000000..10a6050 --- /dev/null +++ b/REST/verteilte-systeme-rest/Students.bru @@ -0,0 +1,11 @@ +meta { + name: Students + type: http + seq: 4 +} + +get { + url: http://localhost:8080/api/v1/students + body: none + auth: none +} diff --git a/REST/verteilte-systeme-rest/bruno.json b/REST/verteilte-systeme-rest/bruno.json new file mode 100644 index 0000000..380da6c --- /dev/null +++ b/REST/verteilte-systeme-rest/bruno.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "name": "verteilte-systeme-rest", + "type": "collection", + "ignore": [ + "node_modules", + ".git" + ] +} \ No newline at end of file diff --git a/REST/verteilte-systeme-rest/environments/Local.bru b/REST/verteilte-systeme-rest/environments/Local.bru new file mode 100644 index 0000000..9d1c9da --- /dev/null +++ b/REST/verteilte-systeme-rest/environments/Local.bru @@ -0,0 +1,2 @@ +vars { +}