-
Notifications
You must be signed in to change notification settings - Fork 0
feat : 모델 장바구니 조회 PR #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,15 +12,22 @@ public class DataModelCartController { | |
| @Autowired | ||
| private DataModelRepository dataModelRepository; | ||
|
|
||
| /** | ||
| * [임시] 사용자별 장바구니 목록을 저장할 Map | ||
| * - key: 사용자 ID (문자열) | ||
| * - value: 해당 사용자가 장바구니에 담은 DataModel 리스트 | ||
| */ | ||
| private Map<String, List<DataModel>> userCartMap = new HashMap<>(); | ||
|
|
||
| //모델 생성,삭제,추가,수정 | ||
| @GetMapping | ||
| public List<DataModel> getAll() { | ||
| return dataModelRepository.findAll(); | ||
| } | ||
|
|
||
| @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<DataModel> addToCart(@PathVariable String userId, | ||
| @PathVariable String modelId) { | ||
| DataModel selectedModel = dataModelRepository.findById(modelId).orElse(null); | ||
| if (selectedModel == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| List<DataModel> cartList = userCartMap.getOrDefault(userId, new ArrayList<>()); | ||
|
|
||
| cartList.add(selectedModel); | ||
|
|
||
| userCartMap.put(userId, cartList); | ||
|
|
||
| return cartList; | ||
| } | ||
|
|
||
| //장바구니 목록 조회 | ||
| @GetMapping("/cart/{userId}") | ||
| public List<DataModel> getCart(@PathVariable String userId) { | ||
| return userCartMap.getOrDefault(userId, Collections.emptyList()); | ||
| } | ||
|
Comment on lines
+67
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification The verification reveals that private Map<String, List<DataModel>> userCartMap = new HashMap<>();This implementation is not thread-safe and could lead to data races when:
Recommendations:
🔗 Analysis chainVerify cart retrieval in concurrent scenarios. Below is a shell script you could use to check for concurrency references in your codebase for better insights: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Searching for concurrency-related annotations or logic in the project.
rg -A 5 "synchronized|volatile|ConcurrentHashMap|Lock|ReadWriteLock|@Async|@Scheduled"
Length of output: 88 Script: #!/bin/bash
# Let's check the implementation of userCartMap to understand its thread-safety
ast-grep --pattern 'userCartMap'
# Also check for any Map initialization or declaration
rg -l "Map[<].*[>].*=.*new"
Length of output: 840 |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review concurrency implications of shared map usage.
Because
userCartMapis a simpleHashMapthat is being modified, consider using a concurrent data structure or synchronizing access if multiple threads can call these endpoints simultaneously (which is common in a Spring Boot application).For instance, switching to a thread-safe structure like
ConcurrentHashMapcould prevent race conditions.📝 Committable suggestion