diff --git a/src/main/java/de/anxietyprime/swajodel/JodelPost.java b/src/main/java/de/anxietyprime/swajodel/JodelPost.java index 8717ed0..4c464f6 100644 --- a/src/main/java/de/anxietyprime/swajodel/JodelPost.java +++ b/src/main/java/de/anxietyprime/swajodel/JodelPost.java @@ -7,6 +7,7 @@ import java.util.Vector; import java.sql.*; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; public class JodelPost { @@ -30,6 +31,8 @@ public class JodelPost { public Optional reaction; // all other reactions public Reactions reactions; + // Parent id + public Optional parent = Optional.empty(); // anonymize function to recursively anonymize the posts public void anonymize(Optional> idCache) { @@ -95,12 +98,19 @@ public class JodelPost { @JsonCreator public JodelPost(@JsonProperty("title") String title, @JsonProperty("content") String content, - @JsonProperty("postdate") Timestamp date, - @JsonProperty("location") Location location) { + @JsonProperty("date") Timestamp date, + @JsonProperty("location") Location location, + @JsonProperty("parent") Optional parent) { this.authorID = 10000L; // TODO: getter from Keycloak this.title = title; this.content = content; this.date = date; - this.location = location; + this.location = location; + this.parent = parent; + } + + @JsonIgnore + public long getAuthorID() { + return authorID; } } diff --git a/src/main/java/de/anxietyprime/swajodel/Location.java b/src/main/java/de/anxietyprime/swajodel/Location.java index addc773..1a077ec 100644 --- a/src/main/java/de/anxietyprime/swajodel/Location.java +++ b/src/main/java/de/anxietyprime/swajodel/Location.java @@ -1,10 +1,14 @@ package de.anxietyprime.swajodel; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + public class Location { public float longitude; public float latitude; - public Location(float longitude, float latitude) { + @JsonCreator + public Location(@JsonProperty("longitude") float longitude, @JsonProperty("latitude") float latitude ) { this.longitude = longitude; this.latitude = latitude; } diff --git a/src/main/java/de/anxietyprime/swajodel/Routes.java b/src/main/java/de/anxietyprime/swajodel/Routes.java index a6b26e3..5323a00 100644 --- a/src/main/java/de/anxietyprime/swajodel/Routes.java +++ b/src/main/java/de/anxietyprime/swajodel/Routes.java @@ -230,8 +230,6 @@ public class Routes { Connection c = null; PreparedStatement stmt = null; - System.out.println(post); - // try to get data from db try { // check for the driver @@ -245,12 +243,38 @@ public class Routes { c.setAutoCommit(false); // create a new statement - stmt = c.prepareStatement(""); + stmt = c.prepareStatement("INSERT INTO Posts(author, title, content, postdate, postlocation) " + + "VALUES (?, ?, ?, ?, Point(?, ?)) RETURNING id"); - //stmt.setObject(); + stmt.setObject(1, post.getAuthorID()); + stmt.setObject(2, post.title); + stmt.setObject(3, post.content); + stmt.setObject(4, post.date); + stmt.setObject(5, post.location.longitude); + stmt.setObject(6, post.location.latitude); - // query recursively for posts inside a 10km radius - stmt.execute(); + // insert post and get its id + ResultSet rs = stmt.executeQuery(); + + // get the id of the new post + rs.next(); + post.id = rs.getLong("id"); + + // check if there is a parent + if (post.parent.isPresent()) { + // create a new statement + stmt = c.prepareStatement("INSERT INTO comments(parent, child) VALUES (?, ?)"); + + // fill statement + stmt.setObject(1, post.parent.get()); + stmt.setObject(2, post.id); + + // execute statement + stmt.execute(); + } + + // commit the changes + c.commit(); // close all connections to db stmt.close(); @@ -265,4 +289,43 @@ public class Routes { return post; } + + @DeleteMapping("/post/{id}") + public void deletePost(@PathVariable long id) { + // DB connection and statement + Connection c = null; + PreparedStatement stmt = null; + + // 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("UPDATE Posts SET deleted = now() WHERE id = ? AND deleted IS NULL"); + + stmt.setObject(1, id); + + // insert delete time + stmt.execute(); + + // close all connections to db + stmt.close(); + c.commit(); + 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"); + } + } }