Skip to content
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

πŸ§ͺ [Unit Test] Item μœ λ‹›ν…ŒμŠ€νŠΈ μΆ”κ°€ 및 μžλ°”λ… μΆ”κ°€ 및 μžμž˜ν•œ μ½”λ“œ 닀듬기 #507

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,29 @@ public class ItemAdminService {
private final ItemAdminRepository itemAdminRepository;
private final AsyncNewItemImageUploader asyncNewItemImageUploader;

/**
* <p>Item νžˆμŠ€ν† λ¦¬λ₯Ό λ°˜ν™˜ν•œλ‹€.</p>
* @param pageable
* @return
*/
@Transactional(readOnly = true)
public ItemListResponseDto getAllItemHistory(Pageable pageable) {
Page<ItemHistoryResponseDto> responseDtos = itemAdminRepository.findAll(pageable)
.map(ItemHistoryResponseDto::new);
return new ItemListResponseDto(responseDtos.getContent(), responseDtos.getTotalPages());
}

// μ•„μ΄ν…œ μˆ˜μ • μ‹œ μ‹ κ·œ 이미지가 μ‘΄μž¬ν•˜λŠ” 경우
/**
* <p>μ•„μ΄ν…œ μˆ˜μ • μ‹œ μ‹ κ·œ 이미지가 μ‘΄μž¬ν•˜λŠ” 경우</p>
* <p>κΈ°μ‘΄ μ•„μ΄ν…œμ˜ item.isVisible λ₯Ό false 둜 λ³€κ²½ν•œλ‹€</p>
* @param itemId νƒ€κ²Ÿ μ•„μ΄ν…œ
* @param itemUpdateRequestDto νƒ€κ²Ÿ μ•„μ΄ν…œ λ³€κ²½ dto
* @param itemImageFile μ‹ κ·œ 이미지
* @param user λ°”κΎΈλŠ” μœ μ € id
* @throws IOException IOException
* @throws ItemNotFoundException μ•„μ΄ν…œ μ—†μŒ
* @throws ItemNotAvailableException μ ‘κ·Ό λΆˆκ°€ν•œ μ•„μ΄ν…œ
*/
@Transactional
public void updateItem(Long itemId, ItemUpdateRequestDto itemUpdateRequestDto,
MultipartFile itemImageFile, UserDto user) throws IOException {
Expand All @@ -50,10 +65,17 @@ public void updateItem(Long itemId, ItemUpdateRequestDto itemUpdateRequestDto,
itemAdminRepository.save(newItem);
}

// μ•„μ΄ν…œ μˆ˜μ • μ‹œ μ‹ κ·œ 이미지가 μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 경우
/**
* <p>μ•„μ΄ν…œ μˆ˜μ • μ‹œ μ‹ κ·œ 이미지가 μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 경우</p>
* <p>κΈ°μ‘΄ μ•„μ΄ν…œμ˜ item.isVisible λ₯Ό false 둜 λ³€κ²½ν•œλ‹€</p>
* @param itemId νƒ€κ²Ÿ μ•„μ΄ν…œ
* @param itemUpdateRequestDto νƒ€κ²Ÿ μ•„μ΄ν…œ λ³€κ²½ dto
* @param user λ°”κΎΈλŠ” μœ μ € id
* @throws ItemNotFoundException μ•„μ΄ν…œ μ—†μŒ
* @throws ItemNotAvailableException μ ‘κ·Ό λΆˆκ°€ν•œ μ•„μ΄ν…œ
*/
@Transactional
public void updateItem(Long itemId, ItemUpdateRequestDto itemUpdateRequestDto,
UserDto user) {
public void updateItem(Long itemId, ItemUpdateRequestDto itemUpdateRequestDto, UserDto user) {
Item item = itemAdminRepository.findById(itemId).orElseThrow(ItemNotFoundException::new);
if (!item.getIsVisible()) {
throw new ItemNotAvailableException();
Expand All @@ -63,6 +85,13 @@ public void updateItem(Long itemId, ItemUpdateRequestDto itemUpdateRequestDto,
itemAdminRepository.save(newItem);
}

/**
* <p>μ•„μ΄ν…œ μ‚­μ œ</p>
* <p>item.isVisible λ₯Ό false 둜 λ³€κ²½ν•œλ‹€</p>
* @param itemId νƒ€κ²Ÿ id
* @param user μ‚­μ œν•˜λŠ” μœ μ €
* @throws ItemNotFoundException μ•„μ΄ν…œ μ—†μŒ
*/
@Transactional
public void deleteItem(Long itemId, UserDto user) {
Item item = itemAdminRepository.findById(itemId).orElseThrow(ItemNotFoundException::new);
Expand Down
87 changes: 62 additions & 25 deletions src/main/java/com/gg/server/domain/item/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,43 @@ public class ItemService {
private final NotiService notiService;
private final UserCoinChangeService userCoinChangeService;

/**
* <p>λͺ¨λ“  μ•„μ΄ν…œμ„ κ°€μ Έμ˜¨λ‹€.</p>
* @return
*/
@Transactional(readOnly = true)
public ItemStoreListResponseDto getAllItems() {

List<ItemStoreResponseDto> itemStoreListResponseDto = itemRepository.findAllByCreatedAtDesc()
.stream().map(ItemStoreResponseDto::new).collect(Collectors.toList());
return new ItemStoreListResponseDto(itemStoreListResponseDto);
}

/**
* <p>게슀트 μœ μ €κ°€ μ•„λ‹Œ μœ μ €κ°€ μ•„μ΄ν…œμ„ κ΅¬λ§€ν•˜λŠ” λ©”μ„œλ“œ 이닀</p>
* <p>할인 쀑 이라면 할인가λ₯Ό μ μš©ν•˜κ³ , ꡬ맀 ν›„ μ˜μˆ˜μ¦μ„ db에 μ €μž₯ν•œλ‹€.</p>
* @param itemId νƒ€κ²Ÿ μ•„μ΄ν…œ Id
* @param userDto ꡬ맀 μœ μ € 정보
* @throws ItemNotFoundException μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” μ•„μ΄ν…œ
* @throws ItemNotPurchasableException ꡬ맀할 수 μ—†λŠ” μ•„μ΄ν…œ
* @throws UserNotFoundException νƒ€κ²Ÿ μœ μ € μ—†μŒ
* @throws KakaoPurchaseException 게슀트(카카였) μœ μ €κ°€ μ•„μ΄ν…œμ„ κ΅¬λ§€ν•˜λ € ν• λ•Œ
* @throws
*/
@Transactional
public void purchaseItem(Long itemId, UserDto userDto) {
Item item = itemRepository.findById(itemId)
.orElseThrow(() -> {
throw new ItemNotFoundException();
});
Item item = itemRepository.findById(itemId).orElseThrow(ItemNotFoundException::new);
if (!item.getIsVisible()) {
throw new ItemNotPurchasableException();
}

//세일가격 μ‘΄μž¬ν• λ•Œ μ„ΈμΌκ°€λ‘œ κ²°μ •
Integer finalPrice;
Integer finalPrice = item.getPrice();
if (item.getDiscount() != null && item.getDiscount() > 0) {
finalPrice = item.getPrice() - (item.getPrice() * item.getDiscount() / 100);
} else {
finalPrice = item.getPrice();
finalPrice -= (item.getPrice() * item.getDiscount() / 100);
}

User payUser = userRepository.findById(userDto.getId())
.orElseThrow(() -> new UserNotFoundException());
.orElseThrow(UserNotFoundException::new);

if (payUser.getRoleType() == RoleType.GUEST) {
throw new KakaoPurchaseException();
Expand All @@ -88,22 +97,29 @@ public void purchaseItem(Long itemId, UserDto userDto) {
receiptRepository.save(receipt);
}

/**
* <p>게슀트 μœ μ €κ°€ μ•„λ‹Œ μœ μ €λ“€ 끼리 선물을 μ£ΌλŠ” λ©”μ„œλ“œ 이닀</p>
* <p>할인 쀑 이라면 할인가λ₯Ό μ μš©ν•˜κ³ , ꡬ맀 ν›„ μ˜μˆ˜μ¦μ„ db에 μ €μž₯ν•œλ‹€.</p>
* @param itemId νƒ€κ²Ÿ μ•„μ΄ν…œ id
* @param ownerId μ„ λ¬Ό λ°›λŠ” owner intraId
* @param userDto ꡬ맀자 id
* @throws ItemNotFoundException μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” μ•„μ΄ν…œ
* @throws ItemNotPurchasableException ꡬ맀할 수 μ—†λŠ” μ•„μ΄ν…œ
* @throws UserNotFoundException νƒ€κ²Ÿ μœ μ € μ—†μŒ
* @throws KakaoPurchaseException 게슀트(카카였) μœ μ €κ°€ μ•„μ΄ν…œμ„ κ΅¬λ§€ν•˜λ € ν• λ•Œ
* @throws KakaoGiftException 게슀트(카카였) μœ μ €μ—κ²Œ μ„ λ¬Όν•˜λ € ν• λ•Œ
*/
@Transactional
public void giftItem(Long itemId, String ownerId, UserDto userDto) {
Item item = itemRepository.findById(itemId)
.orElseThrow(() -> {
throw new ItemNotFoundException();
});
Item item = itemRepository.findById(itemId).orElseThrow(ItemNotFoundException::new);
if (!item.getIsVisible()) {
throw new ItemNotPurchasableException();
}

//세일가격 μ‘΄μž¬ν• λ•Œ μ„ΈμΌκ°€λ‘œ κ²°μ •
Integer finalPrice;
Integer finalPrice = item.getPrice();
if (item.getDiscount() != null && item.getDiscount() > 0) {
finalPrice = item.getPrice() - (item.getPrice() * item.getDiscount() / 100);
} else {
finalPrice = item.getPrice();
finalPrice -= (item.getPrice() * item.getDiscount() / 100);
}

User payUser = userRepository.findById(userDto.getId())
Expand All @@ -128,34 +144,55 @@ public void giftItem(Long itemId, String ownerId, UserDto userDto) {
notiService.createGiftNoti(owner, payUser, item.getName());
}

/**
* <p>μœ μ €κ°€ κ΅¬λ§€ν•œ μ•„μ΄ν…œ 쀑 μƒνƒœκ°€ BEFORE, USING, WAITING 인 것듀을 μ°ΎλŠ” λ©”μ„œλ“œμ΄λ‹€.</p>
* @param userDto μœ μ € 정보
* @param pageable νŽ˜μ΄μ§€
* @return
*/
@Transactional(readOnly = true)
public UserItemListResponseDto getItemByUser(UserDto userDto, Pageable pageable) {
Page<Receipt> receipts = userItemRepository.findByOwnerIntraId(userDto.getIntraId(), pageable);
Page<UserItemResponseDto> responseDtos = receipts.map(UserItemResponseDto::new);
return new UserItemListResponseDto(responseDtos.getContent(), responseDtos.getTotalPages());
Page<UserItemResponseDto> responseDto = receipts.map(UserItemResponseDto::new);
return new UserItemListResponseDto(responseDto.getContent(), responseDto.getTotalPages());
}

/**
* <p>ν•΄λ‹Ή μ•„μ΄ν…œμ˜ 주인이 λ§žλŠ”μ§€ μ²΄ν¬ν•œλ‹€.</p>
* @param loginUser 둜그인 μœ μ €
* @param receipt 영수증
*/
public void checkItemOwner(User loginUser, Receipt receipt) {
if (!receipt.getOwnerIntraId().equals(loginUser.getIntraId())) {
throw new ReceiptNotOwnerException();
}
}

/**
* <p>ν•΄λ‹Ή μ•„μ΄ν…œμ˜ νƒ€μž…μ΄ 영수증과 λ§žλŠ”μ§€ μ²΄ν¬ν•œλ‹€.</p>
* @param receipt 영수증
* @param itemType μ•„μ΄ν…œ νƒ€μž…
*/
public void checkItemType(Receipt receipt, ItemType itemType) {
if (!receipt.getItem().getType().equals(itemType)) {
throw new ItemTypeException();
}
}

/**
* <p>μ•„μ΄ν…œμ˜ μƒνƒœλ₯Ό μ²΄ν¬ν•œλ‹€.</p>
* <p>메가폰인데 ItemStatus.WAITING κ°€ μ•„λ‹ˆκ±°λ‚˜ μ‚¬μš©μ€‘μ΄λΌλ©΄ μ˜ˆμ™Έ λ°œμƒ</p>
* <p>메가폰이 μ•„λ‹Œ μ•„μ΄ν…œμΈλ° ItemStatus.BEFORE 인경우 μ˜ˆμ™Έ λ°œμƒ</p>
* @param receipt
*/
public void checkItemStatus(Receipt receipt) {
if (receipt.getItem().getType().equals(ItemType.MEGAPHONE)) {
if (!(receipt.getStatus().equals(ItemStatus.WAITING) || receipt.getStatus().equals(ItemStatus.USING))) {
throw new ItemStatusException();
}
} else {
if (!receipt.getStatus().equals(ItemStatus.BEFORE)) {
if (!(receipt.getStatus().equals(ItemStatus.WAITING)
|| receipt.getStatus().equals(ItemStatus.USING))) {
throw new ItemStatusException();
}
} else if (!receipt.getStatus().equals(ItemStatus.BEFORE)) {
throw new ItemStatusException();
}
}
}
Loading
Loading