api/tobuy/addByRecipe can select stock-differ option

feature-addByRecipeOption
Amagasu 4 months ago
parent 26d8b67ef6
commit 9eebcf670e
  1. 7
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 25
      backend/src/main/java/com/example/todoapp/dto/AddByRecipeDTO.java
  3. 19
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -7,6 +7,7 @@
package com.example.todoapp.controller;
import com.example.todoapp.dto.AddByRecipeDTO;
import com.example.todoapp.dto.BuyRequestDTO;
import com.example.todoapp.dto.DeleteToBuyRequestDTO;
import com.example.todoapp.dto.ToBuyResponseDTO;
@ -181,12 +182,10 @@ public class ToBuysController {
*/
@PostMapping("/addByRecipe")
public ResponseEntity<Map<String, Object>> addByRecipe(
@RequestBody Map<String, Long> payload,
@RequestBody AddByRecipeDTO addByRecipeDTO,
Authentication authentication) {
Long recipeId = payload.get("recipeId");
Long servings = payload.get("servings");
List<ToBuyResponseDTO> responseList = toBuysService.addByRecipeId(recipeId, servings,authentication);
List<ToBuyResponseDTO> responseList = toBuysService.addByRecipeId(addByRecipeDTO.getRecipeId(), addByRecipeDTO.getServings(), addByRecipeDTO.getDifference(), authentication);
//shopのフィールドを削除
List<Map<String, Object>> filteredList = responseList.stream()

@ -0,0 +1,25 @@
//--------------------------------
// AddByRecipeDTO.java
//
//
// 更新履歴:2025/06/18 新規作成
// Copyright(c) 2025 IVIS All rights reserved.
//--------------------------------------------
package com.example.todoapp.dto;
import lombok.Data;
/**
* 料理からの買うものリスト追加データ転送オブジェクトDTO
* <p>
* このクラスはクライアントとサーバー間で料理情報をやり取りするために使用されます
* エンティティとは異なり必要な情報のみを含み関連エンティティへの参照ではなくIDのみを保持します
* </p>
*/
@Data
public class AddByRecipeDTO {
private Long recipeId;
private Long servings;
private Boolean difference;
}

@ -63,6 +63,9 @@ public class ToBuysService {
@Autowired
private RecipeStuffsRepository RecipeStuffsRepository;
@Autowired
private StocksService stocksService;
/**
* 購入リストに新しいアイテムを追加する
*
@ -222,7 +225,7 @@ public class ToBuysService {
* @param authentication 認証情報
* @return 追加された買うもののリスト
*/
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Long servings,Authentication authentication) {
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Long servings, Boolean difference, Authentication authentication) {
// ユーザー情報を取得
String username = authentication.getName();
User user = userRepository.findByUsername(username)
@ -236,13 +239,23 @@ public class ToBuysService {
for (RecipeStuffs rs : recipeStuffsList) {
Stuffs stuff = rs.getStuff();
// 材料の数量をサービング数に基づいて計算
// 材料の数量を在庫数とサービング数に基づいて計算
int stockAmount = stocksService.calcAmountByStuffId(user.getId(), stuff.getStuffId());
int requiredAmount = rs.getAmount() * (servings != null ? servings.intValue() : 1);
if (difference) {
// 差分を計算
requiredAmount = requiredAmount - stockAmount;
if (requiredAmount <= 0) {
continue; // 在庫が十分なので追加しない
}
} else {
// 差分を考慮しない場合はrequiredAmountをそのまま使用
}
Optional<ToBuys> existingToBuyOpt = toBuysRepository.findByUserAndStuff(user, stuff);
ToBuys toBuy;
if (existingToBuyOpt.isPresent()) {
if (existingToBuyOpt.isPresent()) { // 既存の「買うもの」がある場合
toBuy = existingToBuyOpt.get();
toBuy.setAmount(toBuy.getAmount() + requiredAmount); // 既存の数量を更新
} else {

Loading…
Cancel
Save