fixed post and added delete
This commit is contained in:
@@ -7,6 +7,7 @@ import java.util.Vector;
|
|||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
public class JodelPost {
|
public class JodelPost {
|
||||||
@@ -30,6 +31,8 @@ public class JodelPost {
|
|||||||
public Optional<Boolean> reaction;
|
public Optional<Boolean> reaction;
|
||||||
// all other reactions
|
// all other reactions
|
||||||
public Reactions reactions;
|
public Reactions reactions;
|
||||||
|
// Parent id
|
||||||
|
public Optional<Long> parent = Optional.empty();
|
||||||
|
|
||||||
// anonymize function to recursively anonymize the posts
|
// anonymize function to recursively anonymize the posts
|
||||||
public void anonymize(Optional<Vector<Long>> idCache) {
|
public void anonymize(Optional<Vector<Long>> idCache) {
|
||||||
@@ -95,12 +98,19 @@ public class JodelPost {
|
|||||||
@JsonCreator
|
@JsonCreator
|
||||||
public JodelPost(@JsonProperty("title") String title,
|
public JodelPost(@JsonProperty("title") String title,
|
||||||
@JsonProperty("content") String content,
|
@JsonProperty("content") String content,
|
||||||
@JsonProperty("postdate") Timestamp date,
|
@JsonProperty("date") Timestamp date,
|
||||||
@JsonProperty("location") Location location) {
|
@JsonProperty("location") Location location,
|
||||||
|
@JsonProperty("parent") Optional<Long> parent) {
|
||||||
this.authorID = 10000L; // TODO: getter from Keycloak
|
this.authorID = 10000L; // TODO: getter from Keycloak
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.location = location;
|
this.location = location;
|
||||||
|
this.parent = parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
|
public long getAuthorID() {
|
||||||
|
return authorID;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
package de.anxietyprime.swajodel;
|
package de.anxietyprime.swajodel;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
public class Location {
|
public class Location {
|
||||||
public float longitude;
|
public float longitude;
|
||||||
public float latitude;
|
public float latitude;
|
||||||
|
|
||||||
public Location(float longitude, float latitude) {
|
@JsonCreator
|
||||||
|
public Location(@JsonProperty("longitude") float longitude, @JsonProperty("latitude") float latitude ) {
|
||||||
this.longitude = longitude;
|
this.longitude = longitude;
|
||||||
this.latitude = latitude;
|
this.latitude = latitude;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -230,8 +230,6 @@ public class Routes {
|
|||||||
Connection c = null;
|
Connection c = null;
|
||||||
PreparedStatement stmt = null;
|
PreparedStatement stmt = null;
|
||||||
|
|
||||||
System.out.println(post);
|
|
||||||
|
|
||||||
// try to get data from db
|
// try to get data from db
|
||||||
try {
|
try {
|
||||||
// check for the driver
|
// check for the driver
|
||||||
@@ -245,12 +243,38 @@ public class Routes {
|
|||||||
c.setAutoCommit(false);
|
c.setAutoCommit(false);
|
||||||
|
|
||||||
// create a new statement
|
// 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
|
// insert post and get its id
|
||||||
stmt.execute();
|
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
|
// close all connections to db
|
||||||
stmt.close();
|
stmt.close();
|
||||||
@@ -265,4 +289,43 @@ public class Routes {
|
|||||||
|
|
||||||
return post;
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user