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();
}
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);
}
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);
}
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);
}
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;
}
Aggregations