diff --git a/src/main/java/de/anxietyprime/swajodel/JodelPost.java b/src/main/java/de/anxietyprime/swajodel/JodelPost.java index 78a0c55..5ab98b5 100644 --- a/src/main/java/de/anxietyprime/swajodel/JodelPost.java +++ b/src/main/java/de/anxietyprime/swajodel/JodelPost.java @@ -83,7 +83,7 @@ public class JodelPost { this.content = rs.getString("content"); this.date = rs.getTimestamp("postdate"); this.location = new Location(rs.getFloat("longitude"), rs.getFloat("latitude")); - this.reaction = Optional.of(rs.getBoolean("reaction")); + this.reaction = rs.getString("reaction") == null ? Optional.empty() : Optional.of(rs.getBoolean("reaction")); this.reactions = new Reactions(rs.getLong("positive"), rs.getLong("negative")); this.parent = Optional.of(rs.getLong("parent")); } @@ -99,7 +99,7 @@ public class JodelPost { @JsonProperty("date") Timestamp date, @JsonProperty("location") Location location, @JsonProperty("parent") Optional parent) { - this.authorID = User.getUserID(); // TODO: getter from Keycloak + this.authorID = User.getID(); // TODO: getter from Keycloak this.title = title; this.content = content; this.date = date; diff --git a/src/main/java/de/anxietyprime/swajodel/Routes.java b/src/main/java/de/anxietyprime/swajodel/Routes.java index 0e21782..9d10f5a 100644 --- a/src/main/java/de/anxietyprime/swajodel/Routes.java +++ b/src/main/java/de/anxietyprime/swajodel/Routes.java @@ -19,7 +19,7 @@ public class Routes { Vector posts = new Vector<>(); // get UserID - Long userID = User.getUserID(); + Long userID = User.getID(); // DB connection and statement Connection c; @@ -135,7 +135,7 @@ public class Routes { Optional root_post = Optional.empty(); // get UserID - Long userID = User.getUserID(); + Long userID = User.getID(); // DB connection and statement Connection c; @@ -316,6 +316,65 @@ public class Routes { return post; } + @PatchMapping("/post/{id}") + public void reactToPost(@PathVariable long id, @RequestBody Optional reaction) { + long userID = User.getID(); + + // DB connection and statement + Connection c; + PreparedStatement stmt; + + // 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("UPDATE Reactions SET positive = (?) WHERE post = (?) AND userid = (?) RETURNING id, positive"); + + stmt.setObject(1, reaction.orElse(null)); + stmt.setObject(2, id); + stmt.setObject(3, userID); + + // insert post and get its id + ResultSet rs = stmt.executeQuery(); + + // check if there is a parent + if (!rs.next()) { + // create a new statement + stmt = c.prepareStatement("INSERT INTO Reactions(userid, post, positive) VALUES ((?), (?), (?))"); + + // fill statement + stmt.setObject(1, userID); + stmt.setObject(2, id); + stmt.setObject(3, reaction.orElse(null)); + + // execute statement + stmt.execute(); + } + + // commit the changes + c.commit(); + + // 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"); + } + } + @DeleteMapping("/post/{id}") public void deletePost(@PathVariable long id) { // DB connection and statement @@ -338,7 +397,7 @@ public class Routes { stmt = c.prepareStatement("UPDATE Posts SET deleted = now() WHERE id = ? AND author = (?) AND deleted IS NULL"); stmt.setObject(1, id); - stmt.setObject(2, User.getUserID()); + stmt.setObject(2, User.getID()); // insert delete time stmt.execute(); diff --git a/src/main/java/de/anxietyprime/swajodel/User.java b/src/main/java/de/anxietyprime/swajodel/User.java index ea33422..f9fd9b3 100644 --- a/src/main/java/de/anxietyprime/swajodel/User.java +++ b/src/main/java/de/anxietyprime/swajodel/User.java @@ -14,9 +14,9 @@ public class User { return SecurityContextHolder.getContext().getAuthentication().getName(); } - static public Long getUserID () { + static public Long getID() { String uuid = SecurityContextHolder.getContext().getAuthentication().getName(); - Long id = -1L; + long id; // DB connection and statement Connection c;