Skip to content

Commit

Permalink
Comments and votes now deleted when the associated post is deleted.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsteadly committed Mar 20, 2017
1 parent 1a3c41e commit 529bebd
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,9 @@ public Rating updateRating(Rating rating) throws RatingException {
public Rating selectRating(Long id) {
return rc.selectRating(id);
}

@Override
public List<Vote> getVotes(Comment comment) {
return vc.getVotes(comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface FeedbackServiceInterface {
/**
* Gets all comments associated with the post
* @param post
* @return An ArrayList of comments for the given post
* @return An List of comments for the given post
* @throws PostException
*/
List<Comment> getComments(Post post) throws PostException;
Expand Down Expand Up @@ -95,6 +95,13 @@ public interface FeedbackServiceInterface {
* @return score of given comment
*/
int getScore(Comment comment);

/**
* Returns all votes associated with comment
* @param comment
* @return
*/
List<Vote> getVotes(Comment comment);

/**
* Method to calculate total reputation given by comments for a user
Expand Down
21 changes: 19 additions & 2 deletions compute/src/main/java/com/piccritic/compute/post/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

import org.hibernate.Hibernate;

import com.piccritic.compute.feedback.FeedbackService;
import com.piccritic.compute.feedback.FeedbackServiceInterface;
import com.piccritic.database.feedback.Comment;
import com.piccritic.database.feedback.CommentException;
import com.piccritic.database.feedback.Vote;
import com.piccritic.database.feedback.VoteException;
import com.piccritic.database.post.Album;
import com.piccritic.database.post.AlbumException;
import com.piccritic.database.post.JPAPostConnector;
Expand All @@ -32,6 +38,7 @@ public class PostService implements PostServiceInterface {
public static final String USERS_DIR = "users";

static PostConnector pc = new JPAPostConnector();
private FeedbackServiceInterface fs = FeedbackService.createService();

public File getImageFile(String handle) {
Path p0 = Paths.get(USERS_DIR, handle);
Expand Down Expand Up @@ -82,15 +89,25 @@ public Post createPost(Post post) throws PostException, AlbumException{
return pc.updatePost(post) ;
}

public boolean deletePost(Post post) throws PostException{
public boolean deletePost(Post post) throws PostException, CommentException, VoteException {
if (post == null) {
throw new PostException("Cannot delete null post");
}

List<Comment> comments = fs.getComments(post);
for (Comment comment : comments) {
List<Vote> votes = fs.getVotes(comment);
for (Vote vote : votes) {
fs.deleteVote(vote);
}
fs.deleteComment(comment);
}

File image = new File(post.getPath());
if (image.exists()) {
image.delete();
}

return pc.deletePost(post);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.File;
import java.util.List;

import com.piccritic.database.feedback.CommentException;
import com.piccritic.database.feedback.VoteException;
import com.piccritic.database.post.Album;
import com.piccritic.database.post.AlbumException;
import com.piccritic.database.post.Post;
Expand Down Expand Up @@ -45,8 +47,10 @@ public interface PostServiceInterface {
* @param post
* to delete
* @return Post object deleted
* @throws CommentException
* @throws VoteException
*/
public boolean deletePost(Post post) throws PostException;
public boolean deletePost(Post post) throws PostException, CommentException, VoteException;

/**
* Returns a user's default album
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import org.junit.Before;
import org.junit.Test;

import com.piccritic.compute.feedback.FeedbackService;
import com.piccritic.compute.feedback.FeedbackServiceInterface;
import com.piccritic.database.feedback.Comment;
import com.piccritic.database.feedback.CommentException;
import com.piccritic.database.feedback.VoteException;
import com.piccritic.database.license.AttributionLicense;
import com.piccritic.database.post.Album;
import com.piccritic.database.post.AlbumException;
Expand All @@ -33,9 +38,13 @@ public class PostServiceTest {
private Set<Post> postSet;
private Critic critic;
private Set<Album> albumSet;
private Comment comment;
private Set<Comment> criticComments = new HashSet<Comment>();
private Set<Comment> postComments = new HashSet<Comment>();
private JPAUserConnector uc = new JPAUserConnector();
private JPAPostConnector pc = new JPAPostConnector();
private PostService ps = new PostService();
private FeedbackServiceInterface fs = FeedbackService.createService();

@Before
public void setup() throws Exception{
Expand All @@ -45,12 +54,14 @@ public void setup() throws Exception{
postSet.add(post);
critic = new Critic();
albumSet = new HashSet<Album>();
comment = new Comment();

critic.setFirstName("firstName");
critic.setLastName("lastName");
critic.setJoinDate(new Date(0));
critic.setLicense(new AttributionLicense());
critic.setHandle("handle");
critic.setComments(criticComments);
albumSet.add(album);

album.setName("albumName");
Expand All @@ -63,8 +74,16 @@ public void setup() throws Exception{
post.setUploadDate(null);
post.setLicense(new AttributionLicense());
post.setAlbum(album);
post.setComments(postComments);

uc.insertCritic(critic, "hash");
postComments.add(comment);
comment.setContent("this is a comment");
comment.setCreationDate(new Date(0));
criticComments.add(comment);
comment.setReplies(new HashSet<Comment>());
comment.setScore(0);

critic = uc.insertCritic(critic, "hash");
pc.insertAlbum(album);
post.setPath("path");
}
Expand Down Expand Up @@ -99,9 +118,13 @@ public void testEditPost(){
public void testDeletePost(){
try{
Post created = ps.createPost(post);
comment.setPost(created);
comment.setCritic(critic);
comment = fs.insertComment(comment);
ps.deletePost(created);
post.setComments(new HashSet<Comment>());
pc.insertPost(post);
} catch(PostException | AlbumException e){
} catch(PostException | AlbumException | CommentException | VoteException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,12 @@ public int getScore(Comment comment) {
}
return score;
}

@Override
public List<Vote> getVotes(Comment comment) {
String query = "SELECT v FROM Vote v WHERE v.comment = :comment";
TypedQuery<Vote> q = votes.getEntityProvider().getEntityManager().createQuery(query, Vote.class)
.setParameter("comment", comment);
return q.getResultList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package com.piccritic.database.feedback;

import java.util.List;

import com.piccritic.database.user.Critic;

/**
Expand Down Expand Up @@ -56,4 +58,11 @@ public interface VoteConnector {
* @return
*/
public int getScore(Comment comment);

/**
* Returns the votes associated with the comment
* @param comment
* @return
*/
public List<Vote> getVotes(Comment comment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.piccritic.compute.user.UserService;
import com.piccritic.compute.user.UserServiceInterface;
import com.piccritic.database.feedback.Comment;
import com.piccritic.database.feedback.CommentException;
import com.piccritic.database.feedback.VoteException;
import com.piccritic.database.post.Post;
import com.piccritic.database.post.PostException;
import com.piccritic.website.feedback.CommentComponent;
Expand Down Expand Up @@ -81,7 +83,7 @@ public void enter(ViewChangeEvent event) {
try {
service.deletePost(post);
Notification.show("Post deleted successfuly", Type.TRAY_NOTIFICATION);
} catch (PostException e1) {
} catch (PostException | CommentException | VoteException e1) {
Notification.show(e1.getLocalizedMessage(), Type.WARNING_MESSAGE);
}
});
Expand Down

0 comments on commit 529bebd

Please sign in to comment.