Search in sources :

Example 11 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 12 with Reaction

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

the class ReactionIntegrationTest method testDeletePostReactionOfOthers_forbidden.

@Test
@WithMockUser(username = "student1", roles = "USER")
public void testDeletePostReactionOfOthers_forbidden() throws Exception {
    // student 1 is the author of the post and student 2 reacts on this post
    Post postReactedOn = existingPostsWithAnswers.get(0);
    Reaction reactionSaveOnPost = saveReactionOfOtherUserOnPost(postReactedOn);
    // student 1 wants to delete the reaction of student 2
    request.delete("/api/courses/" + courseId + "/postings/reactions/" + reactionSaveOnPost.getId(), HttpStatus.FORBIDDEN);
    assertThat(postReactedOn.getReactions()).hasSize(reactionRepository.findReactionsByPostId(postReactedOn.getId()).size() - 1);
}
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 13 with Reaction

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

the class AnswerPostService method updateWithReaction.

/**
 * Add reaction to an answer post and persist the answer post
 *
 * @param answerPost answer post that is reacted on
 * @param reaction   reaction that was added by a user
 * @param courseId   id of the course the answer post belongs to
 */
public void updateWithReaction(AnswerPost answerPost, Reaction reaction, Long courseId) {
    final Course course = preCheckUserAndCourse(reaction.getUser(), courseId);
    answerPost.addReaction(reaction);
    AnswerPost updatedAnswerPost = answerPostRepository.save(answerPost);
    this.preparePostAndBroadcast(updatedAnswerPost, course);
}
Also used : Course(de.tum.in.www1.artemis.domain.Course) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost)

Example 14 with Reaction

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

the class PostService method updateWithReaction.

/**
 * Add reaction to a post and persist the post
 *
 * @param post     post that is reacted on
 * @param reaction reaction that was added by a user
 * @param courseId id of course the post belongs to
 */
public void updateWithReaction(Post post, Reaction reaction, Long courseId) {
    final Course course = preCheckUserAndCourse(reaction.getUser(), courseId);
    post.addReaction(reaction);
    Post updatedPost = postRepository.save(post);
    broadcastForPost(new MetisPostDTO(updatedPost, MetisPostAction.UPDATE_POST), course);
}
Also used : 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)

Example 15 with Reaction

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

the class ReactionService method createReaction.

/**
 * Checks reaction validity, determines the reaction's user,
 * retrieves the associated posting and persists the mutual association
 *
 * @param courseId if of course the according posting belongs to
 * @param reaction reaction to create
 * @return created reaction that was persisted
 */
public Reaction createReaction(Long courseId, Reaction reaction) {
    Posting posting = reaction.getPost() == null ? reaction.getAnswerPost() : reaction.getPost();
    // checks
    final User user = this.userRepository.getUserWithGroupsAndAuthorities();
    if (reaction.getId() != null) {
        throw new BadRequestAlertException("A new reaction cannot already have an ID", METIS_REACTION_ENTITY_NAME, "idexists");
    }
    // set user to current user
    reaction.setUser(user);
    // we query the repository dependent on the type of posting and update this posting
    Reaction savedReaction;
    if (posting instanceof Post) {
        Post post = postService.findById(posting.getId());
        reaction.setPost(post);
        // save reaction
        savedReaction = reactionRepository.save(reaction);
        // save post
        postService.updateWithReaction(post, reaction, courseId);
    } else {
        AnswerPost answerPost = answerPostService.findById(posting.getId());
        reaction.setAnswerPost(answerPost);
        // save reaction
        savedReaction = reactionRepository.save(reaction);
        // save answer post
        answerPostService.updateWithReaction(answerPost, reaction, courseId);
    }
    return savedReaction;
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) User(de.tum.in.www1.artemis.domain.User) Posting(de.tum.in.www1.artemis.domain.metis.Posting) 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) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost)

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