Search in sources :

Example 6 with Post

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

the class CommentResourceIntegrationTest 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 Comment createEntity(EntityManager em) {
    Comment comment = new Comment().text(DEFAULT_TEXT).creationDate(DEFAULT_CREATION_DATE);
    // Add required entity
    Post post = PostResourceIntTest.createEntity(em);
    em.persist(post);
    em.flush();
    comment.setPost(post);
    return comment;
}
Also used : Comment(com.baeldung.domain.Comment) Post(com.baeldung.domain.Post)

Example 7 with Post

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

the class PostResource method createPost.

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

Example 8 with Post

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

the class PostResource method updatePost.

/**
 * PUT  /posts : Updates an existing post.
 *
 * @param post the post to update
 * @return the ResponseEntity with status 200 (OK) and with body the updated post,
 * or with status 400 (Bad Request) if the post is not valid,
 * or with status 500 (Internal Server Error) if the post couldnt be updated
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PutMapping("/posts")
@Timed
public ResponseEntity<Post> updatePost(@Valid @RequestBody Post post) throws URISyntaxException {
    log.debug("REST request to update Post : {}", post);
    if (post.getId() == null) {
        return createPost(post);
    }
    Post result = postRepository.save(post);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, post.getId().toString())).body(result);
}
Also used : Post(com.baeldung.domain.Post) Timed(com.codahale.metrics.annotation.Timed)

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