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;
}
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);
}
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);
}
Aggregations