stuff_nameを追加

backend-tobuy-adddish
zhang.pengcheng 5 months ago
parent ddec6a2e23
commit 7d173a03dc
  1. 22
      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<ToBuys> 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<ToBuys> toBuysList = toBuysService.getToBuysByUserId(user.getId());
// DTO形式に変換して返す
List<ToBuyResponse> 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<String, Boolean> response = new HashMap<>();
if (deletedCount > 0) {
response.put("result", true);
} else {

Loading…
Cancel
Save