study/TIL

20250109 TIL

으녕오리 2025. 1. 13. 10:04
@Transactional
public ReportResDto createReport(Long fromId, Long toId, Long matchingId, String content) {

    // fromId, toId가 같으면 에러코드
    if (Objects.equals(fromId, toId)) {
        throw new BadRequestException(ErrorCode.BAD_REQUEST);
    }

    // fromUser, toUser 불러오기
    User fromUser = userRepository.findByIdOrElseThrow(fromId);
    User toUser = userRepository.findByIdOrElseThrow(toId);

    // report의 fromUser와 toUser가 입력값의 fromUser와 toUser와 같은 것이 있다면 에러코드
    if (reportRepository.existsByFromUserAndToUser(fromUser, toUser)) {
        throw new BadRequestException(ErrorCode.BAD_REQUEST);
    }

    // matching 불러오기
    Matching matching = matchRepository.findByIdOrElseThrow(matchingId);

    // report 객체 생성
    Report report = new Report(content, matching, fromUser, toUser);

    // 신고 저장
    reportRepository.save(report);

    // 5회 이상이면 rejected로 변경
    // findByID(1) id=1인 모든 값
    // findByName(은영) name=은영인 모든 값
    // findByToUser(민범) toUser=민범인 모든 값
    if (reportRepository.countAllByToUser(toUser) >= 5) {
        toUser.updateStatus(UserStatus.REJECTED);
    }

    return new ReportResDto(report);
}