Search in sources :

Example 1 with Post

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

the class PostResource method getAllPosts.

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

Example 2 with Post

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

the class PostResource method getPost.

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

Example 3 with Post

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

the class PostResourceIntegrationTest method updatePost.

@Test
@Transactional
public void updatePost() throws Exception {
    // Initialize the database
    postRepository.saveAndFlush(post);
    int databaseSizeBeforeUpdate = postRepository.findAll().size();
    // Update the post
    Post updatedPost = postRepository.findOne(post.getId());
    updatedPost.title(UPDATED_TITLE).content(UPDATED_CONTENT).creationDate(UPDATED_CREATION_DATE);
    restPostMockMvc.perform(put("/api/posts").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(updatedPost))).andExpect(status().isOk());
    // Validate the Post in the database
    List<Post> postList = postRepository.findAll();
    assertThat(postList).hasSize(databaseSizeBeforeUpdate);
    Post testPost = postList.get(postList.size() - 1);
    assertThat(testPost.getTitle()).isEqualTo(UPDATED_TITLE);
    assertThat(testPost.getContent()).isEqualTo(UPDATED_CONTENT);
    assertThat(testPost.getCreationDate()).isEqualTo(UPDATED_CREATION_DATE);
}
Also used : Post(com.baeldung.domain.Post) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Post

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

the class PostResourceIntegrationTest method createPost.

@Test
@Transactional
public void createPost() throws Exception {
    int databaseSizeBeforeCreate = postRepository.findAll().size();
    // Create the Post
    restPostMockMvc.perform(post("/api/posts").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(post))).andExpect(status().isCreated());
    // Validate the Post in the database
    List<Post> postList = postRepository.findAll();
    assertThat(postList).hasSize(databaseSizeBeforeCreate + 1);
    Post testPost = postList.get(postList.size() - 1);
    assertThat(testPost.getTitle()).isEqualTo(DEFAULT_TITLE);
    assertThat(testPost.getContent()).isEqualTo(DEFAULT_CONTENT);
    assertThat(testPost.getCreationDate()).isEqualTo(DEFAULT_CREATION_DATE);
}
Also used : Post(com.baeldung.domain.Post) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Post

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

the class PostResourceIntegrationTest method createEntity.

/**
 * Create an entity for this test.
 *
 * This is a static method, as tests for other entities might also need it,
 * if they test an entity which requires the current entity.
 */
public static Post createEntity(EntityManager em) {
    Post post = new Post().title(DEFAULT_TITLE).content(DEFAULT_CONTENT).creationDate(DEFAULT_CREATION_DATE);
    // Add required entity
    User creator = UserResourceIntTest.createEntity(em);
    em.persist(creator);
    em.flush();
    post.setCreator(creator);
    return post;
}
Also used : User(com.baeldung.domain.User) Post(com.baeldung.domain.Post)

Aggregations

Post (com.baeldung.domain.Post)8 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 Comment (com.baeldung.domain.Comment)1 User (com.baeldung.domain.User)1 URI (java.net.URI)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1