Fix backend Stock API

feature-backend-add-springdocs
Masaharu.Kato 4 months ago
parent e0dcd66962
commit 959491714b
  1. 36
      backend/src/main/java/com/example/todoapp/controller/StocksController.java
  2. 15
      backend/src/main/java/com/example/todoapp/dto/DeleteStockRequest.java
  3. 4
      backend/src/main/java/com/example/todoapp/service/StocksService.java

@ -1,16 +1,22 @@
package com.example.todoapp.controller;
import com.example.todoapp.dto.DeleteStockRequest;
import com.example.todoapp.dto.ResponseStockDTO;
import com.example.todoapp.dto.StockDTO;
import com.example.todoapp.model.Stocks;
import com.example.todoapp.service.StocksService;
import jakarta.validation.Valid;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import lombok.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@ -81,27 +87,33 @@ public class StocksController {
* @param stockDetails 更新内容
* @return 更新された在庫
*/
@PutMapping("/{id}")
public ResponseEntity<StockDTO> updateStock(
@PutMapping("/update")
public ResponseEntity<Map<String, Object>> updateStock(
Authentication authentication,
@PathVariable("id") Long stockId,
@Valid @RequestBody Stocks stockDetails) {
Stocks updatedStock = stockService.updateStocks(authentication.getName(), stockId, stockDetails);
return ResponseEntity.ok(StockDTO.fromEntity(updatedStock));
stockService.updateStocks(authentication.getName(), stockDetails);
Map<String, Object> response = new HashMap<>();
response.put("result", true);
return ResponseEntity.ok(response);
}
/**
* 指定されたIDの在庫を削除する
*
* @param authentication 認証情報
* @param taskId 削除する在庫のID
* @return 空のレスポンス
* @param stockInfo 削除する在庫情報stockIdをキーに持つ
* @return レスポンス
*/
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteStock(
@DeleteMapping("/delete")
public ResponseEntity<Map<String, Object>> deleteStock(
Authentication authentication,
@PathVariable("id") Long stockId) {
stockService.deleteStock(authentication.getName(), stockId);
return ResponseEntity.ok().build();
@Valid @RequestBody DeleteStockRequest request) {
stockService.deleteStock(authentication.getName(), request.getStockId());
Map<String, Object> response = new HashMap<>();
response.put("result", true);
return ResponseEntity.ok(response);
}
}

@ -0,0 +1,15 @@
package com.example.todoapp.dto;
import lombok.Data;
/**
* 在庫削除のリクエストオブジェクトDTO
* <p>
* このクラスはクライアントとサーバー間で在庫情報をやり取りするために使用されます
* エンティティとは異なり必要な情報のみを含み関連エンティティへの参照ではなくIDのみを保持します
* </p>
*/
@Data
public class DeleteStockRequest {
private Long stockId;
}

@ -75,8 +75,8 @@ public class StocksService {
* @param stockDetails 編集内容(新しい情報)
* @return 編集された在庫エンティティ
*/
public Stocks updateStocks(String username, Long stockId, Stocks stockDetails) {
Stocks stock = getStockById(username, stockId);
public Stocks updateStocks(String username, Stocks stockDetails) {
Stocks stock = getStockById(username, stockDetails.getStockId());
stock.setAmount(stockDetails.getAmount());
stock.setPrice(stockDetails.getPrice());
stock.setLastUpdate(stockDetails.getLastUpdate());

Loading…
Cancel
Save