added reaction endpoint
This commit is contained in:
@@ -83,7 +83,7 @@ public class JodelPost {
|
|||||||
this.content = rs.getString("content");
|
this.content = rs.getString("content");
|
||||||
this.date = rs.getTimestamp("postdate");
|
this.date = rs.getTimestamp("postdate");
|
||||||
this.location = new Location(rs.getFloat("longitude"), rs.getFloat("latitude"));
|
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.reactions = new Reactions(rs.getLong("positive"), rs.getLong("negative"));
|
||||||
this.parent = Optional.of(rs.getLong("parent"));
|
this.parent = Optional.of(rs.getLong("parent"));
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ public class JodelPost {
|
|||||||
@JsonProperty("date") Timestamp date,
|
@JsonProperty("date") Timestamp date,
|
||||||
@JsonProperty("location") Location location,
|
@JsonProperty("location") Location location,
|
||||||
@JsonProperty("parent") Optional<Long> parent) {
|
@JsonProperty("parent") Optional<Long> parent) {
|
||||||
this.authorID = User.getUserID(); // TODO: getter from Keycloak
|
this.authorID = User.getID(); // TODO: getter from Keycloak
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.date = date;
|
this.date = date;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class Routes {
|
|||||||
Vector<JodelPost> posts = new Vector<>();
|
Vector<JodelPost> posts = new Vector<>();
|
||||||
|
|
||||||
// get UserID
|
// get UserID
|
||||||
Long userID = User.getUserID();
|
Long userID = User.getID();
|
||||||
|
|
||||||
// DB connection and statement
|
// DB connection and statement
|
||||||
Connection c;
|
Connection c;
|
||||||
@@ -135,7 +135,7 @@ public class Routes {
|
|||||||
Optional<JodelPost> root_post = Optional.empty();
|
Optional<JodelPost> root_post = Optional.empty();
|
||||||
|
|
||||||
// get UserID
|
// get UserID
|
||||||
Long userID = User.getUserID();
|
Long userID = User.getID();
|
||||||
|
|
||||||
// DB connection and statement
|
// DB connection and statement
|
||||||
Connection c;
|
Connection c;
|
||||||
@@ -316,6 +316,65 @@ public class Routes {
|
|||||||
return post;
|
return post;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/post/{id}")
|
||||||
|
public void reactToPost(@PathVariable long id, @RequestBody Optional<Boolean> 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}")
|
@DeleteMapping("/post/{id}")
|
||||||
public void deletePost(@PathVariable long id) {
|
public void deletePost(@PathVariable long id) {
|
||||||
// DB connection and statement
|
// 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 = c.prepareStatement("UPDATE Posts SET deleted = now() WHERE id = ? AND author = (?) AND deleted IS NULL");
|
||||||
|
|
||||||
stmt.setObject(1, id);
|
stmt.setObject(1, id);
|
||||||
stmt.setObject(2, User.getUserID());
|
stmt.setObject(2, User.getID());
|
||||||
|
|
||||||
// insert delete time
|
// insert delete time
|
||||||
stmt.execute();
|
stmt.execute();
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ public class User {
|
|||||||
return SecurityContextHolder.getContext().getAuthentication().getName();
|
return SecurityContextHolder.getContext().getAuthentication().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
static public Long getUserID () {
|
static public Long getID() {
|
||||||
String uuid = SecurityContextHolder.getContext().getAuthentication().getName();
|
String uuid = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||||
Long id = -1L;
|
long id;
|
||||||
|
|
||||||
// DB connection and statement
|
// DB connection and statement
|
||||||
Connection c;
|
Connection c;
|
||||||
|
|||||||
Reference in New Issue
Block a user