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 ceb10df..1efa39b 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -7,9 +7,17 @@ package com.example.todoapp.controller; -import com.example.todoapp.dto.ToBuyDTO; +import com.example.todoapp.dto.ToBuyResponse; +import com.example.todoapp.dto.ToBuysDTO; +import com.example.todoapp.model.ToBuys; import com.example.todoapp.service.ToBuysService; import jakarta.validation.Valid; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -22,7 +30,7 @@ import org.springframework.web.bind.annotation.*; *

*/ @RestController -@RequestMapping("/tobuy/add") +@RequestMapping("/tobuy") public class ToBuysController { @Autowired @@ -35,9 +43,37 @@ public class ToBuysController { * @return 成功時のレスポンスメッセージ */ - @PostMapping - public ResponseEntity addToBuys(@Valid @RequestBody ToBuyDTO dto) { + @PostMapping("/add") + public ResponseEntity addToBuys(@Valid @RequestBody ToBuysDTO dto) { toBuysService.addToBuys(dto); return ResponseEntity.ok("Item added to 'To Buys' successfully"); } + + /** + * 指定されたユーザーIDに基づいてすべての「買うもの」リストを取得する + * + * @param userId ユーザーID + * @return ユーザーに紐づく「買うもの」リスト + */ + @GetMapping("/get") + public ResponseEntity getAllToBuysByUserId(@RequestParam Long user_id) { + List toBuysList = toBuysService.getToBuysByUserId(user_id); + + // DTO形式に変換して返す + List responseList = toBuysList.stream() + .map(toBuy -> { + ToBuyResponse resp = new ToBuyResponse(); + resp.setTobuy_id(toBuy.getTobuy_id()); + resp.setStuff_id(toBuy.getStuffs().getStuff_id()); + resp.setAmount(toBuy.getAmount()); + resp.setShop(toBuy.getStore()); + return resp; + }) + .collect(Collectors.toList()); + + Map responseBody = new HashMap<>(); + responseBody.put("tobuy_array", responseList); + + return ResponseEntity.ok(responseList); + } } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java new file mode 100644 index 0000000..2ffab6e --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java @@ -0,0 +1,12 @@ +// src/main/java/com/example/todoapp/dto/ToBuyResponse.java +package com.example.todoapp.dto; + +import lombok.Data; + +@Data +public class ToBuyResponse { + private int tobuy_id; + private Long stuff_id; + private int amount; + private String shop; +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuyDTO.java b/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java similarity index 93% rename from backend/src/main/java/com/example/todoapp/dto/ToBuyDTO.java rename to backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java index 6212f7d..90830fd 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuyDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java @@ -9,7 +9,7 @@ import lombok.Data; */ @Data - public class ToBuyDTO { + public class ToBuysDTO { private Long stuff_id; private Long user_id; diff --git a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java index 368aef8..49029ea 100644 --- a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -8,7 +8,11 @@ package com.example.todoapp.repository; import com.example.todoapp.model.ToBuys; + +import java.util.List; + import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; /** @@ -20,4 +24,13 @@ import org.springframework.stereotype.Repository; */ @Repository public interface ToBuysRepository extends JpaRepository { + /** + * 指定されたユーザーIDに基づいて「買うもの」リストを取得する + * + * @param userId ユーザーID + * @return 「買うもの」リスト + */ + + @Query("SELECT t FROM ToBuys t WHERE t.user_id.id = ?1") + List findByUserId(Long user_id); } \ No newline at end of file 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 475879f..d1cd65c 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -8,7 +8,7 @@ package com.example.todoapp.service; -import com.example.todoapp.dto.ToBuyDTO; +import com.example.todoapp.dto.ToBuysDTO; import com.example.todoapp.model.Stuffs; import com.example.todoapp.model.ToBuys; import com.example.todoapp.model.User; @@ -18,6 +18,7 @@ import com.example.todoapp.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; import java.util.Optional; /** @@ -46,11 +47,11 @@ public class ToBuysService { * * @param toBuyDTO 追加する購入アイテムのデータ(DTO) */ - public void addToBuys(ToBuyDTO toBuyDTO) { + public void addToBuys(ToBuysDTO toBuyDTO) { // ユーザー情報を取得 User user = userRepository.findById(toBuyDTO.getUser_id()) - .orElseThrow(() -> new RuntimeException("用户不存在: " + toBuyDTO.getUser_id())); + .orElseThrow(() -> new RuntimeException("ユーザーがありませ: " + toBuyDTO.getUser_id())); Stuffs stuffs; if (toBuyDTO.getStuff_id() == null) { @@ -63,13 +64,12 @@ public class ToBuysService { // 材料情報を取得 Optional optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); if (!optionalStuffs.isPresent()) { - throw new RuntimeException("材料不存在"); + throw new RuntimeException("材料がありません"); } stuffs = optionalStuffs.get(); } ToBuys toBuys = new ToBuys(); - System.out.println("AAAAuser_id: " + toBuyDTO.getUser_id()); toBuys.setUser_id(user); toBuys.setStuffs(stuffs); toBuys.setAmount(toBuyDTO.getAmount()); @@ -79,4 +79,14 @@ public class ToBuysService { toBuysRepository.save(toBuys); } + + /** + * 指定されたユーザーIDに基づいてすべての「買うもの」リストを取得する + * + * @param userId ユーザーID + * @return ユーザーに紐づく「買うもの」リスト + */ + public List getToBuysByUserId(Long userId) { + return toBuysRepository.findByUserId(userId); + } } \ No newline at end of file