Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

@EnableJpaAuditing
@SpringBootApplication
public class RealCodingServerApplication {

public class RealCodingServerApplication
public static void main(String[] args) {
SpringApplication.run(RealCodingServerApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ public ResponseEntity<Void> deletePost(@PathVariable("postId") Integer postId) {

return ResponseEntity.noContent().build();
}

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException(ResourceNotFoundException exception) {
return exception.getMessage();
}
}
24 changes: 14 additions & 10 deletions src/main/java/com/cnu/real_coding_server/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ public List<Post> getPosts() {
}

public Optional<Post> getPost(Integer postId) {
return postRepository.findById(postId);
return postRepository.findById(postId)
.orElseThrow(() -> new ResourceNotFoundException("Post not found with id: " + postId));
}

public Optional<Post> updatePost(Integer postId, PostRequest postRequest) {
return postRepository.findById(postId)
.map(post -> {
post.setTitle(postRequest.getTitle());
post.setContents(postRequest.getContents());
post.setTag(postRequest.getTag());
return postRepository.save(post);
});
Post post = postRepository.findById(postId)
.orElseThrow(() -> new ResourceNotFoundException("Post not found with id: " + postId));

post.setTitle(postRequest.getTitle());
post.setContents(postRequest.getContents());
post.setTag(postRequest.getTag());

return Optional.of(postRepository.save(post));
}

public void deletePost(Integer postId) {
postRepository.findById(postId)
.ifPresent(postRepository::delete);
Post post = postRepository.findById(postId)
.orElseThrow(() -> new ResourceNotFoundException("Post not found with id: " + postId));

postRepository.delete(post);
}
}