diff --git a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java index b984698..bce8a33 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -52,8 +52,10 @@ public class ToBuysController { */ @PostMapping("/add") - public ResponseEntity addToBuys(@Valid @RequestBody ToBuysDTO dto) { - toBuysService.addToBuys(dto); + public ResponseEntity addToBuys( + @Valid @RequestBody ToBuysDTO dto, + Authentication authentication) { + toBuysService.addToBuys(dto, authentication); return ResponseEntity.ok("Item added to 'To Buys' successfully"); } @@ -66,11 +68,10 @@ public class ToBuysController { @GetMapping("/get") public ResponseEntity getAllToBuysByUserId(Authentication authentication) { - // 認証されたユーザー名を取得 String username = authentication.getName(); - // ユーザー情報を取得(例: userRepository経由) + // ユーザー情報を取得 User user = userRepository.findByUsername(username) .orElseThrow(() -> new UsernameNotFoundException("User not found")); @@ -102,8 +103,18 @@ public class ToBuysController { * @return 削除が成功した場合にtrueを含むレスポンス */ @DeleteMapping("/delete") - public ResponseEntity> deleteToBuy(@RequestBody DeleteToBuyRequest request) { - int deletedCount = toBuysService.deleteToBuyByIds(request.getUser_id(), request.getTobuy_id()); + public ResponseEntity> deleteToBuy( + @RequestBody DeleteToBuyRequest request, + Authentication authentication) { + + // 認証されたユーザー名を取得 + String username = authentication.getName(); + + // ユーザー情報を取得 + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new UsernameNotFoundException("User not found")); + + int deletedCount = toBuysService.deleteToBuyByIds(user.getId(), request.getTobuy_id()); Map response = new HashMap<>(); diff --git a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java index 589137c..8049ee9 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -18,6 +18,7 @@ import com.example.todoapp.repository.UserRepository; import jakarta.transaction.Transactional; +import org.springframework.security.core.Authentication; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -50,11 +51,12 @@ public class ToBuysService { * * @param toBuyDTO 追加する購入アイテムのデータ(DTO) */ - public void addToBuys(ToBuysDTO toBuyDTO) { + public void addToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { // ユーザー情報を取得 - User user = userRepository.findById(toBuyDTO.getUser_id()) - .orElseThrow(() -> new RuntimeException("ユーザーがありませ: " + toBuyDTO.getUser_id())); + String username = authentication.getName(); + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); Stuffs stuffs; if (toBuyDTO.getStuff_id() == null) {