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