Skip to content

Commit

Permalink
merge develop into
Browse files Browse the repository at this point in the history
  • Loading branch information
hongdosan committed Nov 28, 2023
2 parents a14e084 + eeda751 commit b801572
Show file tree
Hide file tree
Showing 30 changed files with 613 additions and 119 deletions.
36 changes: 27 additions & 9 deletions src/main/java/com/moabam/api/application/bug/BugMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BugMapper {

public static BugHistory toUseBugHistory(Long memberId, BugType bugType, int quantity) {
return BugHistory.builder()
.memberId(memberId)
.bugType(bugType)
.actionType(BugActionType.USE)
.quantity(quantity)
.build();
}

public static BugResponse toBugResponse(Bug bug) {
return BugResponse.builder()
.morningBug(bug.getMorningBug())
Expand All @@ -54,6 +45,15 @@ public static BugHistoryResponse toBugHistoryResponse(List<BugHistoryWithPayment
.build();
}

public static BugHistory toUseBugHistory(Long memberId, BugType bugType, int quantity) {
return BugHistory.builder()
.memberId(memberId)
.bugType(bugType)
.actionType(BugActionType.USE)
.quantity(quantity)
.build();
}

public static BugHistory toChargeBugHistory(Long memberId, int quantity) {
return BugHistory.builder()
.memberId(memberId)
Expand All @@ -62,4 +62,22 @@ public static BugHistory toChargeBugHistory(Long memberId, int quantity) {
.quantity(quantity)
.build();
}

public static BugHistory toRewardBugHistory(Long memberId, BugType bugType, int quantity) {
return BugHistory.builder()
.memberId(memberId)
.bugType(bugType)
.actionType(BugActionType.REWARD)
.quantity(quantity)
.build();
}

public static BugHistory toCouponBugHistory(Long memberId, BugType bugType, int quantity) {
return BugHistory.builder()
.memberId(memberId)
.bugType(bugType)
.actionType(BugActionType.COUPON)
.quantity(quantity)
.build();
}
}
51 changes: 44 additions & 7 deletions src/main/java/com/moabam/api/application/bug/BugService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.coupon.CouponService;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.payment.PaymentMapper;
import com.moabam.api.application.product.ProductMapper;
import com.moabam.api.domain.bug.Bug;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.bug.repository.BugHistoryRepository;
import com.moabam.api.domain.bug.repository.BugHistorySearchRepository;
import com.moabam.api.domain.coupon.CouponWallet;
import com.moabam.api.domain.coupon.repository.CouponWalletSearchRepository;
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.payment.Payment;
import com.moabam.api.domain.payment.repository.PaymentRepository;
import com.moabam.api.domain.product.Product;
Expand All @@ -28,6 +30,7 @@
import com.moabam.api.dto.product.PurchaseProductRequest;
import com.moabam.api.dto.product.PurchaseProductResponse;
import com.moabam.global.error.exception.NotFoundException;
import com.moabam.global.error.model.ErrorMessage;

import lombok.RequiredArgsConstructor;

Expand All @@ -37,14 +40,14 @@
public class BugService {

private final MemberService memberService;
private final CouponService couponService;
private final BugHistoryRepository bugHistoryRepository;
private final BugHistorySearchRepository bugHistorySearchRepository;
private final ProductRepository productRepository;
private final PaymentRepository paymentRepository;
private final CouponWalletSearchRepository couponWalletSearchRepository;

public BugResponse getBug(Long memberId) {
Bug bug = memberService.findMember(memberId).getBug();
Bug bug = getByMemberId(memberId);

return BugMapper.toBugResponse(bug);
}
Expand All @@ -63,28 +66,62 @@ public ProductsResponse getBugProducts() {

@Transactional
public PurchaseProductResponse purchaseBugProduct(Long memberId, Long productId, PurchaseProductRequest request) {
Product product = getById(productId);
Product product = getProductById(productId);
Payment payment = PaymentMapper.toPayment(memberId, product);

if (!isNull(request.couponWalletId())) {
CouponWallet couponWallet = couponService.getWalletByIdAndMemberId(request.couponWalletId(), memberId);
CouponWallet couponWallet = getCouponWallet(request.couponWalletId(), memberId);
payment.applyCoupon(couponWallet);
}
paymentRepository.save(payment);

return ProductMapper.toPurchaseProductResponse(payment);
}

@Transactional
public void use(Member member, BugType bugType, int count) {
Bug bug = member.getBug();

bug.use(bugType, count);
bugHistoryRepository.save(BugMapper.toUseBugHistory(member.getId(), bugType, count));
}

@Transactional
public void reward(Member member, BugType bugType, int count) {
Bug bug = member.getBug();

bug.increase(bugType, count);
bugHistoryRepository.save(BugMapper.toRewardBugHistory(member.getId(), bugType, count));
}

@Transactional
public void charge(Long memberId, Product bugProduct) {
Bug bug = memberService.findMember(memberId).getBug();
Bug bug = getByMemberId(memberId);

bug.charge(bugProduct.getQuantity());
bugHistoryRepository.save(BugMapper.toChargeBugHistory(memberId, bugProduct.getQuantity()));
}

private Product getById(Long productId) {
@Transactional
public void applyCoupon(Long memberId, BugType bugType, int count) {
Bug bug = getByMemberId(memberId);

bug.increase(bugType, count);
bugHistoryRepository.save(BugMapper.toCouponBugHistory(memberId, bugType, count));
}

private Bug getByMemberId(Long memberId) {
return memberService.findMember(memberId)
.getBug();
}

private Product getProductById(Long productId) {
return productRepository.findById(productId)
.orElseThrow(() -> new NotFoundException(PRODUCT_NOT_FOUND));
}

private CouponWallet getCouponWallet(Long couponWalletId, Long memberId) {
return couponWalletSearchRepository.findByIdAndMemberId(couponWalletId, memberId)
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_COUPON_WALLET));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Optional;
import java.util.Set;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.moabam.api.application.notification.NotificationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.bug.BugService;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.coupon.Coupon;
import com.moabam.api.domain.coupon.CouponWallet;
import com.moabam.api.domain.coupon.repository.CouponRepository;
import com.moabam.api.domain.coupon.repository.CouponSearchRepository;
import com.moabam.api.domain.coupon.repository.CouponWalletRepository;
import com.moabam.api.domain.coupon.repository.CouponWalletSearchRepository;
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.member.Role;
import com.moabam.api.dto.coupon.CouponResponse;
import com.moabam.api.dto.coupon.CouponStatusRequest;
Expand All @@ -35,7 +34,7 @@
public class CouponService {

private final ClockHolder clockHolder;
private final MemberService memberService;
private final BugService bugService;
private final CouponManageService couponManageService;
private final CouponRepository couponRepository;
private final CouponSearchRepository couponSearchRepository;
Expand All @@ -59,9 +58,7 @@ public void use(Long memberId, Long couponWalletId) {
Coupon coupon = couponWallet.getCoupon();
BugType bugType = coupon.getType().getBugType();

Member member = memberService.findMember(memberId);
member.getBug().increase(bugType, coupon.getPoint());

bugService.applyCoupon(memberId, bugType, coupon.getPoint());
couponWalletRepository.delete(couponWallet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static ItemResponse toItemResponse(Item item) {
.type(item.getType().name())
.category(item.getCategory().name())
.name(item.getName())
.image(item.getImage())
.image(item.getAwakeImage())
.level(item.getUnlockLevel())
.bugPrice(item.getBugPrice())
.goldenBugPrice(item.getGoldenBugPrice())
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/com/moabam/api/application/item/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.bug.BugMapper;
import com.moabam.api.application.bug.BugService;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.domain.bug.Bug;
import com.moabam.api.domain.bug.repository.BugHistoryRepository;
import com.moabam.api.domain.item.Inventory;
import com.moabam.api.domain.item.Item;
import com.moabam.api.domain.item.ItemType;
Expand All @@ -32,11 +30,11 @@
public class ItemService {

private final MemberService memberService;
private final BugService bugService;
private final ItemRepository itemRepository;
private final ItemSearchRepository itemSearchRepository;
private final InventoryRepository inventoryRepository;
private final InventorySearchRepository inventorySearchRepository;
private final BugHistoryRepository bugHistoryRepository;

public ItemsResponse getItems(Long memberId, ItemType type) {
Item defaultItem = getDefaultInventory(memberId, type).getItem();
Expand All @@ -54,12 +52,10 @@ public void purchaseItem(Long memberId, Long itemId, PurchaseItemRequest request
validateAlreadyPurchased(memberId, itemId);
item.validatePurchasable(request.bugType(), member.getLevel());

Bug bug = member.getBug();
int price = item.getPrice(request.bugType());

bug.use(request.bugType(), price);
bugService.use(member, request.bugType(), price);
inventoryRepository.save(ItemMapper.toInventory(memberId, item));
bugHistoryRepository.save(BugMapper.toUseBugHistory(memberId, request.bugType(), price));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ private void validateNickname(String nickname) {

private Member signUp(Long socialId) {
Member member = MemberMapper.toMember(socialId);
return memberRepository.save(member);
Member savedMember = memberRepository.save(member);
saveMyEgg(savedMember);

return savedMember;
}

private void saveMyEgg(Member member) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.bug.BugService;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.room.mapper.CertificationsMapper;
import com.moabam.api.domain.bug.BugType;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class CertificationService {
private final DailyRoomCertificationRepository dailyRoomCertificationRepository;
private final DailyMemberCertificationRepository dailyMemberCertificationRepository;
private final MemberService memberService;
private final BugService bugService;
private final ClockHolder clockHolder;

@Transactional
Expand Down Expand Up @@ -88,7 +90,7 @@ public void certifyRoom(CertifiedMemberInfo certifyInfo) {
return;
}

member.getBug().increase(bugType, room.getLevel());
bugService.reward(member, bugType, room.getLevel());
}

public boolean existsMemberCertification(Long memberId, Long roomId, LocalDate date) {
Expand Down Expand Up @@ -187,6 +189,6 @@ private void provideBugToCompletedMembers(BugType bugType, List<DailyMemberCerti
.toList();

memberService.getRoomMembers(memberIds)
.forEach(completedMember -> completedMember.getBug().increase(bugType, expAppliedRoomLevel));
.forEach(completedMember -> bugService.reward(completedMember, bugType, expAppliedRoomLevel));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ private List<TodayCertificateRankResponse> completedMembers(
.findAny()
.orElseThrow(() -> new NotFoundException(INVENTORY_NOT_FOUND));

String awakeImage = inventory.getItem().getImage();
String sleepImage = inventory.getItem().getImage();
String awakeImage = inventory.getItem().getAwakeImage();
String sleepImage = inventory.getItem().getSleepImage();

int contributionPoint = calculateContributionPoint(member.getId(), participants, date);
CertificationImagesResponse certificationImages = getCertificationImages(member.getId(), certifications);
Expand Down Expand Up @@ -343,8 +343,8 @@ private List<TodayCertificateRankResponse> uncompletedMembers(
.findAny()
.orElseThrow(() -> new NotFoundException(INVENTORY_NOT_FOUND));

String awakeImage = inventory.getItem().getImage();
String sleepImage = inventory.getItem().getImage();
String awakeImage = inventory.getItem().getAwakeImage();
String sleepImage = inventory.getItem().getSleepImage();

int contributionPoint = calculateContributionPoint(memberId, participants, date);
boolean isNotificationSent = knocks.contains(member.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public static UnJoinedRoomCertificateRankResponse toUnJoinedRoomCertificateRankR
.rank(rank)
.memberId(member.getId())
.nickname(member.getNickname())
.awakeImage(inventory.getItem().getImage())
.sleepImage(inventory.getItem().getImage())
.awakeImage(inventory.getItem().getAwakeImage())
.sleepImage(inventory.getItem().getSleepImage())
.build();
}
}
31 changes: 16 additions & 15 deletions src/main/java/com/moabam/api/domain/bug/Bug.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ private int validateBugCount(int bug) {
return bug;
}

public void use(BugType bugType, int price) {
public void use(BugType bugType, int count) {
int currentBug = getBug(bugType);
validateEnoughBug(currentBug, price);
decrease(bugType, price);

validateEnoughBug(currentBug, count);
decrease(bugType, count);
}

private int getBug(BugType bugType) {
Expand All @@ -59,29 +60,29 @@ private int getBug(BugType bugType) {
};
}

private void validateEnoughBug(int currentBug, int price) {
if (price > currentBug) {
private void validateEnoughBug(int currentBug, int count) {
if (currentBug < count) {
throw new BadRequestException(BUG_NOT_ENOUGH);
}
}

private void decrease(BugType bugType, int bug) {
private void decrease(BugType bugType, int count) {
switch (bugType) {
case MORNING -> this.morningBug -= bug;
case NIGHT -> this.nightBug -= bug;
case GOLDEN -> this.goldenBug -= bug;
case MORNING -> this.morningBug -= count;
case NIGHT -> this.nightBug -= count;
case GOLDEN -> this.goldenBug -= count;
}
}

public void increase(BugType bugType, int bug) {
public void increase(BugType bugType, int count) {
switch (bugType) {
case MORNING -> this.morningBug += bug;
case NIGHT -> this.nightBug += bug;
case GOLDEN -> this.goldenBug += bug;
case MORNING -> this.morningBug += count;
case NIGHT -> this.nightBug += count;
case GOLDEN -> this.goldenBug += count;
}
}

public void charge(int quantity) {
this.goldenBug += quantity;
public void charge(int count) {
this.goldenBug += count;
}
}
Loading

0 comments on commit b801572

Please sign in to comment.