Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Commenting system logic #199

Merged
merged 4 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.github.sdp.mediato.data.CollectionsDatabase;
import com.github.sdp.mediato.data.ReviewInteractionDatabase;
import com.github.sdp.mediato.data.UserDatabase;
import com.github.sdp.mediato.model.Location;
import com.github.sdp.mediato.model.Review;
Expand All @@ -33,6 +34,7 @@
*/
public class CollectionsTests {
private final static int STANDARD_COLLECTION_TIMEOUT = 10;
private final static String COMMENTER_USERNAME = "commenter";
private final User user1 = new User.UserBuilder("uniqueId1")
.setUsername("user_test_1")
.setEmail("email_test_1")
Expand Down Expand Up @@ -76,6 +78,7 @@ public static void cleanDatabase() {
DataBaseTestUtil.cleanDatabase();
}


@Test
//Tests that the collections are added, retrieved and removed properly
public void addsAndRetrievesCollectionProperly() throws ExecutionException, InterruptedException, TimeoutException {
Expand Down Expand Up @@ -127,4 +130,26 @@ public void addsReviewToCollectionProperly() throws InterruptedException, Execut
&& retrievedCollection.getReviews().containsKey(review2.getMedia().getTitle()));
}

@Test
public void commentsReviewProperly() throws InterruptedException, ExecutionException, TimeoutException {
//Add collection
CollectionsDatabase.addCollection(user1.getUsername(), collection2);
Thread.sleep(1000);

//Add review to the collection
CollectionsDatabase.addReviewToCollection(user1.getUsername(), collection2.getCollectionName(), review1);
Thread.sleep(1000);

//Comment review
ReviewInteractionDatabase.commentReview(COMMENTER_USERNAME, user1.getUsername(), collection2.getCollectionName(), review1.getMedia().getTitle(), "This is a comment");
Thread.sleep(1000);

//Retrieve review
Review retrievedReview = CollectionsDatabase.getCollection(user1.getUsername(), collection2.getCollectionName())
.get(STANDARD_COLLECTION_TIMEOUT, TimeUnit.SECONDS).getReviews().get(review1.getMedia().getTitle());

assertTrue(retrievedReview.getComments().containsKey(COMMENTER_USERNAME));
assertEquals(retrievedReview.getComments().get(COMMENTER_USERNAME), "This is a comment");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class DatabaseUtils {

static final String USERS_PATH = "Users/";
static final String REVIEWS_PATH = "reviews/";
static final String COMMENTS_PATH = "comments/";
static final String USER_COLLECTIONS_PATH = "/collections/";
static final String LOCATION_PATH = "/location/";
static final String FOLLOWING_PATH = "/following/";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.sdp.mediato.data;

import static com.github.sdp.mediato.data.DatabaseUtils.COMMENTS_PATH;
import static com.github.sdp.mediato.data.DatabaseUtils.getReactionReference;
import static com.github.sdp.mediato.data.DatabaseUtils.getReviewReference;

import com.github.sdp.mediato.errorCheck.Preconditions;
import com.github.sdp.mediato.model.Review;
Expand All @@ -17,6 +19,23 @@ public class ReviewInteractionDatabase {

public static FirebaseDatabase database = FirebaseDatabase.getInstance();

/**
* Adds a comment to a review.
* @param refUsername the username of the user who is commenting
* @param tarUsername the username of the user who made the review
* @param collectionName the name of the collection
* @param review the review
* @param comment the comment
*/
public static void commentReview(String refUsername, String tarUsername, String collectionName, String review, String comment) {
Preconditions.checkUsername(refUsername);
Preconditions.checkUsername(tarUsername);
getReviewReference(tarUsername, collectionName, review).child(COMMENTS_PATH)
.child(refUsername)
.setValue(comment)
.addOnCompleteListener(task -> System.out.println("Commented review"));
}

/**
* Determines if the user has liked a given review.
* @param refUsername the username of the user who is checked
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/github/sdp/mediato/model/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Review implements Serializable {
private String comment;
private Map<String, Boolean> likes = new HashMap<>();
private Map<String, Boolean> dislikes = new HashMap<>();
private Map<String, String> comments = new HashMap<>();

private Review() {
}
Expand Down Expand Up @@ -65,6 +66,8 @@ public String getComment() {
return comment;
}

public Map<String, String> getComments() { return comments;}

public void setComment(String comment) {
this.comment = comment;
}
Expand Down Expand Up @@ -101,6 +104,9 @@ public void dislike(String username) {
dislikes.put(username, true);
likes.put(username, false);
}
public void addComment(String username, String comment) {
comments.put(username, comment);
}

@Override
public boolean equals(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public String getComment() {
public void unDislike(String username) {this.review.unDislike(username);}
public void like(String username) {this.review.like(username);}
public void dislike(String username) {this.review.dislike(username);}
public void addComment(String username, String comment) {this.review.addComment(username, comment);}

@Override
public boolean equals(@Nullable Object obj) {
Expand Down