added get path
This commit is contained in:
@@ -6,10 +6,9 @@ import java.util.Vector;
|
||||
|
||||
public class JodelPost {
|
||||
// id of the post
|
||||
private Long id;
|
||||
public Long id;
|
||||
// id of the author in db
|
||||
private Long authorID;
|
||||
|
||||
// anonymized authorID
|
||||
public Long anonymousID;
|
||||
// title of the post
|
||||
@@ -53,8 +52,26 @@ public class JodelPost {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: create constructor
|
||||
public JodelPost(long id) {
|
||||
this.authorID = id;
|
||||
// add a comment if it is really a comment
|
||||
public boolean addComment(JodelPost post, long parent) {
|
||||
// check if the post a direct comment
|
||||
if (parent == this.id) {
|
||||
// add the comment
|
||||
this.comments.add(post);
|
||||
// return success
|
||||
return true;
|
||||
}
|
||||
// recursively repeat this for all comments
|
||||
for (JodelPost comment : this.comments) {
|
||||
// return success if the post is a comment on a child
|
||||
if (comment.addComment(post, parent)) return true;
|
||||
}
|
||||
// return no success
|
||||
return false;
|
||||
}
|
||||
|
||||
// constructor with private authorID
|
||||
public JodelPost(long authorID) {
|
||||
this.authorID = authorID;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,123 @@
|
||||
package de.anxietyprime.swajodel;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Vector;
|
||||
|
||||
@RestController
|
||||
public class Routes {
|
||||
|
||||
@GetMapping("")
|
||||
public JodelPost test() {
|
||||
// TODO: remove dec post
|
||||
JodelPost post = new JodelPost(100);
|
||||
@GetMapping("/posts/{longitude}/{latitude}")
|
||||
public Vector<JodelPost> getPosts(@PathVariable("longitude") float longitude, @PathVariable("latitude") float latitude) {
|
||||
// list of all posts (not comments) in range
|
||||
Vector<JodelPost> posts = new Vector<>();
|
||||
|
||||
// calculate anonymous IDs for the post
|
||||
post.anonymize(Optional.empty());
|
||||
// return the post
|
||||
return post;
|
||||
// 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("WITH RECURSIVE targets AS (\n" +
|
||||
" SELECT\n" +
|
||||
" id,\n" +
|
||||
" id AS parent,\n" +
|
||||
" author,\n" +
|
||||
" title,\n" +
|
||||
" content,\n" +
|
||||
" postdate,\n" +
|
||||
" postlocation[0] AS longitude,\n" +
|
||||
" postlocation[1] AS latitude\n" +
|
||||
" FROM\n" +
|
||||
" posts\n" +
|
||||
" WHERE\n" +
|
||||
" deleted IS NULL\n" +
|
||||
" AND sqrt(power(postlocation[0] - (?), 2) + power(postlocation[1] - (?), 2)) <= 10\n" +
|
||||
" AND id NOT IN (SELECT child FROM comments)\n" +
|
||||
" UNION\n" +
|
||||
" SELECT\n" +
|
||||
" com.child,\n" +
|
||||
" com.parent,\n" +
|
||||
" com.author,\n" +
|
||||
" com.title,\n" +
|
||||
" com.content,\n" +
|
||||
" com.postdate,\n" +
|
||||
" com.postlocation[0],\n" +
|
||||
" com.postlocation[1]\n" +
|
||||
" FROM\n" +
|
||||
" (SELECT * FROM comments inner join posts ON comments.child = posts.id) com\n" +
|
||||
" inner join targets ON targets.id = com.parent\n" +
|
||||
")\n" +
|
||||
"SELECT * FROM targets;");
|
||||
|
||||
stmt.setObject(1, longitude);
|
||||
stmt.setObject(2, latitude);
|
||||
|
||||
// query recursively for posts inside a 10km radius
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
// for all the posts found
|
||||
while ( rs.next() ) {
|
||||
// create a post from the author id
|
||||
JodelPost post = new JodelPost(rs.getLong("author"));
|
||||
// add all other information to the post
|
||||
post.id = rs.getLong("id");
|
||||
post.title = rs.getString("title");
|
||||
post.content = rs.getString("content");
|
||||
post.date = rs.getDate("postdate");
|
||||
post.location = new Location(rs.getLong("longitude"), rs.getLong("latitude"));
|
||||
|
||||
// get the posts parent
|
||||
long parent = rs.getLong("parent");
|
||||
|
||||
// check if the parent is the own id
|
||||
// if it is, it is a post
|
||||
if (parent == post.id) {
|
||||
// add the post to the posts
|
||||
posts.add(post);
|
||||
}
|
||||
// else it is a comment
|
||||
else {
|
||||
// iterate over all posts
|
||||
for (JodelPost p : posts) {
|
||||
// try to add the post to a parent
|
||||
p.addComment(post, parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// close all connections to db
|
||||
rs.close();
|
||||
stmt.close();
|
||||
c.close();
|
||||
}
|
||||
|
||||
// else log the error
|
||||
catch ( Exception e ) {
|
||||
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
|
||||
}
|
||||
|
||||
// calculate anonymous IDs for the posts
|
||||
posts.forEach(post -> {
|
||||
post.anonymize(Optional.empty());
|
||||
});
|
||||
// return the posts
|
||||
return posts;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user