added parent id to posts

This commit is contained in:
2024-06-05 11:00:09 +02:00
parent d843bbd787
commit 49254f3d87
2 changed files with 24 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
package de.anxietyprime.swajodel; package de.anxietyprime.swajodel;
import java.util.Date; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Vector; import java.util.Vector;
@@ -14,7 +14,7 @@ public class JodelPost {
// id of the post // id of the post
public Long id; public Long id;
// id of the author in db // id of the author in db
private Long authorID; private final Long authorID;
// anonymized authorID // anonymized authorID
public Long anonymousID; public Long anonymousID;
// title of the post // title of the post
@@ -61,9 +61,10 @@ public class JodelPost {
} }
// add a comment if it is really a comment // add a comment if it is really a comment
public boolean addComment(JodelPost post, long parent) { public boolean addComment(JodelPost post) {
if (post.parent.isEmpty()) return true;
// check if the post is a direct comment // check if the post is a direct comment
if (parent == this.id) { if (Objects.equals(post.parent.get(), this.id)) {
// add the comment // add the comment
this.comments.add(post); this.comments.add(post);
// return success // return success
@@ -72,7 +73,7 @@ public class JodelPost {
// recursively repeat this for all comments // recursively repeat this for all comments
for (JodelPost comment : this.comments) { for (JodelPost comment : this.comments) {
// return success if the post is a comment on a child // return success if the post is a comment on a child
if (comment.addComment(post, parent)) return true; if (comment.addComment(post)) return true;
} }
// return no success // return no success
return false; return false;
@@ -88,6 +89,7 @@ public class JodelPost {
this.date = rs.getTimestamp("postdate"); this.date = rs.getTimestamp("postdate");
this.location = new Location(rs.getLong("longitude"), rs.getLong("latitude")); this.location = new Location(rs.getLong("longitude"), rs.getLong("latitude"));
this.reactions = new Reactions(rs.getLong("positive"), rs.getLong("negative")); this.reactions = new Reactions(rs.getLong("positive"), rs.getLong("negative"));
this.parent = Optional.of(rs.getLong("parent"));
} }
// constructor from authorID for tests // constructor from authorID for tests
@@ -115,4 +117,10 @@ public class JodelPost {
public long getAuthorID() { public long getAuthorID() {
return authorID; return authorID;
} }
@JsonIgnore
public boolean isComment() {
if (parent.isEmpty()) return false;
return !Objects.equals(id, parent.get());
}
} }

View File

@@ -81,12 +81,9 @@ public class Routes {
// create a post from the author id // create a post from the author id
JodelPost post = new JodelPost(rs); JodelPost post = new JodelPost(rs);
// get the posts parent
long parent = rs.getLong("parent");
// check if the parent is the own id // check if the parent is the own id
// if it is, it is a post // if it is, it is a post
if (parent == post.id) { if (!post.isComment()) {
// add the post to the posts // add the post to the posts
posts.add(post); posts.add(post);
} }
@@ -95,7 +92,7 @@ public class Routes {
// iterate over all posts // iterate over all posts
for (JodelPost p : posts) { for (JodelPost p : posts) {
// try to add the post to a parent // try to add the post to a parent
p.addComment(post, parent); p.addComment(post);
} }
} }
} }
@@ -200,7 +197,7 @@ public class Routes {
// else it is a comment // else it is a comment
else { else {
// try to add the post to parent // try to add the post to parent
root_post.ifPresent(root -> root.addComment(post, parent)); root_post.ifPresent(root -> root.addComment(post));
} }
} }
@@ -287,6 +284,14 @@ public class Routes {
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "Database is offline"); throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "Database is offline");
} }
// fill missing post values
if (post.parent.isEmpty()) post.parent = Optional.of(post.id);
post.reactions = new Reactions(0, 0);
post.reaction = Optional.empty();
// anonymize code
post.anonymize(Optional.empty());
return post; return post;
} }