Skip to content

Commit

Permalink
refactor : 모임 참여 API 기능 단위로 함수 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
1000kkannoo committed Sep 18, 2023
1 parent 3c07b57 commit b9607b1
Showing 1 changed file with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,54 @@ public GroupResponse.Create createGroup(GroupServiceRequest.Create request, User
// 모임 참여
@Transactional
public GroupResponse.Participate participateGroup(GroupServiceRequest.Participate request, Users user) {
validateLocationName(request.getLocationName());
Groups group = getGroup(request.getGroupId());

checkDuplicateParticipation(group, user);
validateTransportation(request.getTransportationType());
participateGroupValidate(request, user, group);

// 어드민이 참여하는 경우 (즉, 모임이 생성된 직후)
if (group.getAdminId().equals(user.getUserId())) {
List<Subway> nearestStationsList =
subwayRepository.getNearestStationsList(request.getLatitude(), request.getLongitude());

for (Subway subway : nearestStationsList) {
bestPlaceRepository.save(
BestPlace.builder()
.group(group)
.placeName(subway.getName())
.latitude(subway.getLatitude().doubleValue())
.longitude(subway.getLongitude().doubleValue())
.build()
);
}
saveNearestStationList(request, group);
}

String encryptedPassword = encrypt(request.getPassword());
Participation participation = participationRepository.save(
toParticipationEntity(request, group, user.getUserId(), encryptedPassword)
);
Participation participation = saveParticipation(request, user, group);

return GroupResponse.Participate.response(participation);
}

private Participation saveParticipation(GroupServiceRequest.Participate request, Users user, Groups group) {
return participationRepository.save(Participation.builder()
.group(group)
.userId(user.getUserId())
.userName(request.getUserName())
.locationName(request.getLocationName())
.latitude(request.getLatitude())
.longitude(request.getLongitude())
.transportation(request.getTransportationType())
.password(encrypt(request.getPassword()))
.build());
}

private void participateGroupValidate(GroupServiceRequest.Participate request, Users user, Groups group) {
validateLocationName(request.getLocationName());
checkDuplicateParticipation(group, user);
validateTransportation(request.getTransportationType());
}

private void saveNearestStationList(GroupServiceRequest.Participate request, Groups group) {
List<Subway> nearestStationsList =
subwayRepository.getNearestStationsList(request.getLatitude(), request.getLongitude());

for (Subway subway : nearestStationsList) {
bestPlaceRepository.save(
BestPlace.builder()
.group(group)
.placeName(subway.getName())
.latitude(subway.getLatitude().doubleValue())
.longitude(subway.getLongitude().doubleValue())
.build()
);
}
}

// 내 참여 정보 수정
@Transactional
public GroupResponse.ParticipateUpdate participateUpdate(
Expand Down Expand Up @@ -370,21 +387,6 @@ private Groups toGroupEntity(GroupServiceRequest.Create request, Users user) {
.build();
}

private Participation toParticipationEntity(
GroupServiceRequest.Participate request, Groups group, long userId, String encryptedPassword
) {
return Participation.builder()
.group(group)
.userId(userId)
.userName(request.getUserName())
.locationName(request.getLocationName())
.latitude(request.getLatitude())
.longitude(request.getLongitude())
.transportation(request.getTransportationType())
.password(encryptedPassword)
.build();
}

private Participation getParticipate(Long id) {
return participationRepository.findById(id).orElseThrow(
() -> new CustomException(NOT_FOUND_PARTICIPATE)
Expand Down

0 comments on commit b9607b1

Please sign in to comment.