Search in sources :

Example 1 with Posting

use of de.tum.in.www1.artemis.domain.metis.Posting 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)

Example 2 with Posting

use of de.tum.in.www1.artemis.domain.metis.Posting 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 3 with Posting

use of de.tum.in.www1.artemis.domain.metis.Posting 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)

Example 4 with Posting

use of de.tum.in.www1.artemis.domain.metis.Posting 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)

Aggregations

User (de.tum.in.www1.artemis.domain.User)4 AnswerPost (de.tum.in.www1.artemis.domain.metis.AnswerPost)4 Post (de.tum.in.www1.artemis.domain.metis.Post)4 Reaction (de.tum.in.www1.artemis.domain.metis.Reaction)4 Course (de.tum.in.www1.artemis.domain.Course)2 Posting (de.tum.in.www1.artemis.domain.metis.Posting)2 AccessForbiddenException (de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException)2 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)2 MetisPostDTO (de.tum.in.www1.artemis.web.websocket.dto.MetisPostDTO)2