From 7d173a03dc36df47ebe27986d7d9f0eb85d4e444 Mon Sep 17 00:00:00 2001 From: "zhang.pengcheng" Date: Thu, 5 Jun 2025 11:51:04 +0900 Subject: [PATCH] =?UTF-8?q?stuff=5Fname=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/ToBuysController.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) 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 7398b01..b984698 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -11,6 +11,8 @@ import com.example.todoapp.dto.DeleteToBuyRequest; import com.example.todoapp.dto.ToBuyResponse; import com.example.todoapp.dto.ToBuysDTO; import com.example.todoapp.model.ToBuys; +import com.example.todoapp.model.User; +import com.example.todoapp.repository.UserRepository; import com.example.todoapp.service.ToBuysService; import jakarta.validation.Valid; @@ -21,6 +23,8 @@ import java.util.stream.Collectors; 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.*; /** @@ -37,6 +41,9 @@ public class ToBuysController { @Autowired private ToBuysService toBuysService; + @Autowired + private UserRepository userRepository; + /** * 新しい購入アイテムを追加する * @@ -57,8 +64,17 @@ public class ToBuysController { * @return ユーザーに紐づく「買うもの」リスト */ @GetMapping("/get") - public ResponseEntity getAllToBuysByUserId(@RequestParam Long user_id) { - List toBuysList = toBuysService.getToBuysByUserId(user_id); + public ResponseEntity getAllToBuysByUserId(Authentication authentication) { + + + // 認証されたユーザー名を取得 + String username = authentication.getName(); + + // ユーザー情報を取得(例: userRepository経由) + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new UsernameNotFoundException("User not found")); + + List toBuysList = toBuysService.getToBuysByUserId(user.getId()); // DTO形式に変換して返す List responseList = toBuysList.stream() @@ -66,6 +82,7 @@ public class ToBuysController { ToBuyResponse resp = new ToBuyResponse(); resp.setTobuy_id(toBuy.getTobuy_id()); resp.setStuff_id(toBuy.getStuffs().getStuff_id()); + resp.setStuff_name(toBuy.getStuffs().getStuff_name()); resp.setAmount(toBuy.getAmount()); resp.setShop(toBuy.getStore()); return resp; @@ -90,6 +107,7 @@ public class ToBuysController { Map response = new HashMap<>(); + if (deletedCount > 0) { response.put("result", true); } else {