diff --git a/server/src/main/java/com/yogit/server/board/entity/Board.java b/server/src/main/java/com/yogit/server/board/entity/Board.java index 3709bdd..f37e14a 100644 --- a/server/src/main/java/com/yogit/server/board/entity/Board.java +++ b/server/src/main/java/com/yogit/server/board/entity/Board.java @@ -76,6 +76,8 @@ public class Board extends BaseEntity { @OneToMany(mappedBy = "board") private List boardReports; + private Integer reportedCnt; + // 생성자 @Builder public Board(Long id, City city, User host, String title, String address, String addressDetail, float longitute, float latitude, LocalDateTime date, String notice, String introduction, String kindOfPerson, int currentMember, int totalMember, List boardUsers, List boardImages, Category category, List bookMarks, List clipBoards) { @@ -121,6 +123,7 @@ public Board(CreateBoardReq dto, User host, City city, Category category){ this.category = category; // this.bookMarks = bookMarks; // this.clipBoards = clipBoards; + this.reportedCnt = 0; } /* @@ -169,4 +172,8 @@ public List getBoardImagesUUids(){ public void addCurrentMember(){ this.currentMember+=1; } + + public void changeReportedCnt(){ + this.reportedCnt+=1; + } } diff --git a/server/src/main/java/com/yogit/server/report/controller/BoardReportController.java b/server/src/main/java/com/yogit/server/report/controller/BoardReportController.java index 0ec3b7a..fca1dc3 100644 --- a/server/src/main/java/com/yogit/server/report/controller/BoardReportController.java +++ b/server/src/main/java/com/yogit/server/report/controller/BoardReportController.java @@ -23,19 +23,19 @@ public class BoardReportController { private final BoardReportService boardReportService; -// /** -// * 게시글 신고 생성 -// * @author 토마스 -// */ -// @ApiOperation(value = "게시글 신고 생성", notes = "게시글 신고 생성 요청.") -// @ApiResponses({ -// @ApiResponse(code= 201, message = "요청에 성공하였습니다."), -// @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), -// @ApiResponse(code= 404, message = "존재하지 않는 보드입니다."), -// @ApiResponse(code = 4000 , message = "서버 오류입니다.") -// }) -// @PostMapping -// public ApplicationResponse createBoardReport(@RequestBody @Validated CreateBoardReportReq dto){ -// return boardReportService.createBoardReport(dto); -// } + /** + * 게시글 신고 생성 + * @author 토마스 + */ + @ApiOperation(value = "게시글 신고 생성", notes = "게시글 신고 생성 요청.") + @ApiResponses({ + @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 보드입니다."), + @ApiResponse(code = 4000 , message = "서버 오류입니다.") + }) + @PostMapping + public ApplicationResponse createBoardReport(@RequestBody @Validated CreateBoardReportReq dto){ + return boardReportService.createBoardReport(dto); + } } diff --git a/server/src/main/java/com/yogit/server/report/exception/AlreadyReportBoardException.java b/server/src/main/java/com/yogit/server/report/exception/AlreadyReportBoardException.java new file mode 100644 index 0000000..72345d4 --- /dev/null +++ b/server/src/main/java/com/yogit/server/report/exception/AlreadyReportBoardException.java @@ -0,0 +1,9 @@ +package com.yogit.server.report.exception; + +import static com.yogit.server.report.exception.ReportExceptionList.*; + +public class AlreadyReportBoardException extends ReportException{ + public AlreadyReportBoardException(){ + super(ALREADY_REPORT_BOARD.getCODE(), ALREADY_REPORT_BOARD.getHTTPSTATUS(), ALREADY_REPORT_BOARD.getMESSAGE()); + } +} diff --git a/server/src/main/java/com/yogit/server/report/exception/AlreadyReportException.java b/server/src/main/java/com/yogit/server/report/exception/AlreadyReportException.java deleted file mode 100644 index 0b3fe5a..0000000 --- a/server/src/main/java/com/yogit/server/report/exception/AlreadyReportException.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.yogit.server.report.exception; - -public class AlreadyReportException extends ReportException{ - public AlreadyReportException() - { - super(ReportExceptionList.ALREADY_REPORT.getCODE(), ReportExceptionList.ALREADY_REPORT.getHTTPSTATUS(), ReportExceptionList.ALREADY_REPORT.getMESSAGE()); - } -} diff --git a/server/src/main/java/com/yogit/server/report/exception/AlreadyReportUserException.java b/server/src/main/java/com/yogit/server/report/exception/AlreadyReportUserException.java new file mode 100644 index 0000000..4560e33 --- /dev/null +++ b/server/src/main/java/com/yogit/server/report/exception/AlreadyReportUserException.java @@ -0,0 +1,8 @@ +package com.yogit.server.report.exception; + +public class AlreadyReportUserException extends ReportException{ + public AlreadyReportUserException() + { + super(ReportExceptionList.ALREADY_REPORT_USER.getCODE(), ReportExceptionList.ALREADY_REPORT_USER.getHTTPSTATUS(), ReportExceptionList.ALREADY_REPORT_USER.getMESSAGE()); + } +} diff --git a/server/src/main/java/com/yogit/server/report/exception/ReportExceptionList.java b/server/src/main/java/com/yogit/server/report/exception/ReportExceptionList.java index 99778f0..47cd56a 100644 --- a/server/src/main/java/com/yogit/server/report/exception/ReportExceptionList.java +++ b/server/src/main/java/com/yogit/server/report/exception/ReportExceptionList.java @@ -11,7 +11,8 @@ public enum ReportExceptionList { MAX_REPORTING_CNT("R001",BAD_REQUEST, "신고 횟수를 초과했습니다."), - ALREADY_REPORT("R0002", BAD_REQUEST, "이미 신고한 유저입니다."); + ALREADY_REPORT_USER("R0002", BAD_REQUEST, "이미 신고한 유저입니다."), + ALREADY_REPORT_BOARD("R0003", BAD_REQUEST, "이미 신고한 보드입니다."); private final String CODE; private final HttpStatus HTTPSTATUS; diff --git a/server/src/main/java/com/yogit/server/report/repository/BoardReportRepository.java b/server/src/main/java/com/yogit/server/report/repository/BoardReportRepository.java index 8ec2f29..72c8c02 100644 --- a/server/src/main/java/com/yogit/server/report/repository/BoardReportRepository.java +++ b/server/src/main/java/com/yogit/server/report/repository/BoardReportRepository.java @@ -2,8 +2,15 @@ import com.yogit.server.report.entity.BoardReport; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface BoardReportRepository extends JpaRepository { + + @Query("select br from BoardReport br where br.reportingUser.id = :reportingUserId and br.board.id = :reportedBoardId and br.status = 'ACTIVE'") + List findByReportingUserIdAndReportedBoardId(@Param("reportingUserId") Long reportingUserId, @Param("reportedBoardId") Long reportedBoardId); } diff --git a/server/src/main/java/com/yogit/server/report/service/UserReportServiceImpl.java b/server/src/main/java/com/yogit/server/report/service/UserReportServiceImpl.java index a9a10ee..3da322f 100644 --- a/server/src/main/java/com/yogit/server/report/service/UserReportServiceImpl.java +++ b/server/src/main/java/com/yogit/server/report/service/UserReportServiceImpl.java @@ -5,7 +5,7 @@ import com.yogit.server.report.dto.res.UserReportRes; import com.yogit.server.report.entity.UserReport; import com.yogit.server.report.enums.ReportStatus; -import com.yogit.server.report.exception.AlreadyReportException; +import com.yogit.server.report.exception.AlreadyReportUserException; import com.yogit.server.report.exception.MaxReportingCntException; import com.yogit.server.report.repository.UserReportRepository; import com.yogit.server.user.entity.User; @@ -32,22 +32,22 @@ public ApplicationResponse createUserReport(CreateUserReportReq d User reportedUser = userRepository.findById(dto.getReportedUserId()) .orElseThrow(() -> new NotFoundUserException()); - //신고 객체 생성 - UserReport userReport = new UserReport(dto.getContent(), reportingUser, reportedUser, dto.getReportType(), ReportStatus.ONGOIN); - userReportRepository.save(userReport); - - reportingUser.changeReportingCnt(); //신고하는 유저 신고 한 횟수 1증가 - reportedUser.changeReportedCnt(); //신고 당하는 유저 신고 받은 횟수 1증가 - //validation: 신고하는 유저의 신고 한 횟수 검증, 일주일에 신고 5번 이하 허용 if(reportingUser.getReportingCnt() > 5){ throw new MaxReportingCntException(); } //validation: 신고 받는 유저가 이미 신고 받은 유저인지 검증 - if(userReportRepository.findByReportingUserIdAndReportedUserId(dto.getReportingUserId(), dto.getReportedUserId()).size()!=0){ - throw new AlreadyReportException(); + if(!userReportRepository.findByReportingUserIdAndReportedUserId(dto.getReportingUserId(), dto.getReportedUserId()).isEmpty()){ + throw new AlreadyReportUserException(); } + //신고 객체 생성 + UserReport userReport = new UserReport(dto.getContent(), reportingUser, reportedUser, dto.getReportType(), ReportStatus.ONGOIN); + userReportRepository.save(userReport); + + reportingUser.changeReportingCnt(); //신고하는 유저 신고 한 횟수 1증가 + reportedUser.changeReportedCnt(); //신고 당하는 유저 신고 받은 횟수 1증가 + UserReportRes res = UserReportRes.toDto(userReport); return ApplicationResponse.create("유저 신고 생성 성공했습니다.", res); } diff --git a/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportService.java b/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportService.java index dd7c0a1..da69ed5 100644 --- a/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportService.java +++ b/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportService.java @@ -6,5 +6,5 @@ public interface BoardReportService { -// ApplicationResponse createBoardReport(CreateBoardReportReq createBoardReportReq); + ApplicationResponse createBoardReport(CreateBoardReportReq createBoardReportReq); } diff --git a/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportServiceImpl.java b/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportServiceImpl.java index 8e8eb1c..2f0206f 100644 --- a/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportServiceImpl.java +++ b/server/src/main/java/com/yogit/server/report/service/boardreport/BoardReportServiceImpl.java @@ -1,10 +1,20 @@ package com.yogit.server.report.service.boardreport; +import com.yogit.server.board.entity.Board; +import com.yogit.server.board.exception.NotFoundBoardException; +import com.yogit.server.board.repository.BoardRepository; import com.yogit.server.board.service.BoardService; import com.yogit.server.global.dto.ApplicationResponse; import com.yogit.server.report.dto.req.CreateBoardReportReq; import com.yogit.server.report.dto.res.BoardReportRes; +import com.yogit.server.report.entity.BoardReport; +import com.yogit.server.report.enums.ReportStatus; +import com.yogit.server.report.exception.AlreadyReportBoardException; +import com.yogit.server.report.exception.MaxReportingCntException; import com.yogit.server.report.repository.BoardReportRepository; +import com.yogit.server.user.entity.User; +import com.yogit.server.user.exception.NotFoundUserException; +import com.yogit.server.user.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -15,11 +25,35 @@ public class BoardReportServiceImpl implements BoardReportService { private final BoardReportRepository boardReportRepository; + private final UserRepository userRepository; + private final BoardRepository boardReport; -// @Override -// @Transactional(readOnly = false) -// public ApplicationResponse createBoardReport(CreateBoardReportReq createBoardReportReq) { -// -// -// } + @Override + @Transactional(readOnly = false) + public ApplicationResponse createBoardReport(CreateBoardReportReq dto) { + + User reportingUser = userRepository.findById(dto.getReportingUserId()) + .orElseThrow(() -> new NotFoundUserException()); + User reportedUser = userRepository.findById(dto.getReportedUserId()) + .orElseThrow(() -> new NotFoundUserException()); + Board reportedBoard = boardReport.findBoardById(dto.getReportedBoardId()) + .orElseThrow(() -> new NotFoundBoardException()); + + //validation: 신고하는 유저의 신고 한 횟수 검증, 일주일에 신고 5번 이하 허용 + if(reportingUser.getReportingCnt() > 5){ + throw new MaxReportingCntException(); + } + //validation: 신고 받는 보드가 이미 신고 받은 보드인지 검증 + if (!boardReportRepository.findByReportingUserIdAndReportedBoardId(dto.getReportingUserId(), dto.getReportedBoardId()).isEmpty()) { + throw new AlreadyReportBoardException(); + } + + BoardReport boardReport = new BoardReport(dto.getContent(), reportingUser, reportedUser, dto.getReportType(), ReportStatus.ONGOIN, reportedBoard); + boardReportRepository.save(boardReport); + + reportedBoard.changeReportedCnt();//게시글 신고 당한 횟수 +1 증가 + + BoardReportRes res = BoardReportRes.toDto(boardReport); + return ApplicationResponse.create("보드 신고가 생성됐습니다.",res); + } }