-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from Domitory-CheckMate/feature/39-report
[feat] 게시물, 채팅 신고
- Loading branch information
Showing
9 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/org/gachon/checkmate/domain/report/controller/ReportController.java
This file contains 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 org.gachon.checkmate.domain.report.controller; | ||
|
||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.gachon.checkmate.domain.report.dto.request.ChatRoomReportRequestDto; | ||
import org.gachon.checkmate.domain.report.dto.request.PostReportRequestDto; | ||
import org.gachon.checkmate.domain.report.service.ReportService; | ||
import org.gachon.checkmate.global.common.SuccessResponse; | ||
import org.gachon.checkmate.global.config.auth.UserId; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/report") | ||
@RestController | ||
public class ReportController { | ||
|
||
private final ReportService reportService; | ||
|
||
@PostMapping("/post") | ||
public ResponseEntity<SuccessResponse<?>> reportPost(@UserId final Long userId, | ||
@RequestBody @Valid final PostReportRequestDto requestDto) { | ||
reportService.reportPost(userId, requestDto); | ||
return SuccessResponse.created(null); | ||
} | ||
|
||
@PostMapping("/chat-room") | ||
public ResponseEntity<SuccessResponse<?>> reportChatRoom(@UserId final Long userId, | ||
@RequestBody @Valid final ChatRoomReportRequestDto requestDto) { | ||
reportService.reportChatRoom(userId, requestDto); | ||
return SuccessResponse.created(null); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/gachon/checkmate/domain/report/dto/request/ChatRoomReportRequestDto.java
This file contains 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 org.gachon.checkmate.domain.report.dto.request; | ||
|
||
public record ChatRoomReportRequestDto( | ||
String chatRoomId, | ||
String reason | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/gachon/checkmate/domain/report/dto/request/PostReportRequestDto.java
This file contains 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,10 @@ | ||
package org.gachon.checkmate.domain.report.dto.request; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record PostReportRequestDto ( | ||
@NotNull(message = "신고할 게시물의 ID를 입력해주세요.") Long postId, | ||
@NotEmpty(message = "신고할 이유를 입력해주세요") String reason | ||
) { | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/gachon/checkmate/domain/report/entity/ChatRoomReport.java
This file contains 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 org.gachon.checkmate.domain.report.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import org.gachon.checkmate.domain.chat.entity.ChatRoom; | ||
import org.gachon.checkmate.domain.member.entity.User; | ||
import org.gachon.checkmate.domain.post.entity.Post; | ||
import org.gachon.checkmate.domain.scrap.entity.Scrap; | ||
import org.gachon.checkmate.global.common.BaseTimeEntity; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@Getter | ||
@Entity | ||
public class ChatRoomReport extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "chat_room_report_id") | ||
private Long id; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "chat_room_id") | ||
private ChatRoom chatRoom; | ||
@Column(name = "reason") | ||
private String reason; | ||
|
||
public static ChatRoomReport createChatRoomReport(User user, ChatRoom chatRoom, String reason) { | ||
return ChatRoomReport.builder() | ||
.user(user) | ||
.chatRoom(chatRoom) | ||
.reason(reason) | ||
.build(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/gachon/checkmate/domain/report/entity/PostReport.java
This file contains 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,36 @@ | ||
package org.gachon.checkmate.domain.report.entity; | ||
|
||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import org.gachon.checkmate.domain.member.entity.User; | ||
import org.gachon.checkmate.domain.post.entity.Post; | ||
import org.gachon.checkmate.global.common.BaseTimeEntity; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@Getter | ||
@Entity | ||
public class PostReport extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "post_report_id") | ||
private Long id; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "post_id") | ||
private Post post; | ||
@Column(name = "reason") | ||
private String reason; | ||
|
||
public static PostReport createPostReport(User user, Post post, String reason) { | ||
return PostReport.builder() | ||
.user(user) | ||
.post(post) | ||
.reason(reason) | ||
.build(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/org/gachon/checkmate/domain/report/repository/ChatRoomReportRepository.java
This file contains 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,9 @@ | ||
package org.gachon.checkmate.domain.report.repository; | ||
|
||
import org.gachon.checkmate.domain.report.entity.ChatRoomReport; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ChatRoomReportRepository extends JpaRepository<ChatRoomReport, Long> { | ||
|
||
Boolean existsChatRoomReportByUserIdAndChatRoomId(Long userId, String chatRoomId); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/gachon/checkmate/domain/report/repository/PostReportRepository.java
This file contains 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,11 @@ | ||
package org.gachon.checkmate.domain.report.repository; | ||
|
||
import org.gachon.checkmate.domain.report.entity.PostReport; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface PostReportRepository extends JpaRepository<PostReport, Long> { | ||
|
||
Boolean existsPostReportByUserIdAndPostId(Long userId, Long postId); | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/org/gachon/checkmate/domain/report/service/ReportService.java
This file contains 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,88 @@ | ||
package org.gachon.checkmate.domain.report.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.gachon.checkmate.domain.chat.entity.ChatRoom; | ||
import org.gachon.checkmate.domain.chat.repository.ChatRoomRepository; | ||
import org.gachon.checkmate.domain.member.entity.User; | ||
import org.gachon.checkmate.domain.member.repository.UserRepository; | ||
import org.gachon.checkmate.domain.post.entity.Post; | ||
import org.gachon.checkmate.domain.post.repository.PostRepository; | ||
import org.gachon.checkmate.domain.report.dto.request.ChatRoomReportRequestDto; | ||
import org.gachon.checkmate.domain.report.dto.request.PostReportRequestDto; | ||
import org.gachon.checkmate.domain.report.entity.ChatRoomReport; | ||
import org.gachon.checkmate.domain.report.entity.PostReport; | ||
import org.gachon.checkmate.domain.report.repository.ChatRoomReportRepository; | ||
import org.gachon.checkmate.domain.report.repository.PostReportRepository; | ||
import org.gachon.checkmate.global.error.exception.ConflictException; | ||
import org.gachon.checkmate.global.error.exception.EntityNotFoundException; | ||
import org.gachon.checkmate.global.error.exception.ForbiddenException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static org.gachon.checkmate.domain.report.entity.ChatRoomReport.*; | ||
import static org.gachon.checkmate.domain.report.entity.PostReport.*; | ||
import static org.gachon.checkmate.global.error.ErrorCode.*; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional | ||
@Service | ||
public class ReportService { | ||
|
||
private final PostReportRepository postReportRepository; | ||
private final ChatRoomReportRepository chatRoomReportRepository; | ||
private final UserRepository userRepository; | ||
private final PostRepository postRepository; | ||
private final ChatRoomRepository chatRoomRepository; | ||
|
||
public void reportPost(Long userId, PostReportRequestDto requestDto) { | ||
User user = getUserOrThrow(userId); | ||
Post post = getPostOrThrow(requestDto.postId()); | ||
validateUserAlreadyReportPost(user, post); | ||
PostReport postReport = createPostReport(user, post, requestDto.reason()); | ||
postReportRepository.save(postReport); | ||
} | ||
|
||
public void reportChatRoom(Long userId, ChatRoomReportRequestDto requestDto) { | ||
User user = getUserOrThrow(userId); | ||
ChatRoom chatRoom = getChatRoomOrThrow(requestDto.chatRoomId()); | ||
validateUserChatRoomMember(user, chatRoom); | ||
validateUserAlreadyReportChatRoom(user, chatRoom); | ||
ChatRoomReport chatRoomReport = createChatRoomReport(user, chatRoom, requestDto.reason()); | ||
chatRoomReportRepository.save(chatRoomReport); | ||
} | ||
|
||
private void validateUserChatRoomMember(User user, ChatRoom chatRoom) { | ||
if(!chatRoom.getFirstMemberId().equals(user.getId()) && | ||
!chatRoom.getSecondMemberId().equals(user.getId())) { | ||
throw new ForbiddenException(NOT_CHATROOM_USER); | ||
} | ||
} | ||
|
||
private void validateUserAlreadyReportChatRoom(User user, ChatRoom chatRoom) { | ||
if(chatRoomReportRepository.existsChatRoomReportByUserIdAndChatRoomId(user.getId(), chatRoom.getId())) { | ||
throw new ConflictException(DUPLICATE_CHATROOM_REPORT); | ||
} | ||
} | ||
|
||
private void validateUserAlreadyReportPost(User user, Post post) { | ||
if(postReportRepository.existsPostReportByUserIdAndPostId(user.getId(), post.getId())) { | ||
throw new ConflictException(DUPLICATE_POST_REPORT); | ||
} | ||
} | ||
|
||
private User getUserOrThrow(Long userId) { | ||
return userRepository.findById(userId) | ||
.orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND)); | ||
} | ||
|
||
private Post getPostOrThrow(Long postId) { | ||
return postRepository.findById(postId) | ||
.orElseThrow(() -> new EntityNotFoundException(POST_NOT_FOUND)); | ||
} | ||
|
||
private ChatRoom getChatRoomOrThrow(String chatRoomId) { | ||
return chatRoomRepository.findById(chatRoomId) | ||
.orElseThrow(() -> new EntityNotFoundException(CHATROOM_NOT_FOUND)); | ||
} | ||
|
||
} |
This file contains 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