|
|
|
@ -1,15 +1,21 @@ |
|
|
|
|
package com.example.todoapp.controller; |
|
|
|
|
|
|
|
|
|
import com.example.todoapp.dto.DeleteStockRequest; |
|
|
|
|
import com.example.todoapp.dto.StockDTO; |
|
|
|
|
import com.example.todoapp.model.Stocks; |
|
|
|
|
import com.example.todoapp.model.User; |
|
|
|
|
import com.example.todoapp.repository.UserRepository; |
|
|
|
|
import com.example.todoapp.service.StocksService; |
|
|
|
|
import jakarta.validation.Valid; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
|
import org.springframework.security.core.Authentication; |
|
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -26,6 +32,9 @@ public class StocksController { |
|
|
|
|
@Autowired |
|
|
|
|
private StocksService stockService; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private UserRepository userRepository; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* ログインユーザーのすべての在庫を取得する |
|
|
|
|
* |
|
|
|
@ -64,7 +73,7 @@ public class StocksController { |
|
|
|
|
* @param stock 作成する在庫の情報 |
|
|
|
|
* @return 作成された在庫 |
|
|
|
|
*/ |
|
|
|
|
@PostMapping |
|
|
|
|
@PostMapping("/add") |
|
|
|
|
public ResponseEntity<StockDTO> createStock( |
|
|
|
|
Authentication authentication, |
|
|
|
|
@Valid @RequestBody Stocks stock) { |
|
|
|
@ -80,13 +89,25 @@ 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)); |
|
|
|
|
@Valid @RequestBody StockDTO stockDTO) { |
|
|
|
|
|
|
|
|
|
Stocks updatedStock = stockService.updateStocks(authentication.getName(), stockDTO); |
|
|
|
|
|
|
|
|
|
Map<String, Object> response = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
if (updatedStock == null) { |
|
|
|
|
response.put("result", false); |
|
|
|
|
response.put("message", "指定されたIDの在庫が見つかりませんでした"); |
|
|
|
|
return ResponseEntity.ok(response); |
|
|
|
|
}else { |
|
|
|
|
response.put("result", true); |
|
|
|
|
response.put("message", "变更成功しました"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(response); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -96,11 +117,31 @@ public class StocksController { |
|
|
|
|
* @param taskId 削除する在庫のID |
|
|
|
|
* @return 空のレスポンス |
|
|
|
|
*/ |
|
|
|
|
@DeleteMapping("/{id}") |
|
|
|
|
@DeleteMapping("/delete") |
|
|
|
|
public ResponseEntity<?> deleteStock( |
|
|
|
|
Authentication authentication, |
|
|
|
|
@PathVariable("id") Long stockId) { |
|
|
|
|
stockService.deleteStock(authentication.getName(), stockId); |
|
|
|
|
return ResponseEntity.ok().build(); |
|
|
|
|
@RequestBody DeleteStockRequest request |
|
|
|
|
) { |
|
|
|
|
|
|
|
|
|
// 認証されたユーザー名を取得
|
|
|
|
|
String username = authentication.getName(); |
|
|
|
|
|
|
|
|
|
// ユーザー情報を取得
|
|
|
|
|
User user = userRepository.findByUsername(username) |
|
|
|
|
.orElseThrow(() -> new UsernameNotFoundException("User not found")); |
|
|
|
|
|
|
|
|
|
int deletedCount = stockService.deleteStockById(user.getId(), request.getStockId()); |
|
|
|
|
|
|
|
|
|
Map<String, Object> response = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
if (deletedCount > 0) { |
|
|
|
|
response.put("result", true); |
|
|
|
|
response.put("message", "削除成功しました"); |
|
|
|
|
} else { |
|
|
|
|
response.put("result", false); |
|
|
|
|
response.put("message", "指定された在庫が見つかりませんでした"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(response); |
|
|
|
|
} |
|
|
|
|
} |