feature-backend-tobuy-buy
commit
09e891c27a
@ -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; |
||||
} |
||||
|
||||
} |
@ -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…
Reference in new issue