From 49254f3d879518b16d025eab4ec3ef239b1e1629 Mon Sep 17 00:00:00 2001 From: Timo Schneider Date: Wed, 5 Jun 2024 11:00:09 +0200 Subject: [PATCH] added parent id to posts --- .../de/anxietyprime/swajodel/JodelPost.java | 18 +++++++++++++----- .../java/de/anxietyprime/swajodel/Routes.java | 17 +++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/anxietyprime/swajodel/JodelPost.java b/src/main/java/de/anxietyprime/swajodel/JodelPost.java index 917732f..49498c3 100644 --- a/src/main/java/de/anxietyprime/swajodel/JodelPost.java +++ b/src/main/java/de/anxietyprime/swajodel/JodelPost.java @@ -1,6 +1,6 @@ package de.anxietyprime.swajodel; -import java.util.Date; +import java.util.Objects; import java.util.Optional; import java.util.Vector; @@ -14,7 +14,7 @@ public class JodelPost { // id of the post public Long id; // id of the author in db - private Long authorID; + private final Long authorID; // anonymized authorID public Long anonymousID; // title of the post @@ -61,9 +61,10 @@ public class JodelPost { } // 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 - if (parent == this.id) { + if (Objects.equals(post.parent.get(), this.id)) { // add the comment this.comments.add(post); // return success @@ -72,7 +73,7 @@ public class JodelPost { // 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; + if (comment.addComment(post)) return true; } // return no success return false; @@ -88,6 +89,7 @@ public class JodelPost { this.date = rs.getTimestamp("postdate"); this.location = new Location(rs.getLong("longitude"), rs.getLong("latitude")); this.reactions = new Reactions(rs.getLong("positive"), rs.getLong("negative")); + this.parent = Optional.of(rs.getLong("parent")); } // constructor from authorID for tests @@ -115,4 +117,10 @@ public class JodelPost { public long getAuthorID() { return authorID; } + + @JsonIgnore + public boolean isComment() { + if (parent.isEmpty()) return false; + return !Objects.equals(id, parent.get()); + } } diff --git a/src/main/java/de/anxietyprime/swajodel/Routes.java b/src/main/java/de/anxietyprime/swajodel/Routes.java index 5323a00..d9247e0 100644 --- a/src/main/java/de/anxietyprime/swajodel/Routes.java +++ b/src/main/java/de/anxietyprime/swajodel/Routes.java @@ -81,12 +81,9 @@ public class Routes { // create a post from the author id JodelPost post = new JodelPost(rs); - // 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) { + if (!post.isComment()) { // add the post to the posts posts.add(post); } @@ -95,7 +92,7 @@ public class Routes { // iterate over all posts for (JodelPost p : posts) { // 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 { // 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"); } + // 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; }