From 0e07533d8ae532a69956ee3b054d55b3514ca977 Mon Sep 17 00:00:00 2001 From: youjin Date: Tue, 24 Dec 2024 10:40:52 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EB=AA=A8=EB=8D=B8=20=EC=9E=A5?= =?UTF-8?q?=EB=B0=94=EA=B5=AC=EB=8B=88=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modellist/DataModelCartController.java | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/redunm/modellist/DataModelCartController.java b/src/main/java/com/example/redunm/modellist/DataModelCartController.java index 5bdb0a7..e1e206b 100644 --- a/src/main/java/com/example/redunm/modellist/DataModelCartController.java +++ b/src/main/java/com/example/redunm/modellist/DataModelCartController.java @@ -3,7 +3,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.List; +import java.util.*; @RestController @RequestMapping("/api/data-models") @@ -12,6 +12,14 @@ public class DataModelCartController { @Autowired private DataModelRepository dataModelRepository; + /** + * [임시] 사용자별 장바구니 목록을 저장할 Map + * - key: 사용자 ID (문자열) + * - value: 해당 사용자가 장바구니에 담은 DataModel 리스트 + */ + private Map> userCartMap = new HashMap<>(); + + //모델 생성,삭제,추가,수정 @GetMapping public List getAll() { return dataModelRepository.findAll(); @@ -19,8 +27,7 @@ public List getAll() { @GetMapping("/{id}") public DataModel getById(@PathVariable String id) { - return dataModelRepository.findById(id) - .orElse(null); + return dataModelRepository.findById(id).orElse(null); } @PostMapping @@ -38,4 +45,28 @@ public DataModel update(@PathVariable String id, @RequestBody DataModel model) { public void delete(@PathVariable String id) { dataModelRepository.deleteById(id); } + + //장바구니 기능 + @PostMapping("/cart/{userId}/{modelId}") + public List addToCart(@PathVariable String userId, + @PathVariable String modelId) { + DataModel selectedModel = dataModelRepository.findById(modelId).orElse(null); + if (selectedModel == null) { + return Collections.emptyList(); + } + + List cartList = userCartMap.getOrDefault(userId, new ArrayList<>()); + + cartList.add(selectedModel); + + userCartMap.put(userId, cartList); + + return cartList; + } + + //장바구니 목록 조회 + @GetMapping("/cart/{userId}") + public List getCart(@PathVariable String userId) { + return userCartMap.getOrDefault(userId, Collections.emptyList()); + } }