parent
0b85079b10
commit
36eb8e64c3
@ -0,0 +1,127 @@ |
||||
//--------------------------------
|
||||
// InitTables.java
|
||||
//
|
||||
// 更新履歴:2025/06/10 新規作成
|
||||
// Copyright(c) 2025 IVIS All rights reserved.
|
||||
//
|
||||
// Tobuys, Stocks, Stuffs, Recipes, RecipeStuffsを起動時に初期化するクラス
|
||||
//
|
||||
//--------------------------------------------
|
||||
|
||||
package com.example.todoapp.config; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import jakarta.annotation.PostConstruct; |
||||
|
||||
import com.example.todoapp.model.ToBuys; |
||||
import com.example.todoapp.repository.ToBuysRepository; |
||||
import com.example.todoapp.model.Stocks; |
||||
import com.example.todoapp.repository.StocksRepository; |
||||
import com.example.todoapp.model.Recipes; |
||||
import com.example.todoapp.repository.RecipesRepository; |
||||
import com.example.todoapp.model.RecipeStuffs; |
||||
import com.example.todoapp.repository.RecipeStuffsRepository; |
||||
import com.example.todoapp.model.Stuffs; |
||||
import com.example.todoapp.repository.StuffsRepository; |
||||
|
||||
@Configuration |
||||
public class InitTables { |
||||
|
||||
@Autowired |
||||
private ToBuysRepository tobuysRepository; |
||||
@Autowired |
||||
private StocksRepository stocksRepository; |
||||
@Autowired |
||||
private RecipesRepository recipesRepository; |
||||
@Autowired |
||||
private RecipeStuffsRepository recipeStuffsRepository; |
||||
@Autowired |
||||
private StuffsRepository stuffsRepository; |
||||
|
||||
@PostConstruct |
||||
public void initTables() { |
||||
|
||||
tobuysRepository.deleteAll(); //データを残す場合はコメントアウト
|
||||
stocksRepository.deleteAll(); //データを残す場合はコメントアウト
|
||||
recipeStuffsRepository.deleteAll(); //データを残す場合はコメントアウト
|
||||
recipesRepository.deleteAll(); //データを残す場合はコメントアウト
|
||||
stuffsRepository.deleteAll(); //データを残す場合はコメントアウト
|
||||
|
||||
if (stuffsRepository.count() > 0) { |
||||
return; // すでにデータが存在する場合は何もしない
|
||||
|
||||
} else { |
||||
setNewStuff(1L, "牛乳", null, "乳製品"); |
||||
setNewStuff(2L, "ヨーグルト", null, "乳製品"); |
||||
setNewStuff(3L, "チーズ", null, "乳製品"); |
||||
setNewStuff(4L, "バター", null, "乳製品"); |
||||
setNewStuff(5L, "生クリーム", null, "乳製品"); |
||||
|
||||
setNewStuff(6L, "鮭", null, "魚・肉"); |
||||
setNewStuff(7L, "鶏むね肉", null, "魚・肉"); |
||||
setNewStuff(8L, "豚バラ肉", null, "魚・肉"); |
||||
setNewStuff(9L, "牛ひき肉", null, "魚・肉"); |
||||
setNewStuff(10L, "まぐろ", null, "魚・肉"); |
||||
|
||||
setNewStuff(11L, "にんじん", null, "野菜"); |
||||
setNewStuff(12L, "キャベツ", null, "野菜"); |
||||
setNewStuff(13L, "ほうれん草", null, "野菜"); |
||||
setNewStuff(14L, "玉ねぎ", null, "野菜"); |
||||
setNewStuff(15L, "ピーマン", null, "野菜"); |
||||
|
||||
setNewStuff(16L, "醤油", null, "調味料"); |
||||
setNewStuff(17L, "味噌", null, "調味料"); |
||||
setNewStuff(18L, "塩", null, "調味料"); |
||||
setNewStuff(19L, "砂糖", null, "調味料"); |
||||
setNewStuff(20L, "酢", null, "調味料"); |
||||
|
||||
setNewStuff(21L, "米", null, "その他"); |
||||
setNewStuff(22L, "パスタ", null, "その他"); |
||||
setNewStuff(23L, "小麦粉", null, "その他"); |
||||
setNewStuff(24L, "卵", null, "その他"); |
||||
setNewStuff(25L, "豆腐", null, "その他"); |
||||
} |
||||
|
||||
if (stuffsRepository.count() > 0) { |
||||
return; // すでにデータが存在する場合は何もしない
|
||||
} else { |
||||
setNewRecipe(1L, "鮭のムニエル", "鮭を小麦粉で焼いた料理"); |
||||
} |
||||
|
||||
if (recipesRepository.count() > 0) { |
||||
return; // すでにデータが存在する場合は何もしない
|
||||
} else { |
||||
setNewRecipeStuffs(1L, recipesRepository.findById(1L).orElse(null), 6L, 1); // 鮭
|
||||
setNewRecipeStuffs(3L, recipesRepository.findById(1L).orElse(null), 16L, 10); // 醤油
|
||||
setNewRecipeStuffs(2L, recipesRepository.findById(1L).orElse(null), 23L, 50); // 小麦粉
|
||||
} |
||||
|
||||
} |
||||
|
||||
private void setNewStuff(Long stuffId, String stuffName, String summary, String category) { |
||||
Stuffs stuff = new Stuffs(); |
||||
stuff.setStuffId(stuffId); |
||||
stuff.setStuffName(stuffName); |
||||
stuff.setSummary(summary); |
||||
stuff.setCategory(category); |
||||
stuffsRepository.save(stuff); |
||||
} |
||||
|
||||
private void setNewRecipe(Long recipeId, String recipeName, String summary) { |
||||
Recipes recipe = new Recipes(); |
||||
recipe.setRecipeId(recipeId); |
||||
recipe.setRecipieName(recipeName); |
||||
recipe.setSummary(summary); |
||||
} |
||||
|
||||
private void setNewRecipeStuffs(Long recipeStuffsId, Recipes recipe, Long stuffId, int amount) { |
||||
RecipeStuffs recipeStuff = new RecipeStuffs(); |
||||
recipeStuff.setRecipeStuffsId(recipeStuffsId); |
||||
recipeStuff.setRecipes(recipe); |
||||
recipeStuff.setRecipeStuffsId(stuffId); |
||||
recipeStuff.setAmount(amount); |
||||
recipeStuffsRepository.save(recipeStuff); |
||||
} |
||||
} |
Loading…
Reference in new issue