Skip to content

Commit

Permalink
Merge pull request #102 from DuruDuru-UMC-7th/feature/#95-ingredient-…
Browse files Browse the repository at this point in the history
…expiry-date-api

Feat: 영수증 인식 식재료 전체 구매 날짜 변경 API 구현
  • Loading branch information
ParkJh38 authored Jan 30, 2025
2 parents a216465 + 53da45a commit 0fc6b81
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.backend.DuruDuru.global.converter;

import com.backend.DuruDuru.global.domain.entity.Ingredient;
import com.backend.DuruDuru.global.domain.entity.Receipt;
import com.backend.DuruDuru.global.domain.enums.MinorCategory;
import com.backend.DuruDuru.global.web.dto.Ingredient.IngredientRequestDTO;
import com.backend.DuruDuru.global.web.dto.Ingredient.IngredientResponseDTO;
Expand Down Expand Up @@ -165,5 +166,13 @@ public static IngredientResponseDTO.UpdateOCRIngredientResultDTO UpdateOCRIngred
.build();
}

public static IngredientResponseDTO.UpdateOCRPurchaseDateResultDTO toOCRPurchaseDateResultDTO(Receipt receipt) {
return IngredientResponseDTO.UpdateOCRPurchaseDateResultDTO.builder()
.memberId(receipt.getMember().getMemberId())
.receiptId(receipt.getReceiptId())
.fridgeId(receipt.getMember().getFridgeId())
.purchaseDate(receipt.getPurchaseDate())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ public void setFridge(Fridge fridge) {
}
}

public Long getFridgeId() {
return fridge != null ? fridge.getFridgeId() : null;
}


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

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,6 +26,9 @@ public class Receipt extends BaseEntity {
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@Column(name = "purchase_date", nullable = true, columnDefinition = "timestamp")
private LocalDate purchaseDate;

@OneToMany(mappedBy = "receipt", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Ingredient> ingredients = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IngredientCommandService {

Ingredient createRawIngredient(Long memberId, IngredientRequestDTO.CreateRawIngredientDTO request);
Ingredient updateIngredient(Long memberId, Long ingredientId, IngredientRequestDTO.UpdateIngredientDTO request);
void deleteIngredient(Long memberId, Long fridgeId, Long ingredientId);
void deleteIngredient(Long memberId, Long ingredientId);

Ingredient registerPurchaseDate(Long memberId, Long ingredientId, IngredientRequestDTO.PurchaseDateRequestDTO request);
Ingredient setStorageType(Long memberId, Long ingredientId, IngredientRequestDTO.StorageTypeRequestDTO request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public Ingredient createRawIngredient(Long memberId, IngredientRequestDTO.Create
@Override
public Ingredient updateIngredient(Long memberId, Long ingredientId, IngredientRequestDTO.UpdateIngredientDTO request) {
Member member = findMemberById(memberId);
//Fridge fridge = findFridgeById(fridgeId);
Ingredient ingredient = ingredientRepository.findById(ingredientId)
.orElseThrow(() -> new IllegalArgumentException("Ingredient not found. ID: " + ingredientId));

Expand All @@ -75,9 +74,8 @@ public Ingredient updateIngredient(Long memberId, Long ingredientId, IngredientR
}

@Override
public void deleteIngredient(Long memberId, Long fridgeId, Long ingredientId) {
public void deleteIngredient(Long memberId, Long ingredientId) {
Member member = findMemberById(memberId);
Fridge fridge = findFridgeById(fridgeId);
Ingredient ingredient = ingredientRepository.findById(ingredientId)
.orElseThrow(() -> new IllegalArgumentException("Ingredient not found. ID: " + ingredientId));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public List<Ingredient> extractAndCategorizeProductNames(MultipartFile file, Lon
throw new IllegalArgumentException("사용자의 냉장고가 없습니다.");
}

Receipt receipt = createReceipt(member);
Receipt receipt = createReceipt(memberId);

List<String> productNames = extractProductNames(file);
List<Ingredient> savedIngredients = new ArrayList<>();
Expand Down Expand Up @@ -293,7 +293,7 @@ private Ingredient createAndSaveIngredient(String productName, Member member, Fr
.majorCategory(majorCategory)
.minorCategory(minorCategory != null ? minorCategory : MinorCategory.기타)
.count(1L)
.purchaseDate(LocalDate.now())
.purchaseDate(receipt.getPurchaseDate())
//.expiryDate(LocalDate.now().plusDays(7))
.receipt(receipt)
.build();
Expand All @@ -313,6 +313,19 @@ public Ingredient updateOCRIngredient(Long memberId, Long ingredientId, Long rec
}


public Receipt updateOCRPurchaseDate(Long memberId, Long receiptId, IngredientRequestDTO.PurchaseDateRequestDTO request) {
Member member = findMemberById(memberId);
Receipt receipt = receiptRepository.findById(receiptId)
.orElseThrow(() -> new IllegalArgumentException("Receipt not found. ID: " + receiptId));

receipt.setPurchaseDate(request.getPurchaseDate());
for (Ingredient ingredient : receipt.getIngredients()) {
ingredient.setPurchaseDate(request.getPurchaseDate());
}
return receipt;
}


private Member findMemberById(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("Member not found. ID: " + memberId));
Expand All @@ -323,10 +336,14 @@ private Fridge findFridgeById(Long fridgeId) {
.orElseThrow(() -> new IllegalArgumentException("Fridge not found. ID: " + fridgeId));
}

private Receipt createReceipt(Member member) {
@Transactional
public Receipt createReceipt(Long memberId) {
Member member = findMemberById(memberId);
Receipt receipt = Receipt.builder()
.member(member)
.purchaseDate(LocalDate.now()) // 영수증 등록한 날짜(응답 요청 날짜)로 설정
.build();
return receiptRepository.save(receipt);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public ApiResponse<IngredientResponseDTO.UpdateIngredientResultDTO> updateIngred
// 식재료 삭제
@DeleteMapping("/{ingredient_id}")
@Operation(summary = "식재료 삭제 API", description = "식재료를 삭제하는 API 입니다.")
public ApiResponse<?> deleteIngredient(@RequestParam Long memberId, @RequestParam Long fridgeId, @PathVariable("ingredient_id") Long ingredientId){
ingredientCommandService.deleteIngredient(memberId, fridgeId, ingredientId);
public ApiResponse<?> deleteIngredient(@RequestParam Long memberId, @PathVariable("ingredient_id") Long ingredientId){
ingredientCommandService.deleteIngredient(memberId, ingredientId);
return ApiResponse.onSuccess(SuccessStatus.INGREDIENT_OK, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.backend.DuruDuru.global.apiPayload.code.status.SuccessStatus;
import com.backend.DuruDuru.global.converter.IngredientConverter;
import com.backend.DuruDuru.global.domain.entity.Ingredient;
import com.backend.DuruDuru.global.domain.entity.Receipt;
import com.backend.DuruDuru.global.service.OCRService.ClovaOCRReceiptService;
import com.backend.DuruDuru.global.web.dto.Ingredient.IngredientRequestDTO;
import com.backend.DuruDuru.global.web.dto.Ingredient.IngredientResponseDTO;
Expand Down Expand Up @@ -56,4 +57,12 @@ public ApiResponse<IngredientResponseDTO.UpdateOCRIngredientResultDTO> ingredien
return ApiResponse.onSuccess(SuccessStatus.OCR_OK, IngredientConverter.UpdateOCRIngredientResultDTO(updateOCRIngredient));
}

@PatchMapping(value = "/{receipt_id}/purchase-date")
@Operation(summary = "영수증 인식 식재료 구매 날짜 수정 API", description = "영수증 인식된 식재료의 구매 날짜를 수정하는 API 입니다.")
public ApiResponse<IngredientResponseDTO.UpdateOCRPurchaseDateResultDTO> updateOCRPurchaseDate(@PathVariable("receipt_id") Long receiptId, @RequestParam Long memberId,
@RequestBody IngredientRequestDTO.PurchaseDateRequestDTO request) {
Receipt updatePurchaseDate = clovaOCRReceiptService.updateOCRPurchaseDate(memberId, receiptId, request);
return ApiResponse.onSuccess(SuccessStatus.OCR_OK, IngredientConverter.toOCRPurchaseDateResultDTO(updatePurchaseDate));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,14 @@ public static class UpdateOCRIngredientResultDTO {
String minorCategory;
}

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class UpdateOCRPurchaseDateResultDTO {
Long memberId;
Long fridgeId;
Long receiptId;
LocalDate purchaseDate;
}
}

0 comments on commit 0fc6b81

Please sign in to comment.