Skip to content

Commit

Permalink
#70 #67 feat: 보드 신고 생성API구현(createBoardReport), 보드신고 관련 예외처리 구현 / ref…
Browse files Browse the repository at this point in the history
…actor: 유저 신고(createUserReport) 검증 코드 순서 수정
  • Loading branch information
xhaktmchl committed Nov 28, 2022
1 parent 4c37199 commit 7cbea55
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 41 deletions.
7 changes: 7 additions & 0 deletions server/src/main/java/com/yogit/server/board/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class Board extends BaseEntity {
@OneToMany(mappedBy = "board")
private List<BoardReport> 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<BoardUser> boardUsers, List<BoardImage> boardImages, Category category, List<BookMark> bookMarks, List<ClipBoard> clipBoards) {
Expand Down Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -169,4 +172,8 @@ public List<String> getBoardImagesUUids(){
public void addCurrentMember(){
this.currentMember+=1;
}

public void changeReportedCnt(){
this.reportedCnt+=1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BoardReportRes> 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<BoardReportRes> createBoardReport(@RequestBody @Validated CreateBoardReportReq dto){
return boardReportService.createBoardReport(dto);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BoardReport, Long> {

@Query("select br from BoardReport br where br.reportingUser.id = :reportingUserId and br.board.id = :reportedBoardId and br.status = 'ACTIVE'")
List<BoardReport> findByReportingUserIdAndReportedBoardId(@Param("reportingUserId") Long reportingUserId, @Param("reportedBoardId") Long reportedBoardId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,22 +32,22 @@ public ApplicationResponse<UserReportRes> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

public interface BoardReportService {

// ApplicationResponse<BoardReportRes> createBoardReport(CreateBoardReportReq createBoardReportReq);
ApplicationResponse<BoardReportRes> createBoardReport(CreateBoardReportReq createBoardReportReq);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<BoardReportRes> createBoardReport(CreateBoardReportReq createBoardReportReq) {
//
//
// }
@Override
@Transactional(readOnly = false)
public ApplicationResponse<BoardReportRes> 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);
}
}

0 comments on commit 7cbea55

Please sign in to comment.