-
Notifications
You must be signed in to change notification settings - Fork 0
게시물 신고 기능 구현 #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
itzjb
wants to merge
4
commits into
develop
Choose a base branch
from
feat/post-report
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
게시물 신고 기능 구현 #36
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/com/command/itdaserver/domain/post/domain/Report.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.command.itdaserver.domain.post.domain; | ||
|
|
||
| import com.command.itdaserver.domain.post.domain.enums.ReportReason; | ||
| import com.command.itdaserver.domain.user.domain.User; | ||
| import com.command.itdaserver.global.entity.BaseIdEntity; | ||
| import jakarta.persistence.*; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.hibernate.annotations.CreationTimestamp; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class Report extends BaseIdEntity { | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "post_id", nullable = false) | ||
| private Post post; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "reporter_id", nullable = false) | ||
| private User reporter; | ||
|
|
||
| @NotNull | ||
| @Enumerated(EnumType.STRING) | ||
| @Column(nullable = false) | ||
| private ReportReason reason; | ||
|
|
||
| @Size(max = 300) | ||
| private String detail; | ||
|
|
||
| @CreationTimestamp | ||
| @Column(nullable = false, name = "created_at") | ||
| private LocalDateTime createdAt; | ||
|
|
||
| public static Report create(Post post, User reporter, ReportReason reason, String detail) { | ||
| Report report = new Report(); | ||
| report.post = post; | ||
| report.reporter = reporter; | ||
| report.reason = reason; | ||
| report.detail = detail; | ||
| return report; | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/main/java/com/command/itdaserver/domain/post/domain/enums/ReportReason.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.command.itdaserver.domain.post.domain.enums; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum ReportReason { | ||
|
|
||
| SPAM("광고·홍보·스팸"), | ||
| HATE_SPEECH("욕설·비하·혐오 표현"), | ||
| OBSCENITY("음란·불쾌한 내용"), | ||
| IRRELEVANT("공고 목적과 맞지 않는 내용"), | ||
| OTHER("기타"); | ||
|
|
||
| private final String displayName; | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/command/itdaserver/domain/post/domain/repository/ReportRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.command.itdaserver.domain.post.domain.repository; | ||
|
|
||
| import com.command.itdaserver.domain.post.domain.Report; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface ReportRepository extends JpaRepository<Report, Long> { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...java/com/command/itdaserver/domain/post/presentation/dto/request/CreateReportRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.command.itdaserver.domain.post.presentation.dto.request; | ||
|
|
||
| import com.command.itdaserver.domain.post.domain.enums.ReportReason; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class CreateReportRequest { | ||
|
|
||
| @NotNull(message = "신고 사유는 필수입니다.") | ||
| private ReportReason reason; | ||
|
|
||
| @Size(max = 300, message = "상세설명은 최대 300자까지 입력 가능합니다.") | ||
| private String detail; | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/command/itdaserver/domain/post/service/CreateReportService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.command.itdaserver.domain.post.service; | ||
|
|
||
| import com.command.itdaserver.domain.post.domain.Post; | ||
| import com.command.itdaserver.domain.post.domain.Report; | ||
| import com.command.itdaserver.domain.post.domain.repository.PostRepository; | ||
| import com.command.itdaserver.domain.post.domain.repository.ReportRepository; | ||
| import com.command.itdaserver.domain.post.exceptions.PostNotFoundException; | ||
| import com.command.itdaserver.domain.post.presentation.dto.request.CreateReportRequest; | ||
| import com.command.itdaserver.domain.user.domain.User; | ||
| import com.command.itdaserver.domain.user.domain.repository.UserRepository; | ||
| import com.command.itdaserver.domain.user.exception.UserNotFoundException; | ||
| import com.command.itdaserver.global.auth.CustomUserDetails; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class CreateReportService { | ||
|
|
||
| private final PostRepository postRepository; | ||
| private final ReportRepository reportRepository; | ||
| private final UserRepository userRepository; | ||
|
|
||
| @Transactional | ||
| public void execute(Long postId, CreateReportRequest request, CustomUserDetails userDetails) { | ||
|
|
||
| Post post = postRepository.findById(postId) | ||
| .orElseThrow(() -> PostNotFoundException.EXCEPTION); | ||
|
|
||
| User reporter = userRepository.findByUserId(userDetails.getUserId()) | ||
| .orElseThrow(() -> UserNotFoundException.EXCEPTION); | ||
|
|
||
| Report report = Report.create(post, reporter, request.getReason(), request.getDetail()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 신고 기능에 대한 몇 가지 중요한 검증 로직이 누락된 것으로 보입니다. 아래 두 가지 검증을 추가하는 것을 강력히 권장합니다.
아래와 같이 // 자신의 게시물 신고 방지
if (post.getWriter().getId().equals(reporter.getId())) {
// 적절한 예외(e.g., 400 Bad Request)를 발생시켜야 합니다.
throw new SelfReportNotAllowedException("자신의 게시물은 신고할 수 없습니다.");
}
// 중복 신고 방지 (ReportRepository에 existsByPostAndReporter 메서드 추가 필요)
if (reportRepository.existsByPostAndReporter(post, reporter)) {
// 적절한 예외(e.g., 409 Conflict)를 발생시켜야 합니다.
throw new DuplicateReportException("이미 신고한 게시물입니다.");
}위의 중복 신고 방지 로직을 위해 // ReportRepository.java
boolean existsByPostAndReporter(Post post, User reporter); |
||
| reportRepository.save(report); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재의 정적 팩토리 메서드는
new Report()로 빈 객체를 생성한 후 필드를 수동으로 할당하고 있습니다. 이 방식보다는 빌더 패턴을 사용하여 객체의 불변성을 보장하고 생성 로직을 더 명확하게 만드는 것이 좋습니다.@Builder어노테이션을 클래스에 추가하고, 이create메서드를 제거하는 것을 고려해보세요. 서비스 레이어에서는 빌더를 사용하여Report객체를 생성하게 됩니다.Report.java수정 제안:CreateReportService.java에서의 사용 예시: