Search in sources :

Example 1 with Pin

use of com.vsct.vboard.models.Pin 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 Pin

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

the class LabelsController method getAllFromPins.

// Return all labels still on any pins
@RequestMapping(value = "/throughPins", method = RequestMethod.GET)
@ResponseBody
@Valid
public Set<Label> getAllFromPins() {
    Set<Label> labels = new HashSet<>();
    Iterable<Pin> pins = this.pinDAO.findAll();
    pins.forEach(p -> {
        if (!isBlank(p.getLabels())) {
            List<String> pinLabels = p.getLabelsAsList();
            pinLabels.forEach(l -> {
                labels.add(new Label(l));
                this.labelDAO.save(new Label(l));
            });
        }
    });
    return labels;
}
Also used : Pin(com.vsct.vboard.models.Pin) Label(com.vsct.vboard.models.Label) HashSet(java.util.HashSet) Valid(javax.validation.Valid) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with Pin

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

the class RssController method getFeedInRss.

@RequestMapping(value = "/rss", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
public ModelAndView getFeedInRss(@RequestParam(value = "text", required = false) String text, @RequestParam(value = "labels", required = false) String labels) {
    final String from = DateTimeFormat.forPattern("YYYY-MM-dd").print(DateTime.now().minusDays(7));
    final List<Pin> pins = elsClient.searchPins(text, labels, from, 0);
    final ModelAndView mav = new ModelAndView();
    mav.setView(new RssViewer(publicHostname));
    mav.addObject("feedContent", pins);
    return mav;
}
Also used : Pin(com.vsct.vboard.models.Pin) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Pin

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

the class ElasticSearchClient method jsonMapToPin.

private Pin jsonMapToPin(HashMap jsonMap) {
    final Pin pin = new Pin((String) jsonMap.get("pin_id"), (String) jsonMap.get("pin_title"), (String) jsonMap.get("href_url"), // ElasticSearch or jsonMap seems to integrate doubles by default whereas the parameter is an int
    jsonMap.get("likes") != null ? ((Double) jsonMap.get("likes")).intValue() : 0, (String) jsonMap.get("img_type"), (String) jsonMap.get("labels"), (String) jsonMap.get("indexable_text_content"), (String) jsonMap.get("author"), (String) jsonMap.get("post_date_utc"), jsonMap.get("comments_number") != null ? ((Double) jsonMap.get("comments_number")).intValue() : 0);
    ValidatorUtils.validate(pin);
    return pin;
}
Also used : Pin(com.vsct.vboard.models.Pin)

Example 5 with Pin

use of com.vsct.vboard.models.Pin 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)

Aggregations

Pin (com.vsct.vboard.models.Pin)18 Test (org.junit.Test)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 DateTime (org.joda.time.DateTime)11 Comment (com.vsct.vboard.models.Comment)6 VBoardException (com.vsct.vboard.models.VBoardException)4 ArrayList (java.util.ArrayList)4 Valid (javax.validation.Valid)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)3 User (com.vsct.vboard.models.User)2 LikeParams (com.vsct.vboard.parameterFormat.LikeParams)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Label (com.vsct.vboard.models.Label)1 AddNewPinParams (com.vsct.vboard.parameterFormat.AddNewPinParams)1 CommentParams (com.vsct.vboard.parameterFormat.CommentParams)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Request (org.apache.catalina.connector.Request)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1