Search in sources :

Example 16 with Reaction

use of de.tum.in.www1.artemis.domain.metis.Reaction in project Artemis by ls1intum.

the class ReactionService method deleteReactionById.

/**
 * Determines authority to delete reaction and deletes the reaction
 *
 * @param reactionId    id of the reaction to delete
 * @param courseId      id of the course the according posting belongs to
 */
public void deleteReactionById(Long reactionId, Long courseId) {
    final User user = userRepository.getUserWithGroupsAndAuthorities();
    final Course course = courseRepository.findByIdElseThrow(courseId);
    Reaction reaction = reactionRepository.findByIdElseThrow(reactionId);
    // check if user that wants to delete reaction is user that created the reaction
    if (!user.equals(reaction.getUser())) {
        throw new AccessForbiddenException("Reaction", reaction.getId());
    }
    // get affected post that will be sent as payload in according websocket message
    Post updatedPost;
    if (reaction.getPost() != null) {
        updatedPost = reaction.getPost();
        updatedPost.removeReaction(reaction);
    } else {
        AnswerPost updatedAnswerPost = reaction.getAnswerPost();
        updatedAnswerPost.removeReaction(reaction);
        updatedPost = updatedAnswerPost.getPost();
        // remove and add operations on sets identify an AnswerPost by its id; to update a certain property of an existing answer post,
        // we need to remove the existing AnswerPost (based on unchanged id in updatedAnswerPost) and add the updatedAnswerPost afterwards
        updatedPost.removeAnswerPost(updatedAnswerPost);
        updatedPost.addAnswerPost(updatedAnswerPost);
    }
    postService.broadcastForPost(new MetisPostDTO(updatedPost, MetisPostAction.UPDATE_POST), course);
    reactionRepository.deleteById(reactionId);
}
Also used : User(de.tum.in.www1.artemis.domain.User) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) Post(de.tum.in.www1.artemis.domain.metis.Post) MetisPostDTO(de.tum.in.www1.artemis.web.websocket.dto.MetisPostDTO) Course(de.tum.in.www1.artemis.domain.Course) Reaction(de.tum.in.www1.artemis.domain.metis.Reaction) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) AccessForbiddenException(de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException)

Example 17 with Reaction

use of de.tum.in.www1.artemis.domain.metis.Reaction in project Artemis by ls1intum.

the class ReactionIntegrationTest method testDeletePostReaction.

@Test
@WithMockUser(username = "student2", roles = "USER")
public void testDeletePostReaction() throws Exception {
    // student 1 is the author of the post and student 2 reacts on this post
    Post postReactedOn = existingPostsWithAnswers.get(0);
    Reaction reactionToSaveOnPost = createReactionOnPost(postReactedOn);
    Reaction reactionToBeDeleted = request.postWithResponseBody("/api/courses/" + courseId + "/postings/reactions", reactionToSaveOnPost, Reaction.class, HttpStatus.CREATED);
    // student 2 deletes their reaction on this post
    request.delete("/api/courses/" + courseId + "/postings/reactions/" + reactionToBeDeleted.getId(), HttpStatus.OK);
    assertThat(postReactedOn.getReactions()).hasSameSizeAs(reactionRepository.findReactionsByPostId(postReactedOn.getId()));
    assertThat(reactionRepository.findById(reactionToBeDeleted.getId())).isEmpty();
}
Also used : AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) Post(de.tum.in.www1.artemis.domain.metis.Post) Reaction(de.tum.in.www1.artemis.domain.metis.Reaction) WithMockUser(org.springframework.security.test.context.support.WithMockUser) AbstractSpringIntegrationBambooBitbucketJiraTest(de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest) Test(org.junit.jupiter.api.Test)

Example 18 with Reaction

use of de.tum.in.www1.artemis.domain.metis.Reaction in project Artemis by ls1intum.

the class ReactionIntegrationTest method testGetPostsForCourse_OrderByVoteCountDESC.

// GET
@Test
@WithMockUser(username = "student1", roles = "USER")
public void testGetPostsForCourse_OrderByVoteCountDESC() throws Exception {
    PostSortCriterion sortCriterion = PostSortCriterion.VOTES;
    SortingOrder sortingOrder = SortingOrder.DESCENDING;
    User student1 = database.getUserByLogin("student1");
    User student2 = database.getUserByLogin("student2");
    // student 1 is the author of the post and reacts on this post
    Post postReactedOn = existingPostsWithAnswers.get(0);
    createVoteReactionOnPost(postReactedOn, student1);
    Post postReactedOn2 = existingPostsWithAnswers.get(1);
    createVoteReactionOnPost(postReactedOn2, student1);
    createVoteReactionOnPost(postReactedOn2, student2);
    // refresh posts after reactions are added
    existingPostsWithAnswers = postRepository.findPostsForCourse(courseId, null, false, false, false, null);
    var params = new LinkedMultiValueMap<String, String>();
    // ordering only available in course discussions page, where paging is enabled
    params.add("pagingEnabled", "true");
    params.add("page", "0");
    params.add("size", String.valueOf(MAX_POSTS_PER_PAGE));
    params.add("postSortCriterion", sortCriterion.toString());
    params.add("sortingOrder", sortingOrder.toString());
    List<Post> returnedPosts = request.getList("/api/courses/" + courseId + "/posts", HttpStatus.OK, Post.class, params);
    existingPostsWithAnswers.sort(Comparator.comparing((Post post) -> post.getReactions().stream().filter(reaction -> reaction.getEmojiId().equals(VOTE_EMOJI_ID)).count()).reversed());
    assertThat(returnedPosts).isEqualTo(existingPostsWithAnswers);
}
Also used : User(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) Post(de.tum.in.www1.artemis.domain.metis.Post) SortingOrder(de.tum.in.www1.artemis.domain.enumeration.SortingOrder) PostSortCriterion(de.tum.in.www1.artemis.domain.metis.PostSortCriterion) WithMockUser(org.springframework.security.test.context.support.WithMockUser) AbstractSpringIntegrationBambooBitbucketJiraTest(de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest) Test(org.junit.jupiter.api.Test)

Example 19 with Reaction

use of de.tum.in.www1.artemis.domain.metis.Reaction in project Artemis by ls1intum.

the class ReactionIntegrationTest method testDeleteOwnAnswerPostReaction.

@Test
@WithMockUser(username = "student1", roles = "USER")
public void testDeleteOwnAnswerPostReaction() throws Exception {
    // student 1 is the author of the post and reacts on this post
    AnswerPost answerPostReactedOn = existingAnswerPosts.get(0);
    Reaction reactionToSaveOnPost = createReactionOnAnswerPost(answerPostReactedOn);
    Reaction reactionToBeDeleted = request.postWithResponseBody("/api/courses/" + courseId + "/postings/reactions", reactionToSaveOnPost, Reaction.class, HttpStatus.CREATED);
    // student 1 deletes their reaction on this post
    request.delete("/api/courses/" + courseId + "/postings/reactions/" + reactionToBeDeleted.getId(), HttpStatus.OK);
    assertThat(answerPostReactedOn.getReactions()).hasSameSizeAs(reactionRepository.findReactionsByPostId(answerPostReactedOn.getId()));
    assertThat(reactionRepository.findById(reactionToBeDeleted.getId())).isEmpty();
}
Also used : Reaction(de.tum.in.www1.artemis.domain.metis.Reaction) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) WithMockUser(org.springframework.security.test.context.support.WithMockUser) AbstractSpringIntegrationBambooBitbucketJiraTest(de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest) Test(org.junit.jupiter.api.Test)

Example 20 with Reaction

use of de.tum.in.www1.artemis.domain.metis.Reaction in project Artemis by ls1intum.

the class ReactionIntegrationTest method testDeleteOwnPostReaction.

// DELETE
@Test
@WithMockUser(username = "student1", roles = "USER")
public void testDeleteOwnPostReaction() throws Exception {
    // student 1 is the author of the post and reacts on this post
    Post postReactedOn = existingPostsWithAnswers.get(0);
    Reaction reactionToSaveOnPost = createReactionOnPost(postReactedOn);
    Reaction reactionToBeDeleted = request.postWithResponseBody("/api/courses/" + courseId + "/postings/reactions", reactionToSaveOnPost, Reaction.class, HttpStatus.CREATED);
    // student 1 deletes their reaction on this post
    request.delete("/api/courses/" + courseId + "/postings/reactions/" + reactionToBeDeleted.getId(), HttpStatus.OK);
    assertThat(postReactedOn.getReactions()).hasSameSizeAs(reactionRepository.findReactionsByPostId(postReactedOn.getId()));
    assertThat(reactionRepository.findById(reactionToBeDeleted.getId())).isEmpty();
}
Also used : AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) Post(de.tum.in.www1.artemis.domain.metis.Post) Reaction(de.tum.in.www1.artemis.domain.metis.Reaction) WithMockUser(org.springframework.security.test.context.support.WithMockUser) AbstractSpringIntegrationBambooBitbucketJiraTest(de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest) Test(org.junit.jupiter.api.Test)

Aggregations

Reaction (de.tum.in.www1.artemis.domain.metis.Reaction)42 AnswerPost (de.tum.in.www1.artemis.domain.metis.AnswerPost)34 WithMockUser (org.springframework.security.test.context.support.WithMockUser)32 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)30 Test (org.junit.jupiter.api.Test)30 Post (de.tum.in.www1.artemis.domain.metis.Post)22 Course (de.tum.in.www1.artemis.domain.Course)10 User (de.tum.in.www1.artemis.domain.User)10 SortingOrder (de.tum.in.www1.artemis.domain.enumeration.SortingOrder)4 PostSortCriterion (de.tum.in.www1.artemis.domain.metis.PostSortCriterion)4 MetisPostDTO (de.tum.in.www1.artemis.web.websocket.dto.MetisPostDTO)4 ConstraintViolation (javax.validation.ConstraintViolation)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)4 VOTE_EMOJI_ID (de.tum.in.www1.artemis.config.Constants.VOTE_EMOJI_ID)2 Posting (de.tum.in.www1.artemis.domain.metis.Posting)2 UserRepository (de.tum.in.www1.artemis.repository.UserRepository)2 PostRepository (de.tum.in.www1.artemis.repository.metis.PostRepository)2 ReactionRepository (de.tum.in.www1.artemis.repository.metis.ReactionRepository)2 AccessForbiddenException (de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException)2 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)2