Implementation of post deletion.

This commit is contained in:
Florian
2018-05-18 22:42:08 +02:00
parent 15be129540
commit 9fcbf4f576
2 changed files with 26 additions and 0 deletions
@@ -77,4 +77,23 @@ public class PostService {
pResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
}
}
public void delete(final String pPostKey, final HttpServletRequest pRequest,
final HttpServletResponse pResponse) throws IOException {
final Optional<Post> postToDelete = postRepository.getByKey(pPostKey);
if(postToDelete.isPresent()) {
final Optional<User> connectedUser = tokenService.getAuthenticatedUserByToken(pRequest);
if(connectedUser.isPresent()) {
if(connectedUser.get().getKey().equals(postToDelete.get().getAuthor().getKey())) {
postRepository.delete(postToDelete.get());
} else {
pResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
}
} else {
pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
} else {
pResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
}