Search in sources :

Example 1 with Comment

use of com.vsct.vboard.models.Comment in project vboard by voyages-sncf-technologies.

the class CommentsController method removeComment.

@RequestMapping(value = "", method = RequestMethod.DELETE)
@ResponseBody
@Valid
public Comment removeComment(@RequestParam(value = "id") String id) {
    Comment comment;
    try {
        comment = this.commentDAO.findById(id);
        if (comment != null) {
            // Check if the user can update this comment (or throw an exception)
            permission.ensureUserHasRightsToAlterPin(comment.getAuthor());
            this.commentDAO.delete(comment);
            String pinId = comment.getPinId();
            Pin pin = this.pinDAO.findByPinId(pinId);
            if (pin != null) {
                pin.decreaseCommentsNumber();
                this.pinDAO.save(pin);
            }
            // Decrease the number of comments for the given pin in elasticsearch
            this.elsClient.removeComment(pinId);
            this.logger.debug("deleteComment: id={}", id);
            // Update the stats
            this.gamification.updateStats(permission.getSessionUserWithSyncFromDB());
        } else {
            throw new VBoardException("Comment does not exist or already deleted");
        }
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 2 with Comment

use of com.vsct.vboard.models.Comment in project vboard by voyages-sncf-technologies.

the class CommentControllerTest method deleteComment.

@Test
public void deleteComment() {
    User.getEmailFromString(Mockito.anyString());
    this.pinDAO.save(new Pin("0", "title", "", 0, "", "", "content", "auth", new DateTime().toString(), 1));
    Comment comment = new Comment("id", "0", "auth", "comment", new DateTime().toString());
    Assert.assertFalse(this.commentDAO.findAll().iterator().hasNext());
    this.commentDAO.save(comment);
    Assert.assertTrue(this.commentDAO.findAll().iterator().hasNext());
    this.commentsController.removeComment("id");
    Assert.assertFalse(this.commentDAO.findAll().iterator().hasNext());
    Assert.assertEquals(0, this.pinDAO.findByPinId("0").getCommentsNumber());
    this.pinDAO.delete("0");
    comment = new Comment("id", "0", "auth", "comment", new DateTime().toString());
    Assert.assertFalse(this.commentDAO.findAll().iterator().hasNext());
    this.commentDAO.save(comment);
    Assert.assertTrue(this.commentDAO.findAll().iterator().hasNext());
    this.commentsController.removeComment("id");
    Assert.assertFalse(this.commentDAO.findAll().iterator().hasNext());
    Assert.assertNull(this.pinDAO.findByPinId("0"));
    this.pinDAO.save(new Pin("0", "title", "", 1, "", "", "content", "auth", new DateTime()));
    this.commentDAO.save(comment);
    Assert.assertTrue(this.commentDAO.findAll().iterator().hasNext());
    ArrayList<Pin> pins = new ArrayList<>();
    pins.add(this.pinDAO.findByPinId("0"));
    Mockito.doReturn(pins).when(elsClient).searchPinsById("0");
    this.pinsController.deletePinFromId("0");
    Assert.assertFalse(this.commentDAO.findAll().iterator().hasNext());
}
Also used : Comment(com.vsct.vboard.models.Comment) Pin(com.vsct.vboard.models.Pin) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with Comment

use of com.vsct.vboard.models.Comment in project vboard by voyages-sncf-technologies.

the class CommentControllerTest method updateComment.

@Test
public void updateComment() {
    this.pinDAO.save(new Pin("0", "title", "", 0, "", "", "content", "auth", new DateTime()));
    Comment comment = new Comment("id", "0", "auth", "comment", new DateTime().toString());
    this.commentDAO.save(comment);
    this.commentsController.updateComment("{\"text\": \"comment update\"}", "id");
    Comment updateComment = this.commentDAO.findById("id");
    Assert.assertNotEquals(comment, updateComment);
    Assert.assertEquals(comment.getAuthor(), updateComment.getAuthor());
    Assert.assertEquals(comment.getPostDateUTC(), updateComment.getPostDateUTC());
    Assert.assertEquals("comment update", updateComment.getText());
    Assert.assertEquals(0, this.pinDAO.findByPinId("0").getCommentsNumber());
}
Also used : Comment(com.vsct.vboard.models.Comment) Pin(com.vsct.vboard.models.Pin) DateTime(org.joda.time.DateTime) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with Comment

use of com.vsct.vboard.models.Comment in project vboard by voyages-sncf-technologies.

the class CommentsController method addComment.

@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
@Valid
public Comment addComment(@Valid @RequestBody CommentParams params) {
    String author = params.getAuthor();
    String pinId = params.getPinId();
    String text = params.getText();
    DateTime postDateUTC = new DateTime(DateTimeZone.UTC);
    Comment comment = new Comment(pinId, author, text, postDateUTC);
    // Check if the author given is the same as the session user (the author param could be deleted and replaced with the session one)
    permission.ensureNewEntityAuthorMatchesSessionUser(author);
    try {
        this.logger.debug("addComment: author={} - pin={} -text={}", author, pinId, text);
        this.commentDAO.save(comment);
        Pin pin = this.pinDAO.findByPinId(pinId);
        if (pin != null) {
            pin.increaseCommentsNumber();
            this.pinDAO.save(pin);
        }
        // Increase the number of comments for the given pin in elasticsearch
        this.elsClient.addComment(comment.getPinId());
        // Update the stats (for the user, and for the author of the pin where the comment has been added)
        this.gamification.updateStats(permission.getSessionUserWithSyncFromDB());
        if (pin != null && User.getEmailFromString(pin.getAuthor()).isPresent()) {
            User userAuthor = this.userDAO.findByEmail(User.getEmailFromString(pin.getAuthor()).get());
            if (userAuthor != null && userAuthor != permission.getSessionUserWithSyncFromDB()) {
                this.gamification.updateStats(userAuthor);
            }
        }
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    // Send comment notifications
    notifications.addNotificationsFromComment(pinId);
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) DateTime(org.joda.time.DateTime) Valid(javax.validation.Valid)

Example 5 with Comment

use of com.vsct.vboard.models.Comment in project vboard by voyages-sncf-technologies.

the class CommentsController method updateComment.

@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
@ResponseBody
@Valid
public Comment updateComment(@Valid @RequestBody String text, @PathVariable("id") String id) {
    text = JavaUtils.extractJSONObject(text, "text");
    Comment comment = this.commentDAO.findById(id);
    // Check if the user can update this comment (or throw an exception)
    permission.ensureUserHasRightsToAlterComment(comment.getAuthor());
    comment.setText(text);
    this.logger.debug("updateComment: id={} - text={}", id, text);
    this.commentDAO.save(comment);
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) Valid(javax.validation.Valid)

Aggregations

Comment (com.vsct.vboard.models.Comment)7 Pin (com.vsct.vboard.models.Pin)6 DateTime (org.joda.time.DateTime)5 Valid (javax.validation.Valid)4 VBoardException (com.vsct.vboard.models.VBoardException)3 Test (org.junit.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)3 User (com.vsct.vboard.models.User)2 ArrayList (java.util.ArrayList)2