Merge branch 'feature-backend-zhang' into develop-frontend

feature-frontend-recipe-api
Masaharu.Kato 4 months ago
commit dfe4a3ff14
  1. 7
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 4
      backend/src/main/java/com/example/todoapp/service/StocksService.java
  3. 28
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java
  4. 8
      frontend/src/pages/StockPage.tsx

@ -92,7 +92,6 @@ public class ToBuysController {
}
/**
* 指定されたユーザーIDに基づいてすべての買うものリストを取得する
*
@ -125,9 +124,6 @@ public class ToBuysController {
})
.collect(Collectors.toList());
// Map<String, Object> responseBody = new HashMap<>();
// responseBody.put("tobuy_array", responseList);
return ResponseEntity.ok(responseList);
}
@ -189,7 +185,8 @@ public class ToBuysController {
Authentication authentication) {
Long recipeId = payload.get("recipeId");
List<ToBuyResponseDTO> responseList = toBuysService.addByRecipeId(recipeId, authentication);
Long servings = payload.get("servings");
List<ToBuyResponseDTO> responseList = toBuysService.addByRecipeId(recipeId, servings,authentication);
//shopのフィールドを削除
List<Map<String, Object>> filteredList = responseList.stream()

@ -1,6 +1,5 @@
package com.example.todoapp.service;
import com.example.todoapp.dto.StockDTO;
import com.example.todoapp.dto.AddStocksDTO;
import com.example.todoapp.dto.UpdateStockRequest;
import com.example.todoapp.model.Stocks;
@ -57,9 +56,6 @@ public class StocksService {
} else {
// 材料情報を取得
Optional<Stuffs> existstuffs = stuffsRepository.findById(stock.getStuffId());
if (existstuffs == null) {
throw new RuntimeException("材料がありません");
}
stuffs = existstuffs.get();
}

@ -91,14 +91,22 @@ public class ToBuysService {
stuff = optStuff.get();
}
ToBuys toBuys = new ToBuys();
toBuys.setUser(user);
toBuys.setStuff(stuff);
toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop());
Optional<ToBuys> existingToBuy = toBuysRepository.findByUserAndStuff(user, stuff);
// データベースに保存
return toBuysRepository.save(toBuys);
if (existingToBuy.isPresent()) {
// 存在する場合は数量を更新
ToBuys existing = existingToBuy.get();
existing.setAmount(existing.getAmount() + toBuyDTO.getAmount());
return toBuysRepository.save(existing);
} else {
// 新しい材料を作成
ToBuys toBuys = new ToBuys();
toBuys.setUser(user);
toBuys.setStuff(stuff);
toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop());
return toBuysRepository.save(toBuys);
}
}
@ -212,7 +220,7 @@ public class ToBuysService {
* @param authentication 認証情報
* @return 追加された買うもののリスト
*/
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Authentication authentication) {
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Long servings,Authentication authentication) {
// ユーザー情報を取得
String username = authentication.getName();
User user = userRepository.findByUsername(username)
@ -225,7 +233,9 @@ public class ToBuysService {
for (RecipeStuffs rs : recipeStuffsList) {
Stuffs stuff = rs.getStuff();
int requiredAmount = rs.getAmount();
// 材料の数量をサービング数に基づいて計算
int requiredAmount = rs.getAmount() * (servings != null ? servings.intValue() : 1);
Optional<ToBuys> existingToBuyOpt = toBuysRepository.findByUserAndStuff(user, stuff);

@ -39,6 +39,14 @@ const formatDateLocal = (date: Date) => {
return `${year}-${month}-${day}`;
};
// 日付をyyyy-MM-dd形式で返す関数
const formatDateLocal = (date: Date) => {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
};
// 新規在庫の初期状態
const EMPTY_STOCK: Omit<Stock, 'stockId' | 'stuffId'> & { stuffId: number | null } & { newAddition: boolean } = {
stuffId: null,

Loading…
Cancel
Save