Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
import java.util.Map;
import java.util.Optional;

import static org.festimate.team.domain.matching.validator.MatchingValidator.isMatchingDateValid;
import static org.festimate.team.domain.matching.validator.MatchingValidator.*;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional(readOnly = true)
public class MatchingServiceImpl implements MatchingService {
private final MatchingRepository matchingRepository;
private final PointService pointService;
Expand All @@ -48,32 +49,28 @@ public MatchingStatusResponse createMatching(Participant participant, Festival f
return MatchingStatusResponse.of(matching.getStatus(), matching.getMatchingId());
}

@Transactional(readOnly = true)
@Override
public MatchingListResponse getMatchingList(Participant participant) {
List<MatchingInfo> matchings = getMatchingListByParticipant(participant);
return MatchingListResponse.from(matchings);
}

@Transactional(readOnly = true)
@Override
public AdminMatchingResponse getMatchingSize(Participant participant) {
int allMatchingSize = matchingRepository.countAllByApplicant(participant);
int completeMatchingSize = matchingRepository.countCompletedByApplicant(participant);
return AdminMatchingResponse.from(completeMatchingSize, allMatchingSize);
}

@Transactional(readOnly = true)
@Override
public MatchingDetailInfo getMatchingDetail(Participant participant, Festival festival, Long matchingId) {
Matching matching = matchingRepository.findByMatchingId(matchingId)
.orElseThrow(() -> new FestimateException(ResponseError.TARGET_NOT_FOUND));
if (matching.getTargetParticipant() == null || matching.getTargetParticipant().getUser() == null) {
throw new FestimateException(ResponseError.TARGET_NOT_FOUND);
}
if (!matching.getFestival().getFestivalId().equals(festival.getFestivalId())) {
throw new FestimateException(ResponseError.FORBIDDEN_RESOURCE);
}
isFestivalMatchValid(matching, festival);
isApplicantParticipantValid(matching, participant);
return MatchingDetailInfo.from(matching);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package org.festimate.team.domain.matching.validator;

import lombok.extern.slf4j.Slf4j;
import org.festimate.team.global.response.ResponseError;
import org.festimate.team.domain.festival.entity.Festival;
import org.festimate.team.domain.matching.entity.Matching;
import org.festimate.team.domain.participant.entity.Participant;
import org.festimate.team.global.exception.FestimateException;
import org.festimate.team.global.response.ResponseError;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MatchingValidator {

public static void isMatchingDateValid(final LocalDateTime requestMatchingTime, final LocalDateTime matchingStartAt) {
log.info("Validating requestMatchingTime. requestMatchingTime={}, matchingStartAt={}", requestMatchingTime, matchingStartAt);

if (requestMatchingTime.isBefore(matchingStartAt)) {
log.warn("Invalid requestMatchingTime: requestMatchingTime is before festival matchingStartAt");
throw new FestimateException(ResponseError.INVALID_TIME_TYPE);
}
}

public static void isApplicantParticipantValid(final Matching matching, final Participant participant) {
if (!matching.getApplicantParticipant().equals(participant)) {
throw new FestimateException(ResponseError.FORBIDDEN_RESOURCE);
}
}

log.info("matchingStartAt is valid.");
public static void isFestivalMatchValid(final Matching matching, final Festival festival) {
if (!matching.getFestival().getFestivalId().equals(festival.getFestivalId())) {
throw new FestimateException(ResponseError.FORBIDDEN_RESOURCE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,38 @@ void getMatchingDetail_invalidFestival_throwsException() {
.hasMessage(ResponseError.FORBIDDEN_RESOURCE.getMessage());
}

@Test
@DisplayName("매칭 상세 조회 - 다른 사람이 신청한 매칭을 조회할 경우 예외 발생")
void getMatchingDetail_invalidApplicant_throwsException() {
// given
User user = mockUser("사용자1", Gender.MAN, 1L);
User otherUser = mockUser("사용자2", Gender.MAN, 2L);
Festival festival = mockFestival(user, 1L, LocalDate.now().minusDays(1), LocalDate.now().plusDays(1));

Participant myParticipant = mockParticipant(user, festival, TypeResult.INFLUENCER, 1L);
Participant otherParticipant = mockParticipant(otherUser, festival, TypeResult.INFLUENCER, 2L);

Matching otherMatching = Matching.builder()
.festival(festival)
.applicantParticipant(otherParticipant)
.targetParticipant(Participant.builder()
.user(mockUser("상대방", Gender.WOMAN, 3L))
.build())
.status(MatchingStatus.COMPLETED)
.matchDate(LocalDateTime.now())
.build();

when(userService.getUserByIdOrThrow(user.getUserId())).thenReturn(user);
when(festivalService.getFestivalByIdOrThrow(festival.getFestivalId())).thenReturn(festival);
when(participantService.getParticipantOrThrow(user, festival)).thenReturn(myParticipant);
when(matchingRepository.findByMatchingId(1L)).thenReturn(Optional.of(otherMatching));

// when & then
assertThatThrownBy(() -> matchingService.getMatchingDetail(myParticipant, festival, 1L))
.isInstanceOf(FestimateException.class)
.hasMessage(ResponseError.FORBIDDEN_RESOURCE.getMessage());
}

@Test
@DisplayName("매칭 상세 조회 - 보류 중인 매칭을 조회할 경우 예외 발생")
void getMatchingDetail_pendingMatching_throwsException() {
Expand Down
Loading