Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

[#26] 장바구니 도메인 추가 #44

Open
wants to merge 6 commits into
base: feature/39
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[26] 장바구니 관련 수정
- PUT mapping URI 설계 변경
- init 메서드와 addItem 메서드 통합
- 동시성 이슈 관련 수정
jjeda committed Dec 30, 2019
commit 57a325bc048d3c590c154bf202bb4ea8a63b77e6
25 changes: 12 additions & 13 deletions src/main/java/me/jjeda/mall/cart/controller/CartController.java
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.cart.domain.CartItem;
import me.jjeda.mall.cart.domain.CartModifyType;
import me.jjeda.mall.cart.service.CartService;
import me.jjeda.mall.common.CurrentUser;
import org.springframework.http.ResponseEntity;
@@ -13,33 +14,31 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Objects;

@Controller
@RequiredArgsConstructor
@RequestMapping("/api/carts")
public class CartController {
private final CartService cartService;

@PostMapping
public ResponseEntity initCart(@CurrentUser AccountDto accountDto) {
return ResponseEntity.ok(cartService.initCart(String.valueOf(accountDto.getEmail())));
}

@GetMapping
public ResponseEntity getCart(@CurrentUser AccountDto accountDto) {
return ResponseEntity.ok(cartService.getCart(String.valueOf(accountDto.getEmail())));
}

@PutMapping("/add")
public ResponseEntity addItem(@CurrentUser AccountDto accountDto, @RequestBody CartItem cartItem) {
return ResponseEntity.ok(cartService.addItem(String.valueOf(accountDto.getEmail()), cartItem));
}
@PutMapping
public ResponseEntity modifyCart(@CurrentUser AccountDto accountDto, @RequestBody CartItem cartItem,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런식으로 나누기보다는 /carts/items 이런식의 도메인구조면 더 역할나누기가 수월할 것 같습니다~

@RequestParam CartModifyType cartModifyType) {

@PutMapping("/remove")
public ResponseEntity removeItem(@CurrentUser AccountDto accountDto, @RequestBody CartItem cartItem) {
return ResponseEntity.ok(cartService.removeItem(String.valueOf(accountDto.getEmail()), cartItem));
if(Objects.equals(cartModifyType,CartModifyType.ADD)) {
return ResponseEntity.ok(cartService.addItem(String.valueOf(accountDto.getEmail()), cartItem));
} else {
return ResponseEntity.ok(cartService.removeItem(String.valueOf(accountDto.getEmail()), cartItem));
}
}

@DeleteMapping
public ResponseEntity deleteCart(@CurrentUser AccountDto accountDto) {
cartService.deleteCart(String.valueOf(accountDto.getId()));
5 changes: 5 additions & 0 deletions src/main/java/me/jjeda/mall/cart/domain/CartModifyType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.jjeda.mall.cart.domain;

public enum CartModifyType {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요청의 내용을 TYPE으로 분기하게 되면 API의 설계가 잘못됐을 가능성도 생각해볼 수 있습니다. 위의 리뷰를 참고하셔서 수정해보시면 좋을 것 같습니다~

ADD, REMOVE
}
23 changes: 14 additions & 9 deletions src/main/java/me/jjeda/mall/cart/service/CartService.java
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import me.jjeda.mall.cart.domain.CartItem;
import me.jjeda.mall.cart.repository.CartRedisRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import java.util.List;
@@ -14,32 +15,36 @@
public class CartService {
private final CartRedisRepository cartRedisRepository;

public Cart initCart(String id) {
if (!cartRedisRepository.existsById(id)) {
return cartRedisRepository.save(Cart.of(id));
}
return getCart(id);
}

@Transactional(readOnly = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL이 1개만 나가게되는데 트랜잭션을 걸어준 이유가 있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

트랜잭션에 대한 이해가 부족했네요..ㅎㅎ

public Cart getCart(String id) {
return cartRedisRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}

@Transactional
public Cart addItem(String id, CartItem cartItem) {
Cart cart = getCart(id);
final Cart cart;

if (!cartRedisRepository.existsById(id)) {
cart = Cart.of(id);
} else {
cart = getCart(id);
}

List<CartItem> cartItemList = cart.getCartItemList();
cartItemList.add(cartItem);
return cartRedisRepository.save(cart);
}

@Transactional
public Cart removeItem(String id, CartItem cartItem) {
Cart cart = getCart(id);
final Cart cart = getCart(id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final을 걸어주는건 괜찮지만 이 변수에만 걸어준 이유가 있을까요? 컨벤션은 일관적이면 좋을 것 같아서요~

Copy link
Collaborator Author

@jjeda jjeda Dec 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addItem 메서드 수정하다가 기계적으로 바꿨네요..
여기서는 동시성 문제가 없는 듯하여 일괄적인 컨벤션을 위해 삭제하겠습니다!

List<CartItem> cartItemList = cart.getCartItemList();
cartItemList.remove(cartItem);

return cartRedisRepository.save(cart);
}

@Transactional
public void deleteCart(String id) {
cartRedisRepository.delete(getCart(id));
}