From d2c1a34e5585ac95c7331abbc4f5ad4864753aea Mon Sep 17 00:00:00 2001 From: gkwon Date: Fri, 8 Mar 2024 17:29:53 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=EB=8C=93=EA=B8=80=20=EC=8B=A0?= =?UTF-8?q?=EA=B3=A0=20API=20with=20=ED=8C=A8=EB=84=90=ED=8B=B0=20?= =?UTF-8?q?=EB=B6=80=EC=97=AC=20=EB=A1=9C=EC=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/gg/data/party/CommentReport.java | 7 ++ .../main/java/gg/data/party/PartyPenalty.java | 20 ++++ gg-data/src/main/java/gg/data/party/Room.java | 20 ++-- .../report/controller/ReportController.java | 24 +++- ...eportRoomReqDto.java => ReportReqDto.java} | 2 +- .../user/report/service/ReportService.java | 105 ++++++++++++++++-- .../user/report/utils/PartyGivePenalty.java | 49 -------- .../api/user/room/service/RoomService.java | 6 +- .../repo/party/CommentReportRepository.java | 15 +++ .../gg/repo/party/PartyPenaltyRepository.java | 9 ++ .../gg/repo/party/UserRoomRepository.java | 4 + .../java/gg/utils/exception/ErrorCode.java | 3 +- .../party/AlredayReportedException.java | 10 ++ .../party/AlredayReportedRoomException.java | 10 -- .../exception/party/SelfReportException.java | 10 ++ 15 files changed, 202 insertions(+), 92 deletions(-) rename gg-pingpong-api/src/main/java/gg/party/api/user/report/request/{ReportRoomReqDto.java => ReportReqDto.java} (87%) delete mode 100644 gg-pingpong-api/src/main/java/gg/party/api/user/report/utils/PartyGivePenalty.java create mode 100644 gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java create mode 100644 gg-repo/src/main/java/gg/repo/party/PartyPenaltyRepository.java create mode 100644 gg-utils/src/main/java/gg/utils/exception/party/AlredayReportedException.java delete mode 100644 gg-utils/src/main/java/gg/utils/exception/party/AlredayReportedRoomException.java create mode 100644 gg-utils/src/main/java/gg/utils/exception/party/SelfReportException.java diff --git a/gg-data/src/main/java/gg/data/party/CommentReport.java b/gg-data/src/main/java/gg/data/party/CommentReport.java index bd51e7872..c447c454b 100644 --- a/gg-data/src/main/java/gg/data/party/CommentReport.java +++ b/gg-data/src/main/java/gg/data/party/CommentReport.java @@ -37,4 +37,11 @@ public class CommentReport extends BaseTimeEntity { @Column(length = 100) private String message; + + public CommentReport(User reporter, Comment comment, Room room, String message) { + this.reporter = reporter; + this.comment = comment; + this.room = room; + this.message = message; + } } diff --git a/gg-data/src/main/java/gg/data/party/PartyPenalty.java b/gg-data/src/main/java/gg/data/party/PartyPenalty.java index 85467d016..677a51973 100644 --- a/gg-data/src/main/java/gg/data/party/PartyPenalty.java +++ b/gg-data/src/main/java/gg/data/party/PartyPenalty.java @@ -45,4 +45,24 @@ public class PartyPenalty extends BaseTimeEntity { @NotNull @Column(name = "penalty_time") private Integer penaltyTime; + + public PartyPenalty(User user, String penaltyType, String message, LocalDateTime startTime, Integer penaltyTime) { + this.user = user; + this.penaltyType = penaltyType; + this.message = message; + this.startTime = startTime; + this.penaltyTime = penaltyTime; + } + + public void updatePenaltyTime(Integer penaltyTime) { + this.penaltyTime = penaltyTime; + } + + public void updateMessage(String message) { + this.message = message; + } + + public void updatePenaltyType(String penaltyType) { + this.penaltyType = penaltyType; + } } diff --git a/gg-data/src/main/java/gg/data/party/Room.java b/gg-data/src/main/java/gg/data/party/Room.java index e52ac60c7..240493365 100644 --- a/gg-data/src/main/java/gg/data/party/Room.java +++ b/gg-data/src/main/java/gg/data/party/Room.java @@ -73,14 +73,6 @@ public class Room extends BaseTimeEntity { @Column(name = "status") private RoomType status; - public void updateCurrentPeople(int currentPeople) { - this.currentPeople = currentPeople; - } - - public void updateCategory(Category category) { - this.category = category; - } - @Builder public Room(User host, User creator, Category category, String title, String content, Integer currentPeople, Integer maxPeople, Integer minPeople, LocalDateTime dueDate, RoomType status) { @@ -96,15 +88,19 @@ public Room(User host, User creator, Category category, String title, String con this.status = status; } - public void updateStatus(RoomType status) { - this.status = status; + public void updateCurrentPeople(int currentPeople) { + this.currentPeople = currentPeople; } - public void updateHost(User host) { - this.host = host; + public void updateCategory(Category category) { + this.category = category; } public void updateRoomStatus(RoomType status) { this.status = status; } + + public void updateHost(User host) { + this.host = host; + } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/report/controller/ReportController.java b/gg-pingpong-api/src/main/java/gg/party/api/user/report/controller/ReportController.java index 815c579ad..68b3efc80 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/report/controller/ReportController.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/report/controller/ReportController.java @@ -12,7 +12,7 @@ import gg.auth.UserDto; import gg.auth.argumentresolver.Login; -import gg.party.api.user.report.request.ReportRoomReqDto; +import gg.party.api.user.report.request.ReportReqDto; import gg.party.api.user.report.service.ReportService; import io.swagger.v3.oas.annotations.Parameter; import lombok.RequiredArgsConstructor; @@ -25,15 +25,29 @@ public class ReportController { /** * 방을 신고한다. - * @param reportRoomReqDto 신고 내용 + * @param reportReqDto 신고 내용 * @param roomId 방 번호 - * @return 참여중인 방 전체 List + * @return roomId */ @PostMapping("/rooms/{room_id}") public ResponseEntity reportRoomAdd(@PathVariable("room_id") Long roomId, - @RequestBody @Valid ReportRoomReqDto reportRoomReqDto, + @RequestBody @Valid ReportReqDto reportReqDto, @Parameter(hidden = true) @Login UserDto user) { return ResponseEntity.status(HttpStatus.CREATED) - .body(reportService.addReportRoom(roomId, reportRoomReqDto, user)); + .body(reportService.addReportRoom(roomId, reportReqDto, user)); + } + + /** + * 댓글을 신고한다. + * @param reportReqDto 신고 내용 + * @param commentId 댓글 번호 + * @return commentId + */ + @PostMapping("/comments/{comment_id}") + public ResponseEntity reportCommentAdd(@PathVariable("comment_id") Long commentId, + @RequestBody @Valid ReportReqDto reportReqDto, + @Parameter(hidden = true) @Login UserDto user) { + return ResponseEntity.status(HttpStatus.CREATED) + .body(reportService.addReportComment(commentId, reportReqDto, user)); } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/report/request/ReportRoomReqDto.java b/gg-pingpong-api/src/main/java/gg/party/api/user/report/request/ReportReqDto.java similarity index 87% rename from gg-pingpong-api/src/main/java/gg/party/api/user/report/request/ReportRoomReqDto.java rename to gg-pingpong-api/src/main/java/gg/party/api/user/report/request/ReportReqDto.java index 40dd50d39..5f0440759 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/report/request/ReportRoomReqDto.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/report/request/ReportReqDto.java @@ -7,7 +7,7 @@ @Getter @NoArgsConstructor -public class ReportRoomReqDto { +public class ReportReqDto { @Size(min = 1, max = 200) private String content; } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/report/service/ReportService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/report/service/ReportService.java index c3840faf0..60534b2c8 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/report/service/ReportService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/report/service/ReportService.java @@ -1,6 +1,8 @@ package gg.party.api.user.report.service; +import java.time.LocalDateTime; import java.util.List; +import java.util.Objects; import java.util.Optional; import javax.transaction.Transactional; @@ -8,57 +10,138 @@ import org.springframework.stereotype.Service; import gg.auth.UserDto; +import gg.data.party.Comment; +import gg.data.party.CommentReport; +import gg.data.party.PartyPenalty; import gg.data.party.Room; import gg.data.party.RoomReport; +import gg.data.party.UserRoom; import gg.data.party.type.RoomType; import gg.data.user.User; -import gg.party.api.user.report.request.ReportRoomReqDto; +import gg.party.api.user.report.request.ReportReqDto; +import gg.repo.party.CommentReportRepository; +import gg.repo.party.CommentRepository; +import gg.repo.party.PartyPenaltyRepository; import gg.repo.party.RoomReportRepository; import gg.repo.party.RoomRepository; import gg.repo.party.UserRoomRepository; import gg.repo.user.UserRepository; -import gg.utils.exception.party.AlredayReportedRoomException; +import gg.utils.exception.party.AlredayReportedException; +import gg.utils.exception.party.CommentNotFoundException; import gg.utils.exception.party.RoomNotFoundException; +import gg.utils.exception.party.SelfReportException; import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor public class ReportService { private final RoomRepository roomRepository; + private final CommentRepository commentRepository; private final UserRepository userRepository; - private final UserRoomRepository userRoomRepository; private final RoomReportRepository roomReportRepository; + private final CommentReportRepository commentReportRepository; + private final PartyPenaltyRepository partyPenaltyRepository; + private final UserRoomRepository userRoomRepository; /** * 방을 신고한다. * @param roomId 방 번호 - * @param reportRoomReqDto 신고 내용 + * @param reportReqDto 신고 내용 * @param user 신고자 * @exception RoomNotFoundException 방을 찾을 수 없음 - * @exception AlredayReportedRoomException 이미 신고한 방 + * @exception AlredayReportedException 이미 신고한 경우 * @return 방 번호 */ @Transactional - public Long addReportRoom(Long roomId, ReportRoomReqDto reportRoomReqDto, UserDto user) { + public Long addReportRoom(Long roomId, ReportReqDto reportReqDto, UserDto user) { Room targetRoom = roomRepository.findById(roomId) .orElseThrow(RoomNotFoundException::new); User userEntity = userRepository.findById(user.getId()).get(); + if (Objects.equals(user.getId(), targetRoom.getCreator().getId())) { + throw new SelfReportException(); + } Optional existingReport = roomReportRepository.findByReporterAndRoomId(userEntity, targetRoom.getId()); if (existingReport.isPresent()) { - throw new AlredayReportedRoomException(); + throw new AlredayReportedException(); } RoomReport roomReport = new RoomReport(userEntity, targetRoom.getCreator(), targetRoom, - reportRoomReqDto.getContent()); + reportReqDto.getContent()); roomReportRepository.save(roomReport); List allReportRoom = roomReportRepository.findByRoomId(targetRoom.getId()); if (allReportRoom.size() == 3) { - targetRoom.updateStatus(RoomType.HIDDEN); + targetRoom.updateRoomStatus(RoomType.HIDDEN); roomRepository.save(targetRoom); + User targetUser = targetRoom.getCreator(); + partyGivePenalty(targetUser.getIntraId(), 24, "방 패널티"); } - // 사용자 정지 - //partyGivePenalty(targetRoom.getCreator().getIntraId(), 24, "신고 3회 이상"); return roomId; } + + /** + * 댓글을 신고한다. + * @param commentId 방 번호 + * @param reportReqDto 신고 내용 + * @param user 신고자 + * @exception CommentNotFoundException 방을 찾을 수 없음 + * @exception AlredayReportedException 이미 신고한 경우 + * @return 방 번호 + */ + @Transactional + public Long addReportComment(Long commentId, ReportReqDto reportReqDto, UserDto user) { + Comment targetComment = commentRepository.findById(commentId) + .orElseThrow(CommentNotFoundException::new); + User userEntity = userRepository.findById(user.getId()).get(); + if (Objects.equals(user.getId(), targetComment.getUser().getId())) { + throw new SelfReportException(); + } + Optional existingReport = commentReportRepository.findByReporterAndCommentId(userEntity, + targetComment.getId()); + if (existingReport.isPresent()) { + throw new AlredayReportedException(); + } + Room targetRoom = roomRepository.findById(targetComment.getRoom().getId()) + .orElseThrow(RoomNotFoundException::new); + CommentReport commentReport = new CommentReport(userEntity, targetComment, targetRoom, + reportReqDto.getContent()); + commentReportRepository.save(commentReport); + + List allReportComment = commentReportRepository.findByCommentId(targetComment.getId()); + if (allReportComment.size() == 3) { + targetComment.updateHidden(true); + commentRepository.save(targetComment); + User targetUser = targetComment.getUser(); + partyGivePenalty(targetUser.getIntraId(), 1, "댓글 패널티"); + } + return commentId; + } + + /** + * 패널티 부여 + * @param intraId 신고당한 유저 아이디 + * @param penaltyTime 패널티 시간 + * @param penaltyType 패널티 타입 + */ + @Transactional + public void partyGivePenalty(String intraId, Integer penaltyTime, String penaltyType) { + User user = userRepository.findByIntraId(intraId).get(); + PartyPenalty pPenalty = partyPenaltyRepository.findByUserId(user.getId()); + if (pPenalty != null) { + pPenalty.updatePenaltyTime(pPenalty.getPenaltyTime() + penaltyTime * 60); + pPenalty.updateMessage(pPenalty.getMessage() + penaltyType); + pPenalty.updatePenaltyType("복합"); + partyPenaltyRepository.save(pPenalty); + } else { + partyPenaltyRepository.save(new PartyPenalty( + user, penaltyType, penaltyType, LocalDateTime.now(), penaltyTime * 60)); + List userRoomList = userRoomRepository.findOpenRoomsByUserId(user.getId()); + for (Room room : userRoomList) { + UserRoom userRoom = userRoomRepository.findByUserIdAndRoomIdAndIsExistTrue(user.getId(), room.getId()) + .orElseThrow(RoomNotFoundException::new); + userRoom.updateIsExist(false); + userRoomRepository.save(userRoom); + } + } + } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/report/utils/PartyGivePenalty.java b/gg-pingpong-api/src/main/java/gg/party/api/user/report/utils/PartyGivePenalty.java deleted file mode 100644 index 13783bd47..000000000 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/report/utils/PartyGivePenalty.java +++ /dev/null @@ -1,49 +0,0 @@ -// package gg.party.api.user.report.utils; -// -// import java.time.LocalDateTime; -// import java.util.Optional; -// -// import org.springframework.transaction.annotation.Transactional; -// -// import gg.data.manage.Penalty; -// import gg.data.manage.redis.RedisPenaltyUser; -// import gg.data.manage.type.PenaltyType; -// import gg.data.user.User; -// import gg.repo.user.UserRepository; -// -// public class PartyGivePenalty { -// -// private final UserRepository userRepository; -// -// public PartyGivePenalty(UserRepository userRepository) { -// this.userRepository = userRepository; -// } -// -// /** -// * 패널티 부여 -// * @param reporteeId 신고당한 유저 아이디 -// * @param roomId 방 번호 -// */ -// @Transactional -// public void partyGivePenalty(String intraId, Integer penaltyTime, String reason) { -// User user = userRepository.findByIntraId(intraId).get(); -// Optional redisPenaltyUser = penaltyUserAdminRedisRepository.findByIntraId(intraId); -// LocalDateTime releaseTime; -// RedisPenaltyUser penaltyUser; -// Penalty penalty; -// LocalDateTime now = LocalDateTime.now(); -// if (redisPenaltyUser.isPresent()) { -// releaseTime = redisPenaltyUser.get().getReleaseTime().plusHours(penaltyTime); -// penaltyUser = new RedisPenaltyUser(intraId, redisPenaltyUser.get().getPenaltyTime() + penaltyTime * 60, -// releaseTime, redisPenaltyUser.get().getStartTime(), reason); -// penalty = new Penalty(user, PenaltyType.NOSHOW, reason, redisPenaltyUser.get().getReleaseTime(), -// penaltyTime * 60); -// } else { -// releaseTime = now.plusHours(penaltyTime); -// penaltyUser = new RedisPenaltyUser(intraId, penaltyTime * 60, releaseTime, now, reason); -// penalty = new Penalty(user, PenaltyType.NOSHOW, reason, now, penaltyTime * 60); -// } -// penaltyRepository.save(penalty); -// penaltyUserAdminRedisRepository.addPenaltyUser(penaltyUser, releaseTime); -// } -// } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java index 4fed88888..d80b4bfb0 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java @@ -161,7 +161,7 @@ public LeaveRoomResDto modifyLeaveRoom(Long roomId, UserDto user) { // 모두 나갈 때 방 fail처리 if (targetRoom.getCurrentPeople() == 1) { targetRoom.updateCurrentPeople(0); - targetRoom.updateStatus(RoomType.FAIL); + targetRoom.updateRoomStatus(RoomType.FAIL); targetUserRoom.updateIsExist(false); roomRepository.save(targetRoom); userRoomRepository.save(targetUserRoom); @@ -209,7 +209,7 @@ public Long modifyStartRoom(Long roomId, UserDto user) { if (targetRoom.getHost() != targetUserRoom.getUser()) { throw new UserNotHostException(); } - targetRoom.updateStatus(RoomType.START); + targetRoom.updateRoomStatus(RoomType.START); roomRepository.save(targetRoom); return roomId; @@ -282,7 +282,7 @@ public RoomJoinResDto addJoinRoom(Long roomId, UserDto userDto) { newUserRoom.updateIsExist(false); return newUserRoom; }); - if (userRoom.getIsExist() == true) { + if (userRoom.getIsExist()) { throw new UserAlreadyInRoom(ErrorCode.USER_ALREADY_IN_ROOM); } else { userRoom.updateIsExist(true); diff --git a/gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java b/gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java new file mode 100644 index 000000000..70dc9f715 --- /dev/null +++ b/gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java @@ -0,0 +1,15 @@ +package gg.repo.party; + +import java.util.List; +import java.util.Optional; + +import org.springframework.data.jpa.repository.JpaRepository; + +import gg.data.party.CommentReport; +import gg.data.user.User; + +public interface CommentReportRepository extends JpaRepository { + public List findByCommentId(Long commentId); + + public Optional findByReporterAndCommentId(User reporter, Long commentId); +} diff --git a/gg-repo/src/main/java/gg/repo/party/PartyPenaltyRepository.java b/gg-repo/src/main/java/gg/repo/party/PartyPenaltyRepository.java new file mode 100644 index 000000000..7ec1aba49 --- /dev/null +++ b/gg-repo/src/main/java/gg/repo/party/PartyPenaltyRepository.java @@ -0,0 +1,9 @@ +package gg.repo.party; + +import org.springframework.data.jpa.repository.JpaRepository; + +import gg.data.party.PartyPenalty; + +public interface PartyPenaltyRepository extends JpaRepository { + PartyPenalty findByUserId(Long id); +} diff --git a/gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java b/gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java index 8b13388ca..da78569ff 100644 --- a/gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java +++ b/gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java @@ -27,5 +27,9 @@ public interface UserRoomRepository extends JpaRepository { + "AND ur.room.status = :status ORDER BY ur.room.dueDate ASC") List findFinishRoomsByUserId(@Param("userId") Long userId, @Param("status") RoomType status); + @Query("SELECT ur.room FROM UserRoom ur WHERE ur.user.id = :userId " + + "AND ur.isExist = true AND ur.room.status = 'OPEN'") + List findOpenRoomsByUserId(@Param("userId") Long userId); + Optional findByUserIdAndRoomIdAndIsExistTrue(Long userId, Long roomId); } diff --git a/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java b/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java index fc657ed07..c5310372f 100644 --- a/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java +++ b/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java @@ -167,7 +167,8 @@ public enum ErrorCode { ROOM_NOT_OPEN(400, "PT205", "모집중인 방이 아닙니다."), ROOM_NOT_PARTICIPANT(400, "PT206", "참여하지 않은 방 입니다."), USER_ALREADY_IN_ROOM(409, "PT301", "이미 참여한 방 입니다."), - ALREADY_REPORTED_ROOM(409, "PT302", "이미 신고한 방 입니다."); + ALREADY_REPORTED(409, "PT302", "이미 신고한 요청입니다."), + SELF_REPORT(409, "PT303", "자신을 신고할 수 없습니다."); private final int status; private final String errCode; diff --git a/gg-utils/src/main/java/gg/utils/exception/party/AlredayReportedException.java b/gg-utils/src/main/java/gg/utils/exception/party/AlredayReportedException.java new file mode 100644 index 000000000..f52ee1f75 --- /dev/null +++ b/gg-utils/src/main/java/gg/utils/exception/party/AlredayReportedException.java @@ -0,0 +1,10 @@ +package gg.utils.exception.party; + +import gg.utils.exception.ErrorCode; +import gg.utils.exception.custom.DuplicationException; + +public class AlredayReportedException extends DuplicationException { + public AlredayReportedException() { + super(ErrorCode.ALREADY_REPORTED.getMessage(), ErrorCode.ALREADY_REPORTED); + } +} diff --git a/gg-utils/src/main/java/gg/utils/exception/party/AlredayReportedRoomException.java b/gg-utils/src/main/java/gg/utils/exception/party/AlredayReportedRoomException.java deleted file mode 100644 index b3ea65e6b..000000000 --- a/gg-utils/src/main/java/gg/utils/exception/party/AlredayReportedRoomException.java +++ /dev/null @@ -1,10 +0,0 @@ -package gg.utils.exception.party; - -import gg.utils.exception.ErrorCode; -import gg.utils.exception.custom.DuplicationException; - -public class AlredayReportedRoomException extends DuplicationException { - public AlredayReportedRoomException() { - super(ErrorCode.ALREADY_REPORTED_ROOM.getMessage(), ErrorCode.ALREADY_REPORTED_ROOM); - } -} diff --git a/gg-utils/src/main/java/gg/utils/exception/party/SelfReportException.java b/gg-utils/src/main/java/gg/utils/exception/party/SelfReportException.java new file mode 100644 index 000000000..557db4311 --- /dev/null +++ b/gg-utils/src/main/java/gg/utils/exception/party/SelfReportException.java @@ -0,0 +1,10 @@ +package gg.utils.exception.party; + +import gg.utils.exception.ErrorCode; +import gg.utils.exception.custom.ForbiddenException; + +public class SelfReportException extends ForbiddenException { + public SelfReportException() { + super(ErrorCode.SELF_REPORT.getMessage(), ErrorCode.SELF_REPORT); + } +}