Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ public ResponseEntity<FoodSearchResponse> searchFood(
@Operation(summary = "음식 소비(사용)")
@PutMapping("/consume")
public ResponseEntity<Void> consumeFood(
@Parameter(name = "foodRegisterId", required = true, in = ParameterIn.QUERY)
@RequestParam Long foodRegisterId
@Parameter(hidden = true) @JwtValidation Long userId,
@Parameter(name = "foodRegisterId", required = true, in = ParameterIn.QUERY)
@RequestParam Long foodRegisterId
) {
foodService.consumeFood(foodRegisterId);
foodService.consumeFood(userId, foodRegisterId);
return ResponseEntity.ok().build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Getter
@AllArgsConstructor
public class AllFoodListResponse {
private long foodRegisterId;
private String foodName;
private String foodCategory;
private LocalDate expirationDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface FoodService {
//List<AllFoodListResponse> getAllFoodsWithDday(Long userId); 필터링 기능 추가해서 getFilteredFoods로 대체
FoodSearchResponse searchFood(Long userId, String query);

void consumeFood(Long foodRegisterId);
void consumeFood(Long userId, Long foodRegisterId);
List<AllFoodListResponse> getFilteredFoods(Long userId, String storageMethod, boolean isExpiringSoon);

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,21 @@ public FoodSearchResponse searchFood(Long userId, String query){

@Transactional
@Override
public void consumeFood(Long foodRegisterId) {
public void consumeFood(Long userId, Long foodRegisterId) {
FoodRegister foodRegister = foodRegisterRepository.findById(foodRegisterId)
.orElseThrow(() -> new CustomException(ErrorCode.FOOD_REGISTER_NOT_FOUND));

foodRegister.consume(); //상태 변경
//유저 사용 개수 증가
if (!foodRegister.getUser().getId().equals(userId)) {
throw new CustomException(ErrorCode.FORBIDDEN); // 403 권한 없음
}

foodRegister.consume();

User user = foodRegister.getUser();
user.plusUsedCount();
}


@Transactional
public List<AllFoodListResponse> getAllFoodsWithDday(Long userId) {
// User 객체 조회
Expand All @@ -118,13 +123,14 @@ public List<AllFoodListResponse> getAllFoodsWithDday(Long userId) {
// 응답 DTO 변환
return foodList.stream()
.map(foodRegister -> {
long id = foodRegister.getFoodRegisterId();
String name = foodRegister.getFood().getFoodName();
String category = foodRegister.getFood().getFoodCategory();
LocalDate expiration = foodRegister.getExpirationDate();
long daysLeft = expiration != null
? ChronoUnit.DAYS.between(LocalDate.now(), expiration)
: -1;
return new AllFoodListResponse(name, category, expiration, daysLeft);
return new AllFoodListResponse(id, name, category, expiration, daysLeft);
})
.toList();
}
Expand All @@ -151,13 +157,14 @@ public List<AllFoodListResponse> getFilteredFoods(Long userId, String storageMet

// 응답 변환
.map(fr -> {
long id = fr.getFoodRegisterId();
String name = fr.getFood().getFoodName();
String category = fr.getFood().getFoodCategory();
LocalDate expiration = fr.getExpirationDate();
long daysLeft = expiration != null
? ChronoUnit.DAYS.between(LocalDate.now(), expiration)
: -1;
return new AllFoodListResponse(name, category, expiration, daysLeft);
return new AllFoodListResponse(id, name, category, expiration, daysLeft);
})
.toList();
}
Expand Down