From 9070d1452b24859e874521cace1114646590a113 Mon Sep 17 00:00:00 2001 From: Timo Date: Thu, 30 May 2024 18:21:17 +0200 Subject: [PATCH] post dev --- .../de/anxietyprime/swajodel/JodelPost.java | 19 ++++++++- .../java/de/anxietyprime/swajodel/Routes.java | 42 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/anxietyprime/swajodel/JodelPost.java b/src/main/java/de/anxietyprime/swajodel/JodelPost.java index bab4d6c..8717ed0 100644 --- a/src/main/java/de/anxietyprime/swajodel/JodelPost.java +++ b/src/main/java/de/anxietyprime/swajodel/JodelPost.java @@ -6,6 +6,9 @@ import java.util.Vector; import java.sql.*; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + public class JodelPost { // id of the post public Long id; @@ -33,7 +36,7 @@ public class JodelPost { // check if this is the first post in this process if (idCache.isEmpty()) { // create a new Vector as cache - idCache = Optional.of(new Vector()); + idCache = Optional.of(new Vector<>()); } // get the anonymized id as index in cached authorIDs @@ -88,4 +91,16 @@ public class JodelPost { public JodelPost(int authorID) { this.authorID = (long) authorID; } -} \ No newline at end of file + + @JsonCreator + public JodelPost(@JsonProperty("title") String title, + @JsonProperty("content") String content, + @JsonProperty("postdate") Timestamp date, + @JsonProperty("location") Location location) { + this.authorID = 10000L; // TODO: getter from Keycloak + this.title = title; + this.content = content; + this.date = date; + this.location = location; + } +} diff --git a/src/main/java/de/anxietyprime/swajodel/Routes.java b/src/main/java/de/anxietyprime/swajodel/Routes.java index 0b3aabb..a6b26e3 100644 --- a/src/main/java/de/anxietyprime/swajodel/Routes.java +++ b/src/main/java/de/anxietyprime/swajodel/Routes.java @@ -223,4 +223,46 @@ public class Routes { if (root_post.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No post found"); return root_post.get(); } + + @PostMapping("/posts") + public JodelPost postPost(@RequestBody JodelPost post) { + // DB connection and statement + Connection c = null; + PreparedStatement stmt = null; + + System.out.println(post); + + // try to get data from db + try { + // check for the driver + Class.forName("org.postgresql.Driver"); + // get the connection with credentials from env variables + c = DriverManager + .getConnection("jdbc:postgresql://"+ + System.getenv("POSTGRES_IP")+"/"+System.getenv("POSTGRES_DB"), + System.getenv("POSTGRES_USER"), System.getenv("POSTGRES_PASSWORD")); + // disable auto commits + c.setAutoCommit(false); + + // create a new statement + stmt = c.prepareStatement(""); + + //stmt.setObject(); + + // query recursively for posts inside a 10km radius + stmt.execute(); + + // close all connections to db + stmt.close(); + c.close(); + } + + // else log the error + catch ( Exception e ) { + System.err.println( e.getClass().getName()+": "+ e.getMessage() ); + throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "Database is offline"); + } + + return post; + } }