Skip to content
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

[FEAT] 댓글 신고 API with 패널티 부여 로직 #711

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gg-data/src/main/java/gg/data/party/CommentReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
20 changes: 20 additions & 0 deletions gg-data/src/main/java/gg/data/party/PartyPenalty.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
20 changes: 8 additions & 12 deletions gg-data/src/main/java/gg/data/party/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,15 +25,29 @@ public class ReportController {

/**
* 방을 신고한다.
* @param reportRoomReqDto 신고 내용
* @param reportReqDto 신고 내용
* @param roomId 방 번호
* @return 참여중인 방 전체 List
* @return roomId
*/
@PostMapping("/rooms/{room_id}")
public ResponseEntity<Long> 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<Long> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@Getter
@NoArgsConstructor
public class ReportRoomReqDto {
public class ReportReqDto {
@Size(min = 1, max = 200)
private String content;
}
Original file line number Diff line number Diff line change
@@ -1,64 +1,147 @@
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;

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<RoomReport> 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<RoomReport> 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<CommentReport> 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<CommentReport> 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<Room> 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);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java
Original file line number Diff line number Diff line change
@@ -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<CommentReport, Long> {
public List<CommentReport> findByCommentId(Long commentId);

public Optional<CommentReport> findByReporterAndCommentId(User reporter, Long commentId);
}
Original file line number Diff line number Diff line change
@@ -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, Long> {
PartyPenalty findByUserId(Long id);
}
4 changes: 4 additions & 0 deletions gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ public interface UserRoomRepository extends JpaRepository<UserRoom, Long> {
+ "AND ur.room.status = :status ORDER BY ur.room.dueDate ASC")
List<Room> 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<Room> findOpenRoomsByUserId(@Param("userId") Long userId);

Optional<UserRoom> findByUserIdAndRoomIdAndIsExistTrue(Long userId, Long roomId);
}
Loading
Loading