From 17976b4120957070840cf54d571175b496874deb Mon Sep 17 00:00:00 2001 From: mummhy0811 Date: Wed, 5 Jun 2024 15:25:46 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=BC=EC=A0=95=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EC=8B=9C=20=EB=AA=A9=ED=91=9C=EA=B8=88=EC=95=A1=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=EB=90=98=EA=B2=8C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../trip/domain/TripRepository.java | 7 +++++++ .../trip/service/TripService.java | 9 ++++++-- .../tripPlace/domain/TripPlaceRepository.java | 4 ++++ .../tripPlace/service/TripPlaceService.java | 21 +++++++++++++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/hanaro/triptogether/trip/domain/TripRepository.java b/src/main/java/com/hanaro/triptogether/trip/domain/TripRepository.java index 8d77ec8..ea2fb31 100644 --- a/src/main/java/com/hanaro/triptogether/trip/domain/TripRepository.java +++ b/src/main/java/com/hanaro/triptogether/trip/domain/TripRepository.java @@ -1,11 +1,18 @@ package com.hanaro.triptogether.trip.domain; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.math.BigDecimal; import java.util.List; @Repository public interface TripRepository extends JpaRepository { List findAllByTeam_TeamIdx(Long teamIdx); + @Modifying + @Query("UPDATE Trip t SET t.tripGoalAmount = :goalAmount WHERE t.tripIdx = :tripIdx") + void updateGoalAmount(@Param("tripIdx") Long tripIdx, @Param("goalAmount") BigDecimal goalAmount); } diff --git a/src/main/java/com/hanaro/triptogether/trip/service/TripService.java b/src/main/java/com/hanaro/triptogether/trip/service/TripService.java index 294ce89..50d1f8a 100644 --- a/src/main/java/com/hanaro/triptogether/trip/service/TripService.java +++ b/src/main/java/com/hanaro/triptogether/trip/service/TripService.java @@ -52,9 +52,7 @@ public Trip findByTripIdx(Long tripIdx) { public List getTripsByTeam(Long teamIdx) { teamService.findTeamByTeamIdx(teamIdx); // 팀 확인 - System.out.println("팀 존재~~"); List trips = tripRepository.findAllByTeam_TeamIdx(teamIdx); - System.out.println("여행 사이즈~~"+trips.size()); List dtos = new ArrayList<>(); for (Trip trip : trips) { dtos.add(toTripResDto(trip)); @@ -164,4 +162,11 @@ private TripResDto toTripResDto(Trip trip) { .cities(cities) .build(); } + + @Transactional + public void setGoalAmount(Long tripIdx, BigDecimal goalAmount) { + Trip trip = tripRepository.findById(tripIdx) + .orElseThrow(() -> new ApiException(ExceptionEnum.TRIP_NOT_FOUND)); + tripRepository.updateGoalAmount(tripIdx, goalAmount); + } } diff --git a/src/main/java/com/hanaro/triptogether/tripPlace/domain/TripPlaceRepository.java b/src/main/java/com/hanaro/triptogether/tripPlace/domain/TripPlaceRepository.java index 46ef67c..cc9986f 100644 --- a/src/main/java/com/hanaro/triptogether/tripPlace/domain/TripPlaceRepository.java +++ b/src/main/java/com/hanaro/triptogether/tripPlace/domain/TripPlaceRepository.java @@ -6,6 +6,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.math.BigDecimal; import java.util.List; @@ -21,4 +22,7 @@ public interface TripPlaceRepository extends JpaRepository { @Modifying @Query("UPDATE TripPlace tp SET tp.placeOrder = tp.placeOrder - 1 WHERE tp.trip.tripIdx = :tripIdx AND tp.placeOrder > :placeOrder") void decrementPlaceOrderAfter(@Param("tripIdx") Long tripIdx, @Param("placeOrder") Integer placeOrder); + + @Query("SELECT sum(tp.placeAmount) FROM TripPlace tp WHERE tp.trip.tripIdx= :tripIdx") + BigDecimal getSumPlaceAmountByTripIdx(Long tripIdx); } diff --git a/src/main/java/com/hanaro/triptogether/tripPlace/service/TripPlaceService.java b/src/main/java/com/hanaro/triptogether/tripPlace/service/TripPlaceService.java index 08f1028..1237963 100644 --- a/src/main/java/com/hanaro/triptogether/tripPlace/service/TripPlaceService.java +++ b/src/main/java/com/hanaro/triptogether/tripPlace/service/TripPlaceService.java @@ -19,6 +19,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; import java.util.Objects; @@ -65,11 +66,15 @@ public void updatePlace(Long trip_placeIdx, TripPlaceUpdateInfoReqDto dto) { validateTeamMember(tripPlace.getTrip().getTeam(), dto.getMemberIdx()); PlaceEntity place = null; - if(dto.getPlaceIdx()!=0){ + if(dto.getPlaceIdx()!=null && dto.getPlaceIdx()!=0){ place = placeService.findByPlaceIdx(dto.getPlaceIdx()); } Member member = memberService.findByMemberIdx(dto.getMemberIdx()); tripPlace.update(place, dto.getPlaceAmount(), dto.getPlaceMemo(), member); + + Long tripIdx = tripPlace.getTrip().getTripIdx(); + //goalAmount 계산 및 설정 + tripService.setGoalAmount(tripIdx, tripPlaceRepository.getSumPlaceAmountByTripIdx(tripIdx)); } @Transactional @@ -115,6 +120,8 @@ public void deleteTripPlace(Long trip_placeIdx) { tripPlaceRepository.decrementPlaceOrderAfter(tripId, deletedPlaceOrder); } + + //병합된 코드(수정/추가/삭제) @Transactional public void updateTripPlace(Long tripIdx, TripPlaceUpdateReqDto reqDto) { Trip trip = tripService.findByTripIdx(tripIdx); @@ -143,7 +150,7 @@ public void updateTripPlace(Long tripIdx, TripPlaceUpdateReqDto reqDto) { for(TripPlaceUpdateAddReqDto dto : dtos) { validateTripDate(trip, dto.getTripDate()); PlaceEntity place = null; - if(dto.getPlaceIdx()!=0){ + if(dto.getPlaceIdx()!=null && dto.getPlaceIdx()!=0 ) { place = placeService.findByPlaceIdx(dto.getPlaceIdx()); } TripPlace tripPlace = TripPlace.builder() @@ -172,8 +179,18 @@ public void updateTripPlace(Long tripIdx, TripPlaceUpdateReqDto reqDto) { tripPlace.updateOrder(dto.getPlaceOrder(), dto.getTripDate(), member); } + //goalAmount 계산 및 설정 + tripService.setGoalAmount(tripIdx, tripPlaceRepository.getSumPlaceAmountByTripIdx(tripIdx)); + } +// private BigDecimal calcGoalAmount(Long tripIdx){ +// List placeAmounts = tripPlaceRepository.findAllByTrip_TripIdx(tripIdx); +// System.out.println(placeAmounts.size()+"~~~~~~~~~"); +// if(placeAmounts.isEmpty()) return BigDecimal.ZERO; +// return placeAmounts.stream().reduce(BigDecimal.ZERO, BigDecimal::add); +// } + public TripPlace checkTripPlaceExists(Long trip_placeIdx){ return tripPlaceRepository.findById(trip_placeIdx).orElseThrow(() -> new ApiException(ExceptionEnum.TRIP_PLACE_NOT_FOUND)); }