Search in sources :

Example 21 with AnswerPost

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

the class AnswerPostService method deleteAnswerPostById.

/**
 * Checks course and user validity,
 * determines authority to delete post and deletes the post
 *
 * @param courseId     id of the course the answer post belongs to
 * @param answerPostId id of the answer post to delete
 */
public void deleteAnswerPostById(Long courseId, Long answerPostId) {
    final User user = userRepository.getUserWithGroupsAndAuthorities();
    // checks
    final Course course = preCheckUserAndCourse(user, courseId);
    AnswerPost answerPost = answerPostRepository.findByIdElseThrow(answerPostId);
    mayUpdateOrDeletePostingElseThrow(answerPost, user, course);
    // delete
    answerPostRepository.deleteById(answerPostId);
    // we need to explicitly remove the answer post from the answers of the broadcast post to share up-to-date information
    Post updatedPost = answerPost.getPost();
    updatedPost.removeAnswerPost(answerPost);
    broadcastForPost(new MetisPostDTO(updatedPost, MetisPostAction.UPDATE_POST), course);
}
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) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost)

Example 22 with AnswerPost

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

the class AnswerPostService method createAnswerPost.

/**
 * Checks course, user and answer post and associated post validity,
 * determines the associated post, the answer post's author,
 * sets to approved if author is at least a tutor,
 * persists the answer post, and sends a notification to affected user groups
 *
 * @param courseId   id of the course the answer post belongs to
 * @param answerPost answer post to create
 * @return created answer post that was persisted
 */
public AnswerPost createAnswerPost(Long courseId, AnswerPost answerPost) {
    final User user = this.userRepository.getUserWithGroupsAndAuthorities();
    // check
    if (answerPost.getId() != null) {
        throw new BadRequestAlertException("A new answer post cannot already have an ID", METIS_ANSWER_POST_ENTITY_NAME, "idexists");
    }
    final Course course = preCheckUserAndCourse(user, courseId);
    Post post = postRepository.findByIdElseThrow(answerPost.getPost().getId());
    // use post from database rather than user input
    answerPost.setPost(post);
    // set author to current user
    answerPost.setAuthor(user);
    // on creation of an answer post, we set the resolves_post field to false per default
    answerPost.setResolvesPost(false);
    AnswerPost savedAnswerPost = answerPostRepository.save(answerPost);
    this.preparePostAndBroadcast(savedAnswerPost, course);
    sendNotification(post, answerPost, course);
    return savedAnswerPost;
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) 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) Course(de.tum.in.www1.artemis.domain.Course) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost)

Example 23 with AnswerPost

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

the class AnswerPostService method preparePostAndBroadcast.

/**
 * Helper method to prepare the post included in the websocket message and initiate the broadcasting
 *
 * @param updatedAnswerPost answer post that was updated
 * @param course            course the answer post belongs to
 */
private void preparePostAndBroadcast(AnswerPost updatedAnswerPost, Course course) {
    // we need to explicitly (and newly) add the updated answer post to the answers of the broadcast post to share up-to-date information
    Post 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);
    broadcastForPost(new MetisPostDTO(updatedPost, MetisPostAction.UPDATE_POST), course);
}
Also used : 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)

Example 24 with AnswerPost

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

the class ReactionIntegrationTest method testCreateExistingReaction_badRequest.

@Test
@WithMockUser(username = "student1", roles = "USER")
public void testCreateExistingReaction_badRequest() throws Exception {
    // student 1 is the author of the answer post and reacts on this answer post
    AnswerPost answerPostReactedOn = existingAnswerPosts.get(0);
    Reaction reactionToSaveOnAnswerPost = createReactionOnAnswerPost(answerPostReactedOn);
    Reaction createdReaction = request.postWithResponseBody("/api/courses/" + courseId + "/postings/reactions", reactionToSaveOnAnswerPost, Reaction.class, HttpStatus.CREATED);
    request.postWithResponseBody("/api/courses/" + courseId + "/postings/reactions", createdReaction, Reaction.class, HttpStatus.BAD_REQUEST);
    checkCreatedReaction(reactionToSaveOnAnswerPost, createdReaction);
    assertThat(answerPostReactedOn.getReactions()).hasSize(reactionRepository.findReactionsByAnswerPostId(answerPostReactedOn.getId()).size() - 1);
}
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 25 with AnswerPost

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

the class ReactionIntegrationTest method testCreateMultipleOwnAnswerPostReaction_internalServerError.

@Test
@WithMockUser(username = "student1", roles = "USER")
public void testCreateMultipleOwnAnswerPostReaction_internalServerError() throws Exception {
    // student 1 is the author of the answer post and reacts on this answer post
    AnswerPost answerPostReactedOn = existingAnswerPosts.get(0);
    Reaction reactionToSaveOnAnswerPost = createReactionOnAnswerPost(answerPostReactedOn);
    Reaction createdReaction = request.postWithResponseBody("/api/courses/" + courseId + "/postings/reactions", reactionToSaveOnAnswerPost, Reaction.class, HttpStatus.CREATED);
    checkCreatedReaction(reactionToSaveOnAnswerPost, createdReaction);
    assertThat(answerPostReactedOn.getReactions()).hasSize(reactionRepository.findReactionsByAnswerPostId(answerPostReactedOn.getId()).size() - 1);
    // try again
    request.postWithResponseBody("/api/courses/" + courseId + "/postings/reactions", reactionToSaveOnAnswerPost, Reaction.class, HttpStatus.INTERNAL_SERVER_ERROR);
}
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)

Aggregations

AnswerPost (de.tum.in.www1.artemis.domain.metis.AnswerPost)71 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)48 Test (org.junit.jupiter.api.Test)48 WithMockUser (org.springframework.security.test.context.support.WithMockUser)48 Reaction (de.tum.in.www1.artemis.domain.metis.Reaction)18 Post (de.tum.in.www1.artemis.domain.metis.Post)16 Course (de.tum.in.www1.artemis.domain.Course)12 User (de.tum.in.www1.artemis.domain.User)10 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)6 MetisPostDTO (de.tum.in.www1.artemis.web.websocket.dto.MetisPostDTO)6 ExerciseGroup (de.tum.in.www1.artemis.domain.exam.ExerciseGroup)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4 Exam (de.tum.in.www1.artemis.domain.exam.Exam)2 Posting (de.tum.in.www1.artemis.domain.metis.Posting)2 PlagiarismCase (de.tum.in.www1.artemis.domain.plagiarism.PlagiarismCase)2 PlagiarismComparison (de.tum.in.www1.artemis.domain.plagiarism.PlagiarismComparison)2 PlagiarismSubmission (de.tum.in.www1.artemis.domain.plagiarism.PlagiarismSubmission)2 TextPlagiarismResult (de.tum.in.www1.artemis.domain.plagiarism.text.TextPlagiarismResult)2 AccessForbiddenException (de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException)2 MimeMessage (javax.mail.internet.MimeMessage)2