Skip to content

Commit

Permalink
feat: 일정 변경시 목표금액 수정되게 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
mummhy0811 committed Jun 5, 2024
1 parent a190c91 commit 17976b4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<Trip, Long> {
List<Trip> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public Trip findByTripIdx(Long tripIdx) {

public List<TripResDto> getTripsByTeam(Long teamIdx) {
teamService.findTeamByTeamIdx(teamIdx); // 팀 확인
System.out.println("팀 존재~~");
List<Trip> trips = tripRepository.findAllByTeam_TeamIdx(teamIdx);
System.out.println("여행 사이즈~~"+trips.size());
List<TripResDto> dtos = new ArrayList<>();
for (Trip trip : trips) {
dtos.add(toTripResDto(trip));
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;


Expand All @@ -21,4 +22,7 @@ public interface TripPlaceRepository extends JpaRepository<TripPlace, Long> {
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<BigDecimal> 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));
}
Expand Down

0 comments on commit 17976b4

Please sign in to comment.