Merge remote-tracking branch 'origin/feature-backend-stuff' into feature-backend-tobuy-buy

feature-backend-tobuy-buy
Masaharu.Kato 5 months ago
commit 09e891c27a
  1. 64
      backend/src/main/java/com/example/todoapp/controller/StuffsController.java
  2. 42
      backend/src/main/java/com/example/todoapp/dto/StuffsDTO.java
  3. 3
      backend/src/main/java/com/example/todoapp/model/Stuffs.java
  4. 14
      backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java
  5. 39
      backend/src/main/java/com/example/todoapp/service/StuffsService.java
  6. 78
      frontend/src/services/api.ts

@ -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<Stuffs> stuffsList = stuffsService.getAllStuffsByCategory(category);
//DTOに変換
List<StuffsDTO> 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);
}
}

@ -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;
}
}

@ -1,9 +1,8 @@
//--------------------------------
// Stuffs.java
//
// 分類:社員管理システムV2・ビジネスロジック層
//
// 更新履歴:2025/06/02 新規作成
// 更新履歴:2025/06/09 新規作成
// Copyright(c) 2025 IVIS All rights reserved.
//--------------------------------------------

@ -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, Long> {
// 材料情報を主キーで取得するメソッド(必要に応じて追加)
Stuffs findByStuffId(Long stuffId);
Optional<Stuffs> findByStuffName(String stuffName);
/**
* 指定されたカテゴリに属する材料のリストを取得するメソッド.
*
* @param category カテゴリ名
* @return 指定されたカテゴリに属する材料のリスト
*/
List<Stuffs> findByCategory(String category);
}

@ -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<Stuffs> getAllStuffsByCategory(String category) {
return stuffsRepository.findByCategory(category);
}
}

@ -181,41 +181,49 @@ export const toBuyApi = {
export const stuffApi = {
getStuffs: async (category: string): Promise<Stuff[]> => {
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
}
}

Loading…
Cancel
Save