diff --git a/backend/src/main/java/com/example/todoapp/controller/StuffsController.java b/backend/src/main/java/com/example/todoapp/controller/StuffsController.java new file mode 100644 index 0000000..68722f6 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/controller/StuffsController.java @@ -0,0 +1,64 @@ +//-------------------------------- +// StuffsController.java +// +// 更新履歴:2025/06/09 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.controller; + +import com.example.todoapp.dto.StuffsDTO; +import com.example.todoapp.model.Stuffs; +import com.example.todoapp.service.StuffsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.Collections; + + +/** + * 材料リストに関するRESTコントローラー + * + * このコントローラーは、材料 (stuffs) の管理に関するエンドポイントを提供します。 + * + */ +@RestController +@RequestMapping("/stuff") +public class StuffsController { + + @Autowired + private StuffsService stuffsService; + + + /** + * カテゴリ指定で材料を取得するメソッド + * + * @param category カテゴリ名 + * @return 指定されたカテゴリに属する材料のリスト + */ + @GetMapping("/get") + public ResponseEntity getAllStuffsByCategory(String category) { + List stuffsList = stuffsService.getAllStuffsByCategory(category); + //DTOに変換 + List responceList = stuffsList.stream() + .map(stuff -> { + StuffsDTO resp = new StuffsDTO(); + resp.setStuffId(stuff.getStuffId()); + resp.setStuffName(stuff.getStuffName()); + resp.setSummary(stuff.getSummary()); + resp.setCategory(stuff.getCategory()); + return resp; + }) + .collect(Collectors.toList()); + // 空のリストの場合は空のリストを返す + if (responceList.isEmpty()) { + return ResponseEntity.ok(Collections.emptyList()); + } + return ResponseEntity.ok(responceList); + } + +} diff --git a/backend/src/main/java/com/example/todoapp/dto/StuffsDTO.java b/backend/src/main/java/com/example/todoapp/dto/StuffsDTO.java new file mode 100644 index 0000000..5dd6706 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/StuffsDTO.java @@ -0,0 +1,42 @@ +//-------------------------------- +// StuffsDTO.java +// +// +// 更新履歴:2025/06/09 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.dto; + +import com.example.todoapp.model.Stuffs; +import lombok.Data; + + +/** + * 材料のデータ転送オブジェクト(DTO)クラス + * + * このクラスは、材料情報をクライアントとサーバー間でやり取りするために使用されます。 + * エンティティとは異なり、必要な情報のみを含み、関連エンティティへの参照ではなくIDのみを保持します。 + */ +@Data +public class StuffsDTO { + private Long stuffId; // 材料ID + private String stuffName; // 材料名 + private String summary; // 概要 + private String category; // カテゴリ + + /** + * StuffsエンティティからDTOを作成する + * + * @param stuff 変換元のStuffsエンティティ + * @return 変換されたStuffsDTOオブジェクト + */ + public static StuffsDTO fromEntity(Stuffs stuff) { + StuffsDTO dto = new StuffsDTO(); + dto.setStuffId(stuff.getStuffId()); + dto.setStuffName(stuff.getStuffName()); + dto.setSummary(stuff.getSummary()); + dto.setCategory(stuff.getCategory()); + return dto; + } + +} diff --git a/backend/src/main/java/com/example/todoapp/model/Stuffs.java b/backend/src/main/java/com/example/todoapp/model/Stuffs.java index 14fb111..8807fcf 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/Stuffs.java @@ -1,9 +1,8 @@ //-------------------------------- // Stuffs.java // -// 分類:社員管理システムV2・ビジネスロジック層 // -// 更新履歴:2025/06/02 新規作成 +// 更新履歴:2025/06/09 新規作成 // Copyright(c) 2025 IVIS All rights reserved. //-------------------------------------------- diff --git a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java index 1bd452f..f2fb344 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java @@ -2,7 +2,7 @@ // StuffsRepository.java // // -// 更新履歴:2025/06/05 新規作成 +// 更新履歴:2025/06/09 新規作成 // Copyright(c) 2025 IVIS All rights reserved. //-------------------------------------------- @@ -12,6 +12,9 @@ import com.example.todoapp.model.Stuffs; import java.util.Optional; +import java.util.List; + + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -27,6 +30,15 @@ public interface StuffsRepository extends JpaRepository { // 材料情報を主キーで取得するメソッド(必要に応じて追加) Stuffs findByStuffId(Long stuffId); + Optional findByStuffName(String stuffName); + /** + * 指定されたカテゴリに属する材料のリストを取得するメソッド. + * + * @param category カテゴリ名 + * @return 指定されたカテゴリに属する材料のリスト + */ + List findByCategory(String category); + } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/StuffsService.java b/backend/src/main/java/com/example/todoapp/service/StuffsService.java new file mode 100644 index 0000000..18b3924 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/service/StuffsService.java @@ -0,0 +1,39 @@ +//-------------------------------- +// StuffsService.java +// +// +// 更新履歴:2025/06/09 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.service; + +import com.example.todoapp.model.Stuffs; +import com.example.todoapp.repository.StuffsRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 材料 (stuffs) のサービスクラス + * + * このクラスは、材料 (stuffs) に関するビジネスロジックを提供します。 + */ + +@Service +public class StuffsService { + + @Autowired + private StuffsRepository stuffsRepository; + + /** + * カテゴリ指定で材料を取得するメソッド + * + * @param category カテゴリ名 + * @return 指定されたカテゴリに属する材料のリスト + */ + public List getAllStuffsByCategory(String category) { + return stuffsRepository.findByCategory(category); + } +} diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index 916729d..1477b83 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -181,41 +181,49 @@ export const toBuyApi = { export const stuffApi = { getStuffs: async (category: string): Promise => { - const data = [ - { stuffId: 1, stuffName: "牛乳", category: "乳製品" }, - { stuffId: 2, stuffName: "ヨーグルト", category: "乳製品" }, - { stuffId: 3, stuffName: "チーズ", category: "乳製品" }, - { stuffId: 4, stuffName: "バター", category: "乳製品" }, - { stuffId: 5, stuffName: "生クリーム", category: "乳製品" }, - - { stuffId: 6, stuffName: "鮭", category: "魚・肉" }, - { stuffId: 7, stuffName: "鶏むね肉", category: "魚・肉" }, - { stuffId: 8, stuffName: "豚バラ肉", category: "魚・肉" }, - { stuffId: 9, stuffName: "牛ひき肉", category: "魚・肉" }, - { stuffId: 10, stuffName: "まぐろ", category: "魚・肉" }, - - { stuffId: 11, stuffName: "にんじん", category: "野菜" }, - { stuffId: 12, stuffName: "キャベツ", category: "野菜" }, - { stuffId: 13, stuffName: "ほうれん草", category: "野菜" }, - { stuffId: 14, stuffName: "玉ねぎ", category: "野菜" }, - { stuffId: 15, stuffName: "ピーマン", category: "野菜" }, - - { stuffId: 16, stuffName: "醤油", category: "調味料" }, - { stuffId: 17, stuffName: "味噌", category: "調味料" }, - { stuffId: 18, stuffName: "塩", category: "調味料" }, - { stuffId: 19, stuffName: "砂糖", category: "調味料" }, - { stuffId: 20, stuffName: "酢", category: "調味料" }, - - { stuffId: 21, stuffName: "米", category: "その他" }, - { stuffId: 22, stuffName: "パスタ", category: "その他" }, - { stuffId: 23, stuffName: "小麦粉", category: "その他" }, - { stuffId: 24, stuffName: "卵", category: "その他" }, - { stuffId: 25, stuffName: "豆腐", category: "その他" } - ] - - const filtered = data.filter(stuff => stuff.category == category) - - return filtered + const data = await fetch(`${API_BASE_URL}/api/stuff/get?category=${encodeURIComponent(category)}`, { + headers: getHeaders(), // 認証トークンを含むヘッダー + }); + + if (!data.ok) { + throw new Error(`Failed to fetch stuffs for category ${category}`); + } + return data.json(); + // const data = [ + // { stuffId: 1, stuffName: "牛乳", category: "乳製品" }, + // { stuffId: 2, stuffName: "ヨーグルト", category: "乳製品" }, + // { stuffId: 3, stuffName: "チーズ", category: "乳製品" }, + // { stuffId: 4, stuffName: "バター", category: "乳製品" }, + // { stuffId: 5, stuffName: "生クリーム", category: "乳製品" }, + + // { stuffId: 6, stuffName: "鮭", category: "魚・肉" }, + // { stuffId: 7, stuffName: "鶏むね肉", category: "魚・肉" }, + // { stuffId: 8, stuffName: "豚バラ肉", category: "魚・肉" }, + // { stuffId: 9, stuffName: "牛ひき肉", category: "魚・肉" }, + // { stuffId: 10, stuffName: "まぐろ", category: "魚・肉" }, + + // { stuffId: 11, stuffName: "にんじん", category: "野菜" }, + // { stuffId: 12, stuffName: "キャベツ", category: "野菜" }, + // { stuffId: 13, stuffName: "ほうれん草", category: "野菜" }, + // { stuffId: 14, stuffName: "玉ねぎ", category: "野菜" }, + // { stuffId: 15, stuffName: "ピーマン", category: "野菜" }, + + // { stuffId: 16, stuffName: "醤油", category: "調味料" }, + // { stuffId: 17, stuffName: "味噌", category: "調味料" }, + // { stuffId: 18, stuffName: "塩", category: "調味料" }, + // { stuffId: 19, stuffName: "砂糖", category: "調味料" }, + // { stuffId: 20, stuffName: "酢", category: "調味料" }, + + // { stuffId: 21, stuffName: "米", category: "その他" }, + // { stuffId: 22, stuffName: "パスタ", category: "その他" }, + // { stuffId: 23, stuffName: "小麦粉", category: "その他" }, + // { stuffId: 24, stuffName: "卵", category: "その他" }, + // { stuffId: 25, stuffName: "豆腐", category: "その他" } + // ] + + // const filtered = data.filter(stuff => stuff.category == category) + + // return filtered } }