This commit is contained in:
2024-05-30 18:21:17 +02:00
parent c6deee3da7
commit 9070d1452b
2 changed files with 59 additions and 2 deletions

View File

@@ -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;
}
}
@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;
}
}

View File

@@ -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;
}
}