Compare commits

..

4 Commits

Author SHA1 Message Date
5d494d0ad4 typo 2024-05-24 23:14:00 +02:00
958425feef added get path 2024-05-24 23:10:46 +02:00
0ac475a7d7 added database dependencies 2024-05-24 22:44:42 +02:00
6e4977ed4b added location constructor 2024-05-24 22:44:30 +02:00
4 changed files with 143 additions and 16 deletions

View File

@@ -32,6 +32,12 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -6,10 +6,9 @@ import java.util.Vector;
public class JodelPost { public class JodelPost {
// id of the post // id of the post
private Long id; public Long id;
// id of the author in db // id of the author in db
private Long authorID; private Long authorID;
// anonymized authorID // anonymized authorID
public Long anonymousID; public Long anonymousID;
// title of the post // title of the post
@@ -53,8 +52,26 @@ public class JodelPost {
} }
} }
// TODO: create constructor // add a comment if it is really a comment
public JodelPost(long id) { public boolean addComment(JodelPost post, long parent) {
this.authorID = id; // check if the post is 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;
} }
} }

View File

@@ -3,4 +3,9 @@ package de.anxietyprime.swajodel;
public class Location { public class Location {
public float longitude; public float longitude;
public float latitude; public float latitude;
public Location(float longitude, float latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
} }

View File

@@ -1,24 +1,123 @@
package de.anxietyprime.swajodel; package de.anxietyprime.swajodel;
import com.fasterxml.jackson.databind.JsonSerializer;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.sql.*;
import java.util.Optional; import java.util.Optional;
import java.util.Vector;
@RestController @RestController
public class Routes { public class Routes {
@GetMapping("") @GetMapping("/posts/{longitude}/{latitude}")
public JodelPost test() { public Vector<JodelPost> getPosts(@PathVariable("longitude") float longitude, @PathVariable("latitude") float latitude) {
// TODO: remove dec post // list of all posts (not comments) in range
JodelPost post = new JodelPost(100); Vector<JodelPost> posts = new Vector<>();
// calculate anonymous IDs for the post // DB connection and statement
post.anonymize(Optional.empty()); Connection c = null;
// return the post PreparedStatement stmt = null;
return 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("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;
}
} }