Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 여행 나가기 API 구현 #126

Merged
merged 8 commits into from
Mar 1, 2024
1 change: 1 addition & 0 deletions doorip-api/src/main/java/org/doorip/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public abstract class Constants {
public static final int MAX_STYLE_RATE = 100;
public static final int PROPENSITY_WEIGHT = 25;
public static final int MAX_PARTICIPANT_COUNT = 6;
public static final int MIN_PARTICIPANT_COUNT = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import lombok.RequiredArgsConstructor;
import org.doorip.auth.UserId;
import org.doorip.common.BaseResponse;
import org.doorip.common.ApiResponseUtil;
import org.doorip.common.BaseResponse;
import org.doorip.message.SuccessMessage;
import org.doorip.trip.dto.request.TripCreateRequest;
import org.doorip.trip.dto.request.TripEntryRequest;
Expand Down Expand Up @@ -77,4 +77,11 @@ public ResponseEntity<BaseResponse<?>> getParticipants(@UserId final Long userId
final TripParticipantGetResponse response = tripDetailService.getParticipants(userId, tripId);
return ApiResponseUtil.success(SuccessMessage.OK, response);
}

@PatchMapping("/{tripId}")
public ResponseEntity<BaseResponse<?>> leaveTrip(@UserId final Long userId,
@PathVariable final Long tripId) {
tripDetailService.leaveTrip(userId, tripId);
return ApiResponseUtil.success(SuccessMessage.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,18 @@ private Participant getParticipant(Long participantId) {

private List<Todo> getOurTodosAccordingToProgress(Long tripId, String progress) {
if (progress.equals(Constants.INCOMPLETE)) {
return todoRepository.findOurTodoByTripId(tripId, Secret.OUR, Progress.INCOMPLETE);
return todoRepository.findOurTodoByTripIdAndSecretAndProgress(tripId, Secret.OUR, Progress.INCOMPLETE);
} else if (progress.equals(Constants.COMPLETE)) {
return todoRepository.findOurTodoByTripId(tripId, Secret.OUR, Progress.COMPLETE);
return todoRepository.findOurTodoByTripIdAndSecretAndProgress(tripId, Secret.OUR, Progress.COMPLETE);
}
throw new InvalidValueException(ErrorMessage.INVALID_REQUEST_PARAMETER_VALUE);
}

private List<Todo> getMyTodosAccordingToProgress(Long userId, Long tripId, String progress) {
if (progress.equals(Constants.INCOMPLETE)) {
return todoRepository.findMyTodoByTripId(tripId, userId, Progress.INCOMPLETE);
return todoRepository.findMyTodoByTripIdAndUserIdAndProgress(tripId, userId, Progress.INCOMPLETE);
} else if (progress.equals(Constants.COMPLETE)) {
return todoRepository.findMyTodoByTripId(tripId, userId, Progress.COMPLETE);
return todoRepository.findMyTodoByTripIdAndUserIdAndProgress(tripId, userId, Progress.COMPLETE);
}
throw new InvalidValueException(ErrorMessage.INVALID_REQUEST_PARAMETER_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package org.doorip.trip.service;

import lombok.RequiredArgsConstructor;
import org.doorip.common.Constants;
import org.doorip.exception.EntityNotFoundException;
import org.doorip.message.ErrorMessage;
import org.doorip.trip.domain.Participant;
import org.doorip.trip.domain.Progress;
import org.doorip.trip.domain.Secret;
import org.doorip.trip.domain.Trip;
import org.doorip.trip.domain.*;
import org.doorip.trip.dto.response.MyTodoResponse;
import org.doorip.trip.dto.response.OurTodoResponse;
import org.doorip.trip.dto.response.TripParticipantGetResponse;
import org.doorip.trip.dto.response.TripStyleResponse;
import org.doorip.trip.repository.ParticipantRepository;
import org.doorip.trip.repository.TodoRepository;
import org.doorip.trip.repository.TripRepository;
import org.doorip.user.domain.User;
import org.doorip.user.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -28,8 +28,10 @@
@Transactional(readOnly = true)
@Service
public class TripDetailService {
private final UserRepository userRepository;
private final TripRepository tripRepository;
private final TodoRepository todoRepository;
private final ParticipantRepository participantRepository;

public MyTodoResponse getMyTodoDetail(Long userId, Long tripId) {
Trip findTrip = getTrip(tripId);
Expand Down Expand Up @@ -58,6 +60,18 @@ public TripParticipantGetResponse getParticipants(Long userId, Long tripId) {
return TripParticipantGetResponse.of(participants, response);
}

@Transactional
public void leaveTrip(Long userId, Long tripId) {
User findUser = getUser(userId);
Trip findTrip = getTrip(tripId);
int size = calculateParticipantsCount(findTrip);
Participant findParticipant = getParticipant(findUser, findTrip);
List<Todo> todos = todoRepository.findMyTodoByTripIdAndUserIdAndSecret(tripId, userId, Secret.MY);
todoRepository.deleteAll(todos);
participantRepository.delete(findParticipant);
deleteTripIfLastParticipant(size, findTrip);
}

private Map<String, Integer> createDefaultPropensity() {
return new HashMap<>(Map.of(STYLE_A, MIN_STYLE_RATE, STYLE_B, MIN_STYLE_RATE,
STYLE_C, MIN_STYLE_RATE, STYLE_D, MIN_STYLE_RATE, STYLE_E, MIN_STYLE_RATE)) {
Expand Down Expand Up @@ -105,6 +119,26 @@ private List<TripStyleResponse> calculateAndGetPropensityAverageRates(List<Parti
return response;
}

private User getUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.USER_NOT_FOUND));
}

private int calculateParticipantsCount(Trip findTrip) {
return findTrip.getParticipants().size();
}

private Participant getParticipant(User findUser, Trip findTrip) {
return participantRepository.findByUserAndTrip(findUser, findTrip)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.PARTICIPANT_NOT_FOUND));
}

private void deleteTripIfLastParticipant(int size, Trip trip) {
if (size == Constants.MIN_PARTICIPANT_COUNT) {
tripRepository.delete(trip);
}
}

private Trip getTrip(Long tripId) {
return tripRepository.findById(tripId)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.TRIP_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.doorip.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ParticipantRepository extends JpaRepository<Participant, Long> {
boolean existsByUserAndTrip(User user, Trip trip);

Optional<Participant> findByUserAndTrip(User user, Trip trip);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

더 직관적인 메소드 명이라 좋은 것 같습니다!

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ public interface TodoRepository extends JpaRepository<Todo, Long> {
"and d.progress = :progress " +
"and d.secret = :secret " +
"order by d.endDate")
List<Todo> findOurTodoByTripId(@Param("tripId") Long tripId, @Param("secret") Secret secret, @Param("progress") Progress progress);
List<Todo> findOurTodoByTripIdAndSecretAndProgress(@Param("tripId") Long tripId, @Param("secret") Secret secret, @Param("progress") Progress progress);

@Query("select d " +
"from Todo d " +
"join Trip i " +
"on d.trip = i " +
"join Participant p " +
"on p.trip = i " +
"join User u " +
"on p.user = u " +
"where i.id = :tripId " +
"and u.id = :userId " +
"and d.secret = :secret")
List<Todo> findMyTodoByTripIdAndUserIdAndSecret(@Param("tripId") Long tripId, @Param("userId") Long userId, @Param("secret") Secret secret);

@Query("select d " +
"from Todo d " +
Expand All @@ -35,7 +48,7 @@ public interface TodoRepository extends JpaRepository<Todo, Long> {
"and u.id = :userId " +
"and d.progress = :progress " +
"order by d.endDate")
List<Todo> findMyTodoByTripId(@Param("tripId") Long tripId, @Param("userId") Long userId, @Param("progress") Progress progress);
List<Todo> findMyTodoByTripIdAndUserIdAndProgress(@Param("tripId") Long tripId, @Param("userId") Long userId, @Param("progress") Progress progress);

int countTodoByTripIdAndSecretAndProgress(Long tripId, Secret secret, Progress progress);

Expand Down
Loading