Skip to content

Commit

Permalink
Merge pull request #140 from kookmin-sw/backend/develop/v3
Browse files Browse the repository at this point in the history
🐛 영수증 여행 외래키 문제 해결
  • Loading branch information
J-Yong99 authored May 11, 2024
2 parents 4b8355e + e086aac commit 18fdc5a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.moment.core.domain.BaseEntity;
import com.moment.core.domain.trip.Trip;
import com.moment.core.domain.tripFile.TripFile;
import com.moment.core.domain.user.User;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -22,10 +23,14 @@ public class Receipt extends BaseEntity {
@Column(name = "id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "trip_id", nullable = true)
private Trip trip;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

// 최대 길이 7
@Column(name = "main_departure", nullable = false, length = 7)
private String mainDeparture;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package com.moment.core.domain.receipt;

import com.moment.core.domain.cardView.CardView;
import com.moment.core.domain.trip.Trip;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public interface ReceiptRepository extends PagingAndSortingRepository<Receipt, Long>, JpaRepository<Receipt, Long>{

List<Receipt> findAllByTrip_User_Id(Long userId);
Page<Receipt> findAllByTrip_User_IdOrderByStDate(Long userId, Pageable pageable);
// List<Receipt> findAllByTrip_User_Id(Long userId);
Page<Receipt> findAllByUser_IdOrderByStDate(Long userId, Pageable pageable);
// Optional<Receipt> findById(Long id);

// void deleteAll(List<Receipt> receipts);

// void save(Receipt receipt);
Long countByTrip_User_Id(Long userId);
// Long countByTrip_User_Id(Long userId);

// List<Receipt> findAllByTrip(Trip trip);

List<Receipt> findAllByUser_Id(Long userId);

Number countByUser_Id(Long userId);

List<Receipt> findAllByTrip(Trip trip);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Optional;

public interface TripRepository extends JpaRepository<Trip, Long> {
Optional<Trip> findByUserAndIsNotTitledOrderByStartDate(User user, boolean b);
Optional<Trip> findByUser_IdAndIsNotTitledOrderByStartDate(Long userId, boolean b);
Optional<Trip> findByUserAndIsNotTitled(User user, boolean b);
Optional<Trip> findByUser_IdAndIsNotTitled(Long userId, boolean b);

List<Trip> findAllByUserAndIsNotTitledOrderByStartDate(User user, boolean b);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class TripFile extends BaseEntity {
private Long id;

// 여행 아이디
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "trip_id", nullable = false)
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "trip_id", nullable = true)
private Trip trip;

// 유저
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.moment.core.domain.tripFile.TripFile;
import com.moment.core.domain.tripFile.TripFileRepository;
import com.moment.core.domain.user.User;
import com.moment.core.domain.user.UserRepository;
import com.moment.core.dto.Pagination;
import com.moment.core.dto.request.ReceiptRequestDTO;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,8 +18,6 @@
import org.springframework.stereotype.Service;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -37,11 +34,11 @@ public class ReceiptService {

public Integer getReceiptCount(Long userId) {
// 유저의 총 영수증 개수 반환
return receiptRepository.countByTrip_User_Id(userId).intValue();
return receiptRepository.countByUser_Id(userId).intValue();
}

public ReceiptRequestDTO.getReceiptAll getAllReceipt(Long userId, Pageable pageable) {
Page<Receipt> receiptList = receiptRepository.findAllByTrip_User_IdOrderByStDate(userId, pageable);
Page<Receipt> receiptList = receiptRepository.findAllByUser_IdOrderByStDate(userId, pageable);

Page<ReceiptRequestDTO.getReceipt> receiptDTOPage = receiptList.map(receipt -> mapToReceiptDTO(receipt));

Expand All @@ -59,7 +56,7 @@ public ReceiptRequestDTO.getReceiptAll getAllReceipt(Long userId, Pageable pagea
private ReceiptRequestDTO.getReceipt mapToReceiptDTO(Receipt receipt) {
return ReceiptRequestDTO.getReceipt.builder()
.id(receipt.getId())
.tripId(receipt.getTrip().getId())
// .tripId(receipt.getTrip().getId())
.mainDeparture(receipt.getMainDeparture())
.subDeparture(receipt.getSubDeparture())
.mainDestination(receipt.getMainDestination())
Expand All @@ -68,7 +65,7 @@ private ReceiptRequestDTO.getReceipt mapToReceiptDTO(Receipt receipt) {
.numOfCard(receipt.getNumOfCard())
.stDate(receipt.getStDate())
.edDate(receipt.getEdDate())
.tripName(receipt.getTrip().getTripName())
.tripName(receipt.getTripName())
.happy(receipt.getHappy())
.sad(receipt.getSad())
.angry(receipt.getAngry())
Expand Down Expand Up @@ -162,6 +159,7 @@ public void createReceipt(Long userId, ReceiptRequestDTO.createReceipt createRec
// 영수증 생성
Receipt receipt = Receipt.builder()
.trip(trip)
.user(user)
.mainDeparture(createReceipt.getMainDeparture())
.subDeparture(createReceipt.getSubDeparture())
.mainDestination(createReceipt.getMainDestination())
Expand Down Expand Up @@ -196,8 +194,7 @@ public void updateReceipt(Long userId, ReceiptRequestDTO.updateReceipt updateRec

public void validateUserWithReceipt(Long userId, Long id) {
Receipt receipt = receiptRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 영수증이 존재하지 않습니다."));
Trip trip = receipt.getTrip();
User user = trip.getUser();
User user = receipt.getUser();
if (!user.getId().equals(userId)) {
throw new IllegalArgumentException("해당 영수증을 수정할 권한이 없습니다.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.moment.core.domain.cardView.CardView;
import com.moment.core.domain.cardView.CardViewRepository;
import com.moment.core.domain.receipt.Receipt;
import com.moment.core.domain.receipt.ReceiptRepository;
import com.moment.core.domain.trip.Trip;
import com.moment.core.domain.trip.TripRepository;
import com.moment.core.domain.tripFile.TripFile;
Expand Down Expand Up @@ -37,6 +39,7 @@ public class TripService {
private final ReceiptService receiptService;
private final TripFileRepository tripFileRepository;
private final CardViewRepository cardViewRepository;
private final ReceiptRepository receiptRepository;

public void save(Trip trip) {
tripRepository.save(trip);
Expand All @@ -57,6 +60,11 @@ public Trip delete(Long tripId) {
Trip untitledTrip = getUntitledTrip(user);
tripFileService.deleteByTripOrUntitled(trip, untitledTrip);
alreadyBookedDateService.deleteAll(user, trip.getStartDate(), trip.getEndDate());
List<Receipt> receipts = receiptRepository.findAllByTrip(trip);
// receipt에 trip을 전부 null 처리
for (Receipt receipt : receipts) {
receipt.setTrip(null);
}
tripRepository.delete(trip);
return trip;
}
Expand Down Expand Up @@ -121,13 +129,13 @@ public void register(TripRequestDTO.RegisterTrip registerTrip, Long userId) {

// 묶이지 않은 여행 가져오기
public Trip getUntitledTrip(User user) {
return tripRepository.findByUserAndIsNotTitledOrderByStartDate(user, true)
return tripRepository.findByUserAndIsNotTitled(user, true)
.orElseThrow(() -> new RuntimeException("묶이지 않은 기록이 존재하지 않음."));
}

// 묶이지 않은 여행 가져오기 userId로
public Trip getUntitledTripById(Long userId) {
return tripRepository.findByUser_IdAndIsNotTitledOrderByStartDate(userId, true)
return tripRepository.findByUser_IdAndIsNotTitled(userId, true)
.orElseThrow(() -> new RuntimeException("묶이지 않은 기록이 존재하지 않음."));
}

Expand Down

0 comments on commit 18fdc5a

Please sign in to comment.