create API stuffs/get (get all stuffs which has input category)

feature-backend-tobuy-buy^2
Amagasu 5 months ago
parent b08c92f6c0
commit e4414713dc
  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. 11
      backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java
  5. 39
      backend/src/main/java/com/example/todoapp/service/StuffsService.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<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.
//--------------------------------------------
@ -10,6 +10,7 @@ package com.example.todoapp.repository;
import com.example.todoapp.model.Stuffs;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@ -26,4 +27,12 @@ public interface StuffsRepository extends JpaRepository<Stuffs, Long> {
// 材料情報を主キーで取得するメソッド(必要に応じて追加)
Stuffs findByStuffId(Long stuffId);
/**
* 指定されたカテゴリに属する材料のリストを取得するメソッド.
*
* @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);
}
}
Loading…
Cancel
Save