forked from 9oormthon-univ/2024_DANPOONG_TEAM_5_BE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 장바구니 추가 기능 구현 (9oormthon-univ#41)
- Loading branch information
1 parent
a9f6131
commit bede955
Showing
7 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/com/jangburich/domain/order/application/OrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.jangburich.domain.order.application; | ||
|
||
import com.jangburich.domain.menu.domain.Menu; | ||
import com.jangburich.domain.menu.domain.repository.MenuRepository; | ||
import com.jangburich.domain.order.domain.Cart; | ||
import com.jangburich.domain.order.domain.repository.CartRepository; | ||
import com.jangburich.domain.order.domain.repository.OrdersRepository; | ||
import com.jangburich.domain.order.dto.request.AddCartRequest; | ||
import com.jangburich.domain.store.domain.Store; | ||
import com.jangburich.domain.store.domain.repository.StoreRepository; | ||
import com.jangburich.domain.user.domain.User; | ||
import com.jangburich.domain.user.domain.repository.UserRepository; | ||
import com.jangburich.global.payload.Message; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class OrderService { | ||
|
||
private final CartRepository cartRepository; | ||
private final UserRepository userRepository; | ||
private final MenuRepository menuRepository; | ||
private final StoreRepository storeRepository; | ||
|
||
@Transactional | ||
public Message addCart(String userProviderId, AddCartRequest addCartRequest) { | ||
User user = userRepository.findByProviderId(userProviderId) | ||
.orElseThrow(() -> new NullPointerException()); | ||
|
||
Menu menu = menuRepository.findById(addCartRequest.menuId()) | ||
.orElseThrow(() -> new IllegalArgumentException("등록된 메뉴를 찾을 수 없습니다.")); | ||
|
||
System.out.println("menu.getId() = " + menu.getId()); | ||
System.out.println("user.getUserId() = " + user.getUserId()); | ||
|
||
Optional<Cart> optionalCart = cartRepository.findByUserIdAndMenuId(user.getUserId(), menu.getId()); | ||
|
||
Store store = storeRepository.findById(addCartRequest.storeId()) | ||
.orElseThrow(() -> new IllegalArgumentException("유효하지 않은 가게 id 입니다.")); | ||
|
||
if (optionalCart.isPresent()) { | ||
Cart existingCart = optionalCart.get(); | ||
existingCart.updateQuantity(addCartRequest.quantity()); | ||
|
||
return Message.builder() | ||
.message("장바구니에 상품을 추가했습니다.") | ||
.build(); | ||
} | ||
|
||
Cart newCart = Cart.builder() | ||
.quantity(addCartRequest.quantity()) | ||
.menu(menu) | ||
.user(user) | ||
.store(store) | ||
.orders(null) | ||
.build(); | ||
|
||
cartRepository.save(newCart); | ||
|
||
return Message.builder() | ||
.message("장바구니에 상품을 추가했습니다.") | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/jangburich/domain/order/domain/repository/CartRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.jangburich.domain.order.domain.repository; | ||
|
||
import com.jangburich.domain.order.domain.Cart; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CartRepository extends JpaRepository<Cart, Long> { | ||
@Query("SELECT c FROM Cart c WHERE c.user.userId = :userId AND c.menu.id = :menuId") | ||
Optional<Cart> findByUserIdAndMenuId(@Param("userId") Long userId, @Param("menuId") Long menuId); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/jangburich/domain/order/domain/repository/OrdersRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.jangburich.domain.order.domain.repository; | ||
|
||
import com.jangburich.domain.order.domain.Orders; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface OrdersRepository extends JpaRepository<Orders, Long> { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/jangburich/domain/order/dto/request/AddCartRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.jangburich.domain.order.dto.request; | ||
|
||
public record AddCartRequest( | ||
Long storeId, | ||
Long menuId, | ||
int quantity | ||
) { | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/jangburich/domain/order/presentation/OrderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.jangburich.domain.order.presentation; | ||
|
||
import com.jangburich.domain.order.application.OrderService; | ||
import com.jangburich.domain.order.dto.request.AddCartRequest; | ||
import com.jangburich.global.payload.Message; | ||
import com.jangburich.global.payload.ResponseCustom; | ||
import com.jangburich.utils.parser.AuthenticationParser; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Order", description = "Order API") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/orders") | ||
public class OrderController { | ||
|
||
private final OrderService orderService; | ||
|
||
@Operation(summary = "장바구니 담기", description = "장바구니에 물건과 수량을 담습니다.") | ||
@PostMapping("/cart") | ||
public ResponseCustom<Message> addCart( | ||
Authentication authentication, | ||
@RequestBody AddCartRequest addCartRequest | ||
) { | ||
return ResponseCustom.OK(orderService.addCart(AuthenticationParser.parseUserId(authentication), addCartRequest)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters