Search in sources :

Example 1 with Comment

use of com.baeldung.domain.Comment in project tutorials by eugenp.

the class CommentResource method getComment.

/**
 * GET  /comments/:id : get the "id" comment.
 *
 * @param id the id of the comment to retrieve
 * @return the ResponseEntity with status 200 (OK) and with body the comment, or with status 404 (Not Found)
 */
@GetMapping("/comments/{id}")
@Timed
public ResponseEntity<Comment> getComment(@PathVariable Long id) {
    log.debug("REST request to get Comment : {}", id);
    Comment comment = commentRepository.findOne(id);
    return ResponseUtil.wrapOrNotFound(Optional.ofNullable(comment));
}
Also used : Comment(com.baeldung.domain.Comment) Timed(com.codahale.metrics.annotation.Timed)

Example 2 with Comment

use of com.baeldung.domain.Comment in project tutorials by eugenp.

the class CommentResource method getAllComments.

/**
 * GET  /comments : get all the comments.
 *
 * @param pageable the pagination information
 * @return the ResponseEntity with status 200 (OK) and the list of comments in body
 * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
 */
@GetMapping("/comments")
@Timed
public ResponseEntity<List<Comment>> getAllComments(@ApiParam Pageable pageable) {
    log.debug("REST request to get a page of Comments");
    Page<Comment> page = commentRepository.findAll(pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/comments");
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
Also used : Comment(com.baeldung.domain.Comment) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) Timed(com.codahale.metrics.annotation.Timed)

Example 3 with Comment

use of com.baeldung.domain.Comment in project tutorials by eugenp.

the class CommentResource method createComment.

/**
 * POST  /comments : Create a new comment.
 *
 * @param comment the comment to create
 * @return the ResponseEntity with status 201 (Created) and with body the new comment, or with status 400 (Bad Request) if the comment has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/comments")
@Timed
public ResponseEntity<Comment> createComment(@Valid @RequestBody Comment comment) throws URISyntaxException {
    log.debug("REST request to save Comment : {}", comment);
    if (comment.getId() != null) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new comment cannot already have an ID")).body(null);
    }
    Comment result = commentRepository.save(comment);
    return ResponseEntity.created(new URI("/api/comments/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : Comment(com.baeldung.domain.Comment) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed)

Example 4 with Comment

use of com.baeldung.domain.Comment in project tutorials by eugenp.

the class CommentResourceIntegrationTest method updateComment.

@Test
@Transactional
public void updateComment() throws Exception {
    // Initialize the database
    commentRepository.saveAndFlush(comment);
    int databaseSizeBeforeUpdate = commentRepository.findAll().size();
    // Update the comment
    Comment updatedComment = commentRepository.findOne(comment.getId());
    updatedComment.text(UPDATED_TEXT).creationDate(UPDATED_CREATION_DATE);
    restCommentMockMvc.perform(put("/api/comments").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(updatedComment))).andExpect(status().isOk());
    // Validate the Comment in the database
    List<Comment> commentList = commentRepository.findAll();
    assertThat(commentList).hasSize(databaseSizeBeforeUpdate);
    Comment testComment = commentList.get(commentList.size() - 1);
    assertThat(testComment.getText()).isEqualTo(UPDATED_TEXT);
    assertThat(testComment.getCreationDate()).isEqualTo(UPDATED_CREATION_DATE);
}
Also used : Comment(com.baeldung.domain.Comment) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Comment

use of com.baeldung.domain.Comment in project tutorials by eugenp.

the class CommentResourceIntegrationTest method createComment.

@Test
@Transactional
public void createComment() throws Exception {
    int databaseSizeBeforeCreate = commentRepository.findAll().size();
    // Create the Comment
    restCommentMockMvc.perform(post("/api/comments").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(comment))).andExpect(status().isCreated());
    // Validate the Comment in the database
    List<Comment> commentList = commentRepository.findAll();
    assertThat(commentList).hasSize(databaseSizeBeforeCreate + 1);
    Comment testComment = commentList.get(commentList.size() - 1);
    assertThat(testComment.getText()).isEqualTo(DEFAULT_TEXT);
    assertThat(testComment.getCreationDate()).isEqualTo(DEFAULT_CREATION_DATE);
}
Also used : Comment(com.baeldung.domain.Comment) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Comment (com.baeldung.domain.Comment)7 Timed (com.codahale.metrics.annotation.Timed)4 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Post (com.baeldung.domain.Post)1 URI (java.net.URI)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1