Skip to content

Commit

Permalink
Merge pull request #167 from THEGOODs-repo/bug/166
Browse files Browse the repository at this point in the history
[Bug/166] μž₯λ°”κ΅¬λ‹ˆ μƒν’ˆ μ‚­μ œ API 둜직 μˆ˜μ •
  • Loading branch information
Eunjin3395 authored Mar 27, 2024
2 parents 339dd3a + bcb2189 commit ed78e2e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public enum ErrorStatus implements BaseErrorCode {
NOT_CART_OWNER(HttpStatus.BAD_REQUEST, "CART4002", "본인의 μž₯λ°”κ΅¬λ‹ˆ 내역이 μ•„λ‹™λ‹ˆλ‹€. μ ‘κ·Όν•  수 μ—†μŠ΅λ‹ˆλ‹€."),
CART_DETAIL_FOUND_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "CART4003", "단일 μƒν’ˆμ˜ μž₯λ°”κ΅¬λ‹ˆ 상세 내역을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€. κ΄€λ¦¬μžμ—κ²Œ 문의 λ°”λžλ‹ˆλ‹€."),
CART_DETAIL_NOT_FOUND(HttpStatus.NOT_FOUND, "CART4004", "ν•΄λ‹Ή μž₯λ°”κ΅¬λ‹ˆ 상세 내역을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€"),
DELETE_CART_DETAIL_FAILED(HttpStatus.BAD_REQUEST, "CART4005", "μž₯λ°”κ΅¬λ‹ˆ 상세 내역을 μ‚­μ œν•  수 μ—†μŠ΅λ‹ˆλ‹€."),

// test
TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "ν…ŒμŠ€νŠΈ"),
Expand All @@ -61,11 +62,11 @@ public enum ErrorStatus implements BaseErrorCode {

//JWT

JWT_BAD_REQUEST(HttpStatus.UNAUTHORIZED, "JWT4001","잘λͺ»λœ JWT μ„œλͺ…μž…λ‹ˆλ‹€."),
JWT_ACCESS_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "JWT4002","μ•‘μ„ΈμŠ€ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€."),
JWT_REFRESH_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "JWT4003","λ¦¬ν”„λ ˆμ‹œ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ λ‘œκ·ΈμΈν•˜μ‹œκΈ° λ°”λžλ‹ˆλ‹€."),
JWT_UNSUPPORTED_TOKEN(HttpStatus.UNAUTHORIZED, "JWT4004","μ§€μ›ν•˜μ§€ μ•ŠλŠ” JWT ν† ν°μž…λ‹ˆλ‹€."),
JWT_TOKEN_NOT_FOUND(HttpStatus.UNAUTHORIZED, "JWT4005","μœ νš¨ν•œ JWT 토큰이 μ—†μŠ΅λ‹ˆλ‹€."),
JWT_BAD_REQUEST(HttpStatus.UNAUTHORIZED, "JWT4001", "잘λͺ»λœ JWT μ„œλͺ…μž…λ‹ˆλ‹€."),
JWT_ACCESS_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "JWT4002", "μ•‘μ„ΈμŠ€ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€."),
JWT_REFRESH_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "JWT4003", "λ¦¬ν”„λ ˆμ‹œ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ λ‘œκ·ΈμΈν•˜μ‹œκΈ° λ°”λžλ‹ˆλ‹€."),
JWT_UNSUPPORTED_TOKEN(HttpStatus.UNAUTHORIZED, "JWT4004", "μ§€μ›ν•˜μ§€ μ•ŠλŠ” JWT ν† ν°μž…λ‹ˆλ‹€."),
JWT_TOKEN_NOT_FOUND(HttpStatus.UNAUTHORIZED, "JWT4005", "μœ νš¨ν•œ JWT 토큰이 μ—†μŠ΅λ‹ˆλ‹€."),

//POST

Expand Down
17 changes: 0 additions & 17 deletions src/main/java/com/umc/TheGoods/domain/order/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,4 @@ public void setItem(Item item) {
item.getItemCartList().add(this);
}

public void detachMember() {
if (this.member != null) {
this.member.getCartList().remove(this);
}
}

public void detachItem() {
if (this.item != null) {
this.item.getItemCartList().remove(this);
}
}

public void detachCartDetail() {
this.cartDetailList = new ArrayList<>();
}


}
2 changes: 2 additions & 0 deletions src/main/java/com/umc/TheGoods/domain/order/CartDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public void setCart(Cart cart) {
public void detachCart() {
if (this.cart != null) {
this.cart.getCartDetailList().remove(this);
this.cart = null;
}
}

public void detachItemOption() {
if (this.itemOption != null) {
this.itemOption.getCartDetailList().remove(this);
this.itemOption = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import com.umc.TheGoods.domain.member.Member;
import com.umc.TheGoods.domain.order.Cart;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;
Expand All @@ -17,7 +14,4 @@ public interface CartRepository extends JpaRepository<Cart, Long> {

List<Cart> findAllByMember(Member member);

@Modifying
@Query("delete from Cart c where c.id=:id")
void deleteCartById(@Param("id") Long cartId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,31 +172,45 @@ public List<CartDetail> updateCart(CartRequestDTO.cartUpdateDTOList request, Mem
@Override
public void deleteCartDetail(CartRequestDTO.cartDetailDeleteDTO request, Member member) {

request.getCartDetailIdList().forEach(cartDetailId -> {
Cart firstCart = cartDetailRepository.findById(request.getCartDetailIdList().get(0)).orElseThrow(() -> new OrderHandler(ErrorStatus.CART_DETAIL_NOT_FOUND)).getCart();

// request의 cartDetailIdList둜 cartDetailList 생성
List<CartDetail> cartDetailList = request.getCartDetailIdList().stream().map(cartDetailId -> {
CartDetail cartDetail = cartDetailRepository.findById(cartDetailId).orElseThrow(() -> new OrderHandler(ErrorStatus.CART_DETAIL_NOT_FOUND));

Cart cart = cartDetail.getCart();

// ν•΄λ‹Ή cart 내역을 μˆ˜μ •ν•  κΆŒν•œ μžˆλŠ”μ§€ 검증
if (!cartDetail.getCart().getMember().equals(member)) {
throw new OrderHandler(ErrorStatus.NOT_CART_OWNER);
}
cartDetail.detachCart();
cartDetail.detachItemOption();

cartDetailRepository.deleteById(cartDetail.getId());
cartDetailRepository.flush();

// μž₯λ°”κ΅¬λ‹ˆ μƒν’ˆμ˜ λ§ˆμ§€λ§‰ μ˜΅μ…˜ 내역을 μ‚­μ œν•œ 경우: μž₯λ°”κ΅¬λ‹ˆ μƒν’ˆ 내역도 μ‚­μ œ
if (cart.getCartDetailList().isEmpty()) {
log.info(" ===================== λ§ˆμ§€λ§‰ μ˜΅μ…˜ μ‚­μ œ ========================");
cart.detachMember();
cart.detachItem();
cartRepository.deleteCartById(cart.getId());

// cartDetailIdκ°€ λͺ¨λ‘ λ™μΌν•œ μž₯λ°”κ΅¬λ‹ˆ 내역에 λŒ€ν•œ 담은 μ˜΅μ…˜μ΄ λ§žλŠ”μ§€ 검증
if (!firstCart.equals(cartDetail.getCart())) {
throw new OrderHandler(ErrorStatus.DELETE_CART_DETAIL_FAILED);
}
});
return cartDetail;
}).collect(Collectors.toList());


// ν•΄λ‹Ή μž₯λ°”κ΅¬λ‹ˆ μƒν’ˆμ˜ λͺ¨λ“  담은 μ˜΅μ…˜ list 쑰회
List<CartDetail> originCartDetailList = firstCart.getCartDetailList();

List<Long> cartDetailIdList = cartDetailList.stream().map(cartDetail -> {
return cartDetail.getId();
}).collect(Collectors.toList());
List<Long> originCartDetailIdList = originCartDetailList.stream().map(originCartDetail -> {
return originCartDetail.getId();
}).collect(Collectors.toList());

// μž₯λ°”κ΅¬λ‹ˆ μ‚­μ œ
if (isEqualCartDetailList(cartDetailIdList, originCartDetailIdList)) { // ν•΄λ‹Ή μž₯λ°”κ΅¬λ‹ˆ μƒν’ˆ λ‚΄μ—­μ˜ 담은 μ˜΅μ…˜ λͺ¨λ‘λ₯Ό μ‚­μ œν•˜λŠ” 경우
cartRepository.deleteById(firstCart.getId());
} else { // ν•΄λ‹Ή μž₯λ°”κ΅¬λ‹ˆ μƒν’ˆ λ‚΄μ—­μ˜ 담은 μ˜΅μ…˜ μΌλΆ€λ§Œ μ‚­μ œν•˜λŠ” 경우
cartDetailList.forEach(cartDetail -> {
cartDetail.detachCart();
cartDetail.detachItemOption();
cartDetailRepository.deleteById(cartDetail.getId());
});
}
}

@Override
Expand All @@ -209,29 +223,23 @@ public void deleteCart(CartRequestDTO.cartDeleteDTO request, Member member) {
throw new OrderHandler(ErrorStatus.NOT_CART_OWNER);
}

List<CartDetail> cartDetailList = cartDetailRepository.findAllByCart(cart);
cartDetailList.forEach(cartDetail -> {
log.info("===== cartDetailId: {}", cartDetail.getId());
cartDetail.detachCart();
cartDetail.detachItemOption();
cartRepository.deleteById(cart.getId());
});

cartDetailRepository.deleteById(cartDetail.getId());
});
}

// cart.getCartDetailList().forEach(cartDetail -> {
// log.info("===== cartDetailId: {}", cartDetail.getId());
// cartDetail.detachCart();
// cartDetail.detachItemOption();
//
// cartDetailRepository.deleteById(cartDetail.getId());
// });
private boolean isEqualCartDetailList(List<Long> x, List<Long> y) {
if (x == null) {
return y == null;
}

cartDetailRepository.flush();
if (x.size() != y.size()) {
return false;
}

cart.detachMember();
cart.detachItem();
cartRepository.deleteCartById(cart.getId());
});
x = x.stream().sorted().collect(Collectors.toList());
y = y.stream().sorted().collect(Collectors.toList());

return x.equals(y);
}
}

0 comments on commit ed78e2e

Please sign in to comment.