Skip to content

Commit

Permalink
Merge pull request #135 from kookmin-sw/backend/develop/v3
Browse files Browse the repository at this point in the history
Backend/develop/v3
  • Loading branch information
J-Yong99 authored May 8, 2024
2 parents b6ea698 + e5a9a14 commit fda893b
Show file tree
Hide file tree
Showing 19 changed files with 266 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public ResponseEntity<APIResponse> deleteReceipts(
@RequestHeader Long userId,
@RequestBody ReceiptRequestDTO.deleteReceipts deleteReceipts
) {

receiptService.deleteReceipts(userId, deleteReceipts);
return ResponseEntity.ok(APIResponse.of(SuccessCode.DELETE_SUCCESS));
}
Expand All @@ -60,4 +61,15 @@ public ResponseEntity<APIResponse> createReceipt(
receiptService.createReceipt(userId, createReceipt);
return ResponseEntity.ok(APIResponse.of(SuccessCode.INSERT_SUCCESS));
}

// 영수증 수정
@PutMapping()
public ResponseEntity<APIResponse> updateReceipt(
@RequestHeader Long userId,
@RequestBody ReceiptRequestDTO.updateReceipt updateReceipt
) {
receiptService.validateUserWithReceipt(userId, updateReceipt.getId());
receiptService.updateReceipt(userId, updateReceipt);
return ResponseEntity.ok(APIResponse.of(SuccessCode.UPDATE_SUCCESS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AlreadyBookedDate extends BaseEntity {
private Long id;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CardView extends BaseEntity {
private Long id;

// tripfile_id
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "trip_file_id", nullable = false)
private TripFile tripFile;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.moment.core.domain.cardView;

import com.moment.core.domain.tripFile.TripFile;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface CardViewRepository extends JpaRepository<CardView, Long> {
List<CardView> findAllByTripFile_IdOrderByRecordedAt(Long tripFileId);
Expand All @@ -15,4 +17,10 @@ public interface CardViewRepository extends JpaRepository<CardView, Long> {
List<CardView> findByTripFile_User_IdAndIsLovedOrderByRecordedAt(Long userId, Boolean isLoved);

List<CardView> findAllByTripFile(TripFile tripFile);

Long countByTripFile_Trip_Id(Long tripId);

@Override
@EntityGraph(attributePaths = {"tripFile"})
Optional<CardView> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ImageFile {
private Long id;

// cardview 아이디
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "card_view_id", nullable = false)
private CardView cardView;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Receipt extends BaseEntity {
@Column(name = "id")
private Long id;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public interface ReceiptRepository extends PagingAndSortingRepository<Receipt, L
// void deleteAll(List<Receipt> receipts);

// void save(Receipt receipt);
Long countByTrip_User_Id(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Trip extends BaseEntity {
private Long id;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.moment.core.domain.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;
Expand All @@ -11,4 +12,7 @@ public interface TripRepository extends JpaRepository<Trip, Long> {
Optional<Trip> findByUser_IdAndIsNotTitledOrderByStartDate(Long userId, boolean b);

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



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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

public class ReceiptRequestDTO {
Expand Down Expand Up @@ -58,6 +59,8 @@ public static class getReceipt {
private Float disgust;

private String receiptThemeType;

private LocalDateTime createdAt;
}

@Data
Expand Down Expand Up @@ -113,4 +116,27 @@ public static class createReceipt {

private String receiptThemeType;
}

@Data
@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class updateReceipt {

private Long id;

private String mainDeparture;

private String subDeparture;

private String mainDestination;

private String subDestination;

private String oneLineMemo;

private String receiptThemeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ public static class GetTrip {
@Schema(description = "여행 이름")
private final String tripName;

private final int numOfCard;

// fromEntity
public static GetTrip fromEntity(Trip trip) {
public static GetTrip fromEntity(Trip trip, int numOfCard) {
return GetTrip.builder()
.id(trip.getId())
.email(trip.getUser().getEmail())
.startDate(trip.getStartDate())
.endDate(trip.getEndDate())
.analyzingCount(trip.getAnalyzingCount())
.tripName(trip.getTripName())
.numOfCard(numOfCard)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.moment.core.domain.cardView.CardView;
import com.moment.core.domain.cardView.CardViewRepository;
import com.moment.core.domain.trip.Trip;
import com.moment.core.domain.tripFile.TripFile;
import com.moment.core.domain.user.User;
import com.moment.core.domain.user.UserRepository;
Expand Down Expand Up @@ -97,7 +98,8 @@ public CardViewResponseDTO.GetCardView uploadRecord(CardViewRequestDTO.UploadRec

// tripFile, trip의 analyzingCount 증가
tripFileService.increaseAnalyzingCount(tripFile);
tripService.increaseAnalyzingCount(tripFile.getTrip());
Trip trip = tripFile.getTrip();
tripService.increaseAnalyzingCount(trip);
List<String> imageUrls = new ArrayList<>();
return CardViewResponseDTO.GetCardView.fromEntity(cardViewRepository.save(cardView), imageUrls);
}
Expand Down Expand Up @@ -150,32 +152,36 @@ public void updateRecord(Long cardViewId, CardViewRequestDTO.UpdateRecord update
@Transactional
public void deleteRecord(Long cardViewId) {
CardView cardView = cardViewRepository.findById(cardViewId).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 카드뷰입니다."));
TripFile tripFile = cardView.getTripFile();
Trip trip = tripFile.getTrip();
User user = tripFile.getUser();
String userId = user.getId().toString();

// cardView에 엮인 사진들 먼저 삭제
String userId = cardView.getTripFile().getUser().getId().toString();
imageFileService.deleteAll(cardView, userId);
s3Service.deleteFile(cardView.getRecordFileName(), userId);
boolean isAnalyzed = cardView.getRecordFileStatus().equals("WAIT");
boolean isWaiting = cardView.getRecordFileStatus().equals("WAIT");
boolean isFail = cardView.getRecordFileStatus().equals("FAIL");

// 만약 tripfile의 Trip이 untitled일 경우
// 만약 tripfile의 크기가 1이라면 tripFile과 cardView 전부 삭제, untitledTrip의 analyzingCount 감소
TripFile tripFile = cardView.getTripFile();
if (tripFile.getTrip().getIsNotTitled()) {
if (trip.getIsNotTitled()) {
if (tripFileService.getCardViewCount(tripFile) == 1) {
cardViewRepository.delete(cardView);
tripFileService.delete(cardView.getTripFile());
if (isAnalyzed)
tripService.decreaseAnalyzingCount(cardView.getTripFile().getTrip());
tripFileService.delete(tripFile);
if (isWaiting || isFail)
tripService.decreaseAnalyzingCount(trip);
}
}
// 만약 tripfile의 trip이 untitled가 아닐경우
// cardView만 삭제, tripFile의 analyzingCount 감소
// trip의 analyzingCount 감소
else {
cardViewRepository.delete(cardView);
if (isAnalyzed){
tripFileService.decreaseAnalyzingCount(cardView.getTripFile());
tripService.decreaseAnalyzingCount(cardView.getTripFile().getTrip());
if (isWaiting || isFail){
tripFileService.decreaseAnalyzingCount(tripFile);
tripService.decreaseAnalyzingCount(trip);
}
}
cardViewRepository.delete(cardView);
}

public void likeCardView(Long cardViewId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public void uploadAll(List<MultipartFile> imageFiles, Long cardViewId, Long user
public void deleteImages(List<Long> images, Long userId) {
for (Long imageId : images) {
ImageFile image = imageFileRepository.findById(imageId).orElseThrow(() -> new IllegalArgumentException("해당 이미지가 없습니다."));
userService.validateUserWithCardView(userId, image.getCardView().getId());
CardView cardView = image.getCardView();
userService.validateUserWithCardView(userId, cardView.getId());
s3Service.deleteFile(image.getFileName(), userId.toString());
imageFileRepository.delete(image);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
@RequiredArgsConstructor
public class ReceiptService {
private final ReceiptRepository receiptRepository;
private final UserRepository userRepository;
private final TripFileRepository tripFileRepository;
private final TripRepository tripRepository;
private final CardViewRepository cardViewRepository;

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

public ReceiptRequestDTO.getReceiptAll getAllReceipt(Long userId, Pageable pageable) {
Expand Down Expand Up @@ -76,16 +75,15 @@ private ReceiptRequestDTO.getReceipt mapToReceiptDTO(Receipt receipt) {
.neutral(receipt.getNeutral())
.disgust(receipt.getDisgust())
.receiptThemeType(receipt.getReceiptThemeType())
.createdAt(receipt.getCreatedAt())
.build();
}

public void deleteReceipts(Long userId, ReceiptRequestDTO.deleteReceipts deleteReceipts) {
List<Receipt> receipts = new ArrayList<>();
for (ReceiptRequestDTO.deleteReceipt deleteReceipt : deleteReceipts.getReceiptIds()) {
Receipt receipt = receiptRepository.findById(deleteReceipt.getReceiptId()).orElseThrow(() -> new IllegalArgumentException("해당 영수증이 존재하지 않습니다."));
if (!receipt.getTrip().getUser().getId().equals(userId)) {
throw new IllegalArgumentException("해당 영수증을 삭제할 권한이 없습니다.");
}
validateUserWithReceipt(userId, deleteReceipt.getReceiptId());
receipts.add(receipt);
}
receiptRepository.deleteAll(receipts);
Expand Down Expand Up @@ -125,7 +123,8 @@ public Map<String, Float> getCardViewCount(Trip trip) {
public void createReceipt(Long userId, ReceiptRequestDTO.createReceipt createReceipt) {
// 유저가 해당 여행에 접근 권한이 있는지 확인
Trip trip = tripRepository.findById(createReceipt.getTripId()).orElseThrow(() -> new IllegalArgumentException("해당 여행이 존재하지 않습니다."));
if (!trip.getUser().getId().equals(userId)) {
User user = trip.getUser();
if (!user.getId().equals(userId)) {
throw new IllegalArgumentException("해당 여행에 접근 권한이 없습니다.");
}

Expand Down Expand Up @@ -177,4 +176,25 @@ public void createReceipt(Long userId, ReceiptRequestDTO.createReceipt createRec

receiptRepository.save(receipt);
}

public void updateReceipt(Long userId, ReceiptRequestDTO.updateReceipt updateReceipt) {
Receipt receipt = receiptRepository.findById(updateReceipt.getId()).orElseThrow(() -> new IllegalArgumentException("해당 영수증이 존재하지 않습니다."));
receipt.setMainDeparture(updateReceipt.getMainDeparture());
receipt.setSubDeparture(updateReceipt.getSubDeparture());
receipt.setMainDestination(updateReceipt.getMainDestination());
receipt.setSubDestination(updateReceipt.getSubDestination());
receipt.setOneLineMemo(updateReceipt.getOneLineMemo());
receipt.setReceiptThemeType(updateReceipt.getReceiptThemeType());

receiptRepository.save(receipt);
}

public void validateUserWithReceipt(Long userId, Long id) {
Receipt receipt = receiptRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 영수증이 존재하지 않습니다."));
Trip trip = receipt.getTrip();
User user = trip.getUser();
if (!user.getId().equals(userId)) {
throw new IllegalArgumentException("해당 영수증을 수정할 권한이 없습니다.");
}
}
}
Loading

0 comments on commit fda893b

Please sign in to comment.