1
1
package com .example .jpa .controller ;
2
2
3
- import com .example .jpa .exception .ResourceNotFoundExcption ;
3
+ import com .example .jpa .exception .ResourceNotFoundException ;
4
4
import com .example .jpa .model .Comment ;
5
5
import com .example .jpa .repository .CommentRepository ;
6
6
import com .example .jpa .repository .PostRepository ;
7
7
import org .springframework .beans .factory .annotation .Autowired ;
8
8
import org .springframework .data .domain .Page ;
9
- import org .springframework .data .domain .PageRequest ;
10
9
import org .springframework .data .domain .Pageable ;
11
- import org .springframework .data .domain .Sort ;
12
10
import org .springframework .http .ResponseEntity ;
13
11
import org .springframework .web .bind .annotation .*;
14
12
15
13
import javax .validation .Valid ;
16
- import java .util .List ;
17
14
18
15
@ RestController
19
16
public class CommentController {
@@ -36,33 +33,33 @@ public Comment createComment(@PathVariable (value = "postId") Long postId,
36
33
return postRepository .findById (postId ).map (post -> {
37
34
comment .setPost (post );
38
35
return commentRepository .save (comment );
39
- }).orElseThrow (() -> new ResourceNotFoundExcption ("PostId " + postId + " not found" ));
36
+ }).orElseThrow (() -> new ResourceNotFoundException ("PostId " + postId + " not found" ));
40
37
}
41
38
42
39
@ PutMapping ("/posts/{postId}/comments/{commentId}" )
43
40
public Comment updateComment (@ PathVariable (value = "postId" ) Long postId ,
44
41
@ PathVariable (value = "commentId" ) Long commentId ,
45
42
@ Valid @ RequestBody Comment commentRequest ) {
46
43
if (!postRepository .existsById (postId )) {
47
- throw new ResourceNotFoundExcption ("PostId " + postId + " not found" );
44
+ throw new ResourceNotFoundException ("PostId " + postId + " not found" );
48
45
}
49
46
50
47
return commentRepository .findById (commentId ).map (comment -> {
51
48
comment .setText (commentRequest .getText ());
52
49
return commentRepository .save (comment );
53
- }).orElseThrow (() -> new ResourceNotFoundExcption ("CommentId " + commentId + "not found" ));
50
+ }).orElseThrow (() -> new ResourceNotFoundException ("CommentId " + commentId + "not found" ));
54
51
}
55
52
56
53
@ DeleteMapping ("/posts/{postId}/comments/{commentId}" )
57
54
public ResponseEntity <?> deleteComment (@ PathVariable (value = "postId" ) Long postId ,
58
55
@ PathVariable (value = "commentId" ) Long commentId ) {
59
56
if (!postRepository .existsById (postId )) {
60
- throw new ResourceNotFoundExcption ("PostId " + postId + " not found" );
57
+ throw new ResourceNotFoundException ("PostId " + postId + " not found" );
61
58
}
62
59
63
60
return commentRepository .findById (commentId ).map (comment -> {
64
61
commentRepository .delete (comment );
65
62
return ResponseEntity .ok ().build ();
66
- }).orElseThrow (() -> new ResourceNotFoundExcption ("CommentId " + commentId + " not found" ));
63
+ }).orElseThrow (() -> new ResourceNotFoundException ("CommentId " + commentId + " not found" ));
67
64
}
68
65
}
0 commit comments