Skip to content

Commit 45efdcf

Browse files
authored
Merge pull request #21 from KONKUK-MAP-Service/Comment
[chore]: 댓글 삭제 완료 #21
2 parents e434de5 + 3f77074 commit 45efdcf

14 files changed

+262
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.cona.KUsukKusuk.comment.controller;
2+
3+
import com.cona.KUsukKusuk.comment.domain.Comment;
4+
import com.cona.KUsukKusuk.comment.dto.*;
5+
import com.cona.KUsukKusuk.comment.exception.CommentNotFoundException;
6+
import com.cona.KUsukKusuk.comment.exception.CommentUserNotMatchedException;
7+
import com.cona.KUsukKusuk.comment.service.CommentService;
8+
import com.cona.KUsukKusuk.global.response.HttpResponse;
9+
import com.cona.KUsukKusuk.spot.domain.Spot;
10+
import com.cona.KUsukKusuk.user.domain.User;
11+
import com.cona.KUsukKusuk.user.service.UserService;
12+
import io.swagger.v3.oas.annotations.Operation;
13+
import org.springframework.web.bind.annotation.*;
14+
15+
16+
@RestController
17+
@RequestMapping("comment")
18+
public class CommentController {
19+
final private CommentService commentService;
20+
final private UserService userService;
21+
private CommentJoinRequest CommentJoinRequest;
22+
23+
public CommentController(CommentService commentService, UserService userService) {
24+
this.commentService = commentService;
25+
this.userService = userService;
26+
}
27+
28+
@PostMapping("/{spotId}/register")
29+
@Operation(summary = "특정 장소 글에 댓글 등록", description = "로그인한 사용자의 댓글을 등록합니다.")
30+
public HttpResponse<CommentJoinResponse> saveComment(@PathVariable("spotId") Long spotId, @RequestBody CommentJoinRequest commentJoinRequest){
31+
//user, spot 가져오기 - 수정 필요
32+
User user = commentService.getCurrentUser();
33+
Spot spot = commentService.getCurrentSpot(spotId);
34+
//위 user, spot 사용해서 comment 객체 만들기
35+
System.out.println("user, spot 까지 담기 완료");
36+
Comment comment = commentJoinRequest.toEntity(user, spot);
37+
System.out.println("comment 객체 생성 완료");
38+
Comment savedComment = commentService.save(comment);
39+
return HttpResponse.okBuild(
40+
CommentJoinResponse.of(savedComment));
41+
}
42+
43+
@PostMapping("/{spotId}/{commentId}/update")
44+
@Operation(summary = "특정 장소 글의 댓글 수정", description = "로그인한 사용자의 자신의 댓글을 수정합니다.")
45+
public HttpResponse<CommentUpdateResponse> updateComment(@PathVariable("spotId")Long spotId, @PathVariable("commentId")Long commentId, @RequestBody CommentUpdateRequest commentUpdateRequest) throws CommentNotFoundException, CommentUserNotMatchedException {
46+
47+
String commentUserName = userService.getUsernameBySecurityContext();
48+
49+
50+
Spot spot = commentService.getCurrentSpot(spotId);
51+
Comment updateComment = commentService.getCurrentComment(commentUserName,spot,commentId);
52+
updateComment.setComment(commentUpdateRequest.comment());
53+
54+
Comment savedComment = commentService.save(updateComment);
55+
return HttpResponse.okBuild(
56+
CommentUpdateResponse.of(updateComment)
57+
);
58+
}
59+
60+
@GetMapping("/{spotId}/{commentId}/delete")
61+
@Operation(summary = "특정 장소 글의 댓글 삭제" , description = "로그인한 사용자의 자신의 댓글을 삭제합니다.")
62+
public HttpResponse<CommentDeleteResponse> deleteComment(@PathVariable("spotId")Long spotId, @PathVariable("commentId")Long commentId) throws CommentNotFoundException, CommentUserNotMatchedException {
63+
64+
String commentUserName = userService.getUsernameBySecurityContext();
65+
Spot spot = commentService.getCurrentSpot(spotId);
66+
Comment comment = commentService.getCurrentComment(commentUserName,spot,commentId);
67+
68+
commentService.delete(comment);
69+
return HttpResponse.okBuild(
70+
CommentDeleteResponse.of("댓글이 삭제 되었습니다.")
71+
);
72+
}
73+
74+
75+
}

src/main/java/com/cona/KUsukKusuk/comment/Comment.java src/main/java/com/cona/KUsukKusuk/comment/domain/Comment.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.cona.KUsukKusuk.comment;
1+
package com.cona.KUsukKusuk.comment.domain;
22

33
import com.cona.KUsukKusuk.global.domain.BaseEntity;
44
import com.cona.KUsukKusuk.spot.domain.Spot;
@@ -10,15 +10,16 @@
1010
import jakarta.persistence.Id;
1111
import jakarta.persistence.JoinColumn;
1212
import jakarta.persistence.ManyToOne;
13-
import lombok.AllArgsConstructor;
14-
import lombok.Getter;
15-
import lombok.NoArgsConstructor;
16-
import lombok.Setter;
13+
import lombok.*;
14+
15+
import java.sql.ConnectionBuilder;
1716

1817
@Entity
18+
@Builder
1919
@Getter
2020
@Setter
2121
@NoArgsConstructor
22+
@AllArgsConstructor
2223
public class Comment extends BaseEntity {
2324
@Id
2425
@GeneratedValue(strategy = GenerationType.IDENTITY)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cona.KUsukKusuk.comment.dto;
2+
3+
import com.cona.KUsukKusuk.spot.dto.SpotDeleteResponse;
4+
import lombok.Builder;
5+
6+
@Builder
7+
public record CommentDeleteResponse (
8+
String message
9+
){
10+
public static CommentDeleteResponse of(String message) {
11+
return CommentDeleteResponse.builder()
12+
.message(message)
13+
.build();
14+
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cona.KUsukKusuk.comment.dto;
2+
3+
import com.cona.KUsukKusuk.comment.domain.Comment;
4+
import com.cona.KUsukKusuk.spot.domain.Spot;
5+
import com.cona.KUsukKusuk.user.domain.User;
6+
import io.swagger.v3.oas.annotations.media.Schema;
7+
import jakarta.validation.constraints.NotNull;
8+
9+
public record CommentJoinRequest (
10+
@NotNull(message = "댓글은 필수 입력값입니다.")
11+
@Schema(description = "댓글", nullable = false, example = "")
12+
String comment
13+
14+
){
15+
public Comment toEntity(User u, Spot s) {
16+
return Comment.builder()
17+
.user(u)
18+
.spot(s)
19+
.comment(comment)
20+
.build();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cona.KUsukKusuk.comment.dto;
2+
3+
import com.cona.KUsukKusuk.comment.domain.Comment;
4+
import com.cona.KUsukKusuk.spot.domain.Spot;
5+
import com.cona.KUsukKusuk.spot.dto.SpotJoinResponse;
6+
import com.cona.KUsukKusuk.user.domain.User;
7+
import lombok.Builder;
8+
9+
@Builder
10+
public record CommentJoinResponse (
11+
Long id, User user, Spot spot, String comment
12+
)
13+
{
14+
public static CommentJoinResponse of(Comment comment){
15+
return CommentJoinResponse.builder()
16+
.id(comment.getId())
17+
.user(comment.getUser())
18+
.spot(comment.getSpot())
19+
.comment(comment.getComment())
20+
.build();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.cona.KUsukKusuk.comment.dto;
2+
3+
import lombok.Builder;
4+
5+
@Builder
6+
public record CommentUpdateRequest (
7+
String comment
8+
){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cona.KUsukKusuk.comment.dto;
2+
3+
import com.cona.KUsukKusuk.comment.domain.Comment;
4+
import com.cona.KUsukKusuk.spot.domain.Spot;
5+
import com.cona.KUsukKusuk.spot.dto.SpotUpdatResponse;
6+
import com.cona.KUsukKusuk.user.domain.User;
7+
import lombok.Builder;
8+
9+
@Builder
10+
public record CommentUpdateResponse (
11+
Long id, User user, Spot spot, String comment
12+
13+
){
14+
public static CommentUpdateResponse of(Comment comment){
15+
return CommentUpdateResponse.builder()
16+
.id(comment.getId())
17+
.user(comment.getUser())
18+
.spot(comment.getSpot())
19+
.comment(comment.getComment())
20+
.build();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.cona.KUsukKusuk.comment.exception;
2+
3+
public class CommentNotFoundException extends Throwable {
4+
public CommentNotFoundException(String s) {
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.cona.KUsukKusuk.comment.exception;
2+
3+
public class CommentUserNotMatchedException extends Throwable {
4+
public CommentUserNotMatchedException(String s) {
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.cona.KUsukKusuk.comment.repository;
22

3-
import com.cona.KUsukKusuk.comment.Comment;
3+
import com.cona.KUsukKusuk.comment.domain.Comment;
44
import com.cona.KUsukKusuk.user.domain.User;
55
import org.springframework.data.jpa.repository.JpaRepository;
66

7-
public interface CommentRepositofy extends JpaRepository<Comment, Long> {
7+
public interface CommentRepository extends JpaRepository<Comment, Long> {
88
void deleteAllByUser(User user);
99

1010
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.cona.KUsukKusuk.comment.service;
2+
3+
import com.cona.KUsukKusuk.comment.domain.Comment;
4+
import com.cona.KUsukKusuk.comment.exception.CommentNotFoundException;
5+
import com.cona.KUsukKusuk.comment.exception.CommentUserNotMatchedException;
6+
import com.cona.KUsukKusuk.comment.repository.CommentRepository;
7+
import com.cona.KUsukKusuk.spot.domain.Spot;
8+
import com.cona.KUsukKusuk.spot.exception.SpotNotFoundException;
9+
import com.cona.KUsukKusuk.spot.repository.SpotRepository;
10+
import com.cona.KUsukKusuk.user.domain.User;
11+
import com.cona.KUsukKusuk.user.service.UserService;
12+
import jakarta.validation.constraints.NotNull;
13+
import lombok.NonNull;
14+
import org.springframework.stereotype.Service;
15+
16+
import java.util.List;
17+
import java.util.Optional;
18+
19+
@Service
20+
public class CommentService {
21+
private final CommentRepository commentRepository;
22+
private final SpotRepository spotRepository;
23+
private final UserService userService;
24+
public CommentService(CommentRepository commentRepository, SpotRepository spotRepository, UserService userService) {
25+
this.commentRepository = commentRepository;
26+
this.spotRepository = spotRepository;
27+
this.userService = userService;
28+
}
29+
public Comment save(Comment comment) {
30+
Comment savedComment = commentRepository.save(comment);
31+
return savedComment;
32+
}
33+
34+
public User getCurrentUser() {
35+
String name = userService.getUsernameBySecurityContext();
36+
User user = userService.findUserByUserid(name);
37+
return user;
38+
}
39+
40+
public Spot getCurrentSpot(Long spotId) {
41+
Spot spot = spotRepository.findById(spotId)
42+
.orElseThrow(() -> new SpotNotFoundException());
43+
return spot;
44+
}
45+
46+
public Comment getCurrentComment(String commentUserName , Spot spot, Long commentId) throws CommentNotFoundException, CommentUserNotMatchedException {
47+
List<Comment> commentList = spot.getComments();
48+
Comment wantToUpdate = null; // 초기화를 null로 설정
49+
for (Comment comment : commentList) {
50+
if (comment.getId().equals(commentId)) { // spot 내의 comment인지 확인
51+
wantToUpdate = comment;
52+
break; // 원하는 comment를 찾았으므로 루프 종료
53+
}
54+
}
55+
56+
// wantToUpdate가 여전히 null인 경우 예외 처리
57+
if (wantToUpdate == null) {
58+
throw new CommentNotFoundException("Comment with id: " + commentId + " not found.");
59+
}
60+
61+
//commentUserName과 comment의 작성자 일치 확인
62+
if (wantToUpdate.getUser().getNickname().equals(commentUserName))
63+
return wantToUpdate;
64+
else
65+
throw new CommentUserNotMatchedException("Don't have authority to update the comment.");
66+
67+
}
68+
69+
public void delete(Comment comment) {
70+
commentRepository.delete(comment);
71+
}
72+
}

src/main/java/com/cona/KUsukKusuk/spot/domain/Spot.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.cona.KUsukKusuk.spot.domain;
22

33
import com.cona.KUsukKusuk.bookmark.domain.Bookmark;
4-
import com.cona.KUsukKusuk.comment.Comment;
4+
import com.cona.KUsukKusuk.comment.domain.Comment;
55
import com.cona.KUsukKusuk.global.domain.BaseEntity;
66
import com.cona.KUsukKusuk.like.UserLike;
77
import com.cona.KUsukKusuk.picture.Picture;

src/main/java/com/cona/KUsukKusuk/user/domain/User.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.cona.KUsukKusuk.user.domain;
22

33
import com.cona.KUsukKusuk.bookmark.domain.Bookmark;
4-
import com.cona.KUsukKusuk.comment.Comment;
4+
import com.cona.KUsukKusuk.comment.domain.Comment;
55
import com.cona.KUsukKusuk.global.domain.BaseEntity;
66
import com.cona.KUsukKusuk.like.UserLike;
77
import com.cona.KUsukKusuk.spot.domain.Spot;

src/main/java/com/cona/KUsukKusuk/user/service/UserService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.cona.KUsukKusuk.bookmark.domain.Bookmark;
44
import com.cona.KUsukKusuk.bookmark.repository.BookmarkRepository;
5-
import com.cona.KUsukKusuk.comment.Comment;
6-
import com.cona.KUsukKusuk.comment.repository.CommentRepositofy;
5+
import com.cona.KUsukKusuk.comment.domain.Comment;
6+
import com.cona.KUsukKusuk.comment.repository.CommentRepository;
77
import com.cona.KUsukKusuk.email.service.EmailService;
88
import com.cona.KUsukKusuk.global.exception.HttpExceptionCode;
99
import com.cona.KUsukKusuk.global.exception.custom.security.IncorrectRefreshTokenException;
@@ -37,7 +37,7 @@
3737
@RequiredArgsConstructor
3838
public class UserService {
3939
private final UserRepository userRepository;
40-
private final CommentRepositofy commentRepository;
40+
private final CommentRepository commentRepository;
4141
private final SpotRepository spotRepository;
4242
private final UserLikeRepository userLikeRepository;
4343
private final BookmarkRepository bookmarkRepository;

0 commit comments

Comments
 (0)