Skip to content

Commit

Permalink
Merge branch 'develop' into feature/#25-authtime-notifiaction
Browse files Browse the repository at this point in the history
  • Loading branch information
hongdosan committed Nov 7, 2023
2 parents 055c143 + 66eda55 commit 858ca88
Show file tree
Hide file tree
Showing 26 changed files with 376 additions and 150 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/moabam/api/application/ItemService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.moabam.api.application;

import static com.moabam.global.error.model.ErrorMessage.*;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.domain.entity.Inventory;
import com.moabam.api.domain.entity.Item;
import com.moabam.api.domain.entity.enums.ItemType;
import com.moabam.api.domain.repository.InventorySearchRepository;
import com.moabam.api.domain.repository.ItemSearchRepository;
import com.moabam.api.dto.ItemMapper;
import com.moabam.api.dto.ItemsResponse;
import com.moabam.global.error.exception.NotFoundException;

import lombok.RequiredArgsConstructor;

Expand All @@ -28,4 +32,18 @@ public ItemsResponse getItems(Long memberId, ItemType type) {

return ItemMapper.toItemsResponse(purchasedItems, notPurchasedItems);
}

@Transactional
public void selectItem(Long memberId, Long itemId) {
Inventory inventory = getInventory(memberId, itemId);

inventorySearchRepository.findDefault(memberId, inventory.getItemType())
.ifPresent(Inventory::deselect);
inventory.select();
}

private Inventory getInventory(Long memberId, Long itemId) {
return inventorySearchRepository.findOne(memberId, itemId)
.orElseThrow(() -> new NotFoundException(INVENTORY_NOT_FOUND));
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/moabam/api/domain/entity/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.hibernate.annotations.ColumnDefault;

import com.moabam.api.domain.entity.enums.ItemType;
import com.moabam.global.common.entity.BaseTimeEntity;

import jakarta.persistence.Column;
Expand Down Expand Up @@ -49,4 +50,16 @@ private Inventory(Long memberId, Item item, boolean isDefault) {
this.item = requireNonNull(item);
this.isDefault = isDefault;
}

public ItemType getItemType() {
return this.item.getType();
}

public void select() {
this.isDefault = true;
}

public void deselect() {
this.isDefault = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import static com.moabam.api.domain.entity.QItem.*;

import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Repository;

import com.moabam.api.domain.entity.Inventory;
import com.moabam.api.domain.entity.Item;
import com.moabam.api.domain.entity.enums.ItemType;
import com.moabam.global.common.util.DynamicQuery;
Expand All @@ -20,6 +22,27 @@ public class InventorySearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public Optional<Inventory> findOne(Long memberId, Long itemId) {
return Optional.ofNullable(
jpaQueryFactory.selectFrom(inventory)
.where(
DynamicQuery.generateEq(memberId, inventory.memberId::eq),
DynamicQuery.generateEq(itemId, inventory.item.id::eq))
.fetchOne()
);
}

public Optional<Inventory> findDefault(Long memberId, ItemType type) {
return Optional.ofNullable(
jpaQueryFactory.selectFrom(inventory)
.where(
DynamicQuery.generateEq(memberId, inventory.memberId::eq),
DynamicQuery.generateEq(type, inventory.item.type::eq),
inventory.isDefault.isTrue())
.fetchOne()
);
}

public List<Item> findItems(Long memberId, ItemType type) {
return jpaQueryFactory.selectFrom(inventory)
.join(inventory.item, item)
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/moabam/api/presentation/ItemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand All @@ -25,4 +27,10 @@ public class ItemController {
public ItemsResponse getItems(@RequestParam ItemType type) {
return itemService.getItems(1L, type);
}

@PostMapping("/{itemId}/select")
@ResponseStatus(HttpStatus.OK)
public void selectItem(@PathVariable Long itemId) {
itemService.selectItem(1L, itemId);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/moabam/global/error/model/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public enum ErrorMessage {
MEMBER_NOT_FOUND("존재하지 않는 회원입니다."),
MEMBER_ROOM_EXCEED("참여할 수 있는 방의 개수가 모두 찼습니다."),

INVENTORY_NOT_FOUND("구매하지 않은 아이템은 적용할 수 없습니다."),

INVALID_BUG_COUNT("벌레 개수는 0 이상이어야 합니다."),
INVALID_PRICE("가격은 0 이상이어야 합니다."),
INVALID_QUANTITY("수량은 1 이상이어야 합니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import com.moabam.api.dto.AuthorizationTokenRequest;
import com.moabam.api.dto.AuthorizationTokenResponse;
import com.moabam.api.dto.OAuthMapper;
import com.moabam.fixture.AuthorizationResponseFixture;
import com.moabam.global.config.OAuthConfig;
import com.moabam.global.error.exception.BadRequestException;
import com.moabam.global.error.model.ErrorMessage;
import com.moabam.support.fixture.AuthorizationResponseFixture;

@ExtendWith(MockitoExtension.class)
class AuthenticationServiceTest {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/moabam/api/application/BugServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.moabam.api.application;

import static com.moabam.support.fixture.MemberFixture.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

Expand All @@ -13,7 +14,6 @@
import com.moabam.api.domain.entity.Bug;
import com.moabam.api.domain.entity.Member;
import com.moabam.api.dto.BugResponse;
import com.moabam.fixture.MemberFixture;

@ExtendWith(MockitoExtension.class)
class BugServiceTest {
Expand All @@ -29,7 +29,7 @@ class BugServiceTest {
void get_bug_success() {
// given
Long memberId = 1L;
Member member = MemberFixture.member();
Member member = member();
given(memberService.getById(memberId)).willReturn(member);

// when
Expand Down
49 changes: 48 additions & 1 deletion src/test/java/com/moabam/api/application/ItemServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package com.moabam.api.application;

import static com.moabam.fixture.ItemFixture.*;
import static com.moabam.support.fixture.ItemFixture.*;
import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.moabam.api.domain.entity.Inventory;
import com.moabam.api.domain.entity.Item;
import com.moabam.api.domain.entity.enums.ItemType;
import com.moabam.api.domain.repository.InventorySearchRepository;
import com.moabam.api.domain.repository.ItemSearchRepository;
import com.moabam.api.dto.ItemResponse;
import com.moabam.api.dto.ItemsResponse;
import com.moabam.global.common.util.StreamUtils;
import com.moabam.global.error.exception.NotFoundException;
import com.moabam.support.fixture.InventoryFixture;

@ExtendWith(MockitoExtension.class)
class ItemServiceTest {
Expand Down Expand Up @@ -54,4 +60,45 @@ void get_products_success() {
assertThat(purchasedItemNames).containsExactly(MORNING_SANTA_SKIN_NAME, MORNING_KILLER_SKIN_NAME);
assertThat(response.notPurchasedItems()).isEmpty();
}

@DisplayName("아이템을 적용한다.")
@Nested
class SelectItem {

@DisplayName("성공한다.")
@Test
void success() {
// given
Long memberId = 1L;
Long itemId = 1L;
Inventory inventory = InventoryFixture.inventory(memberId, nightMageSkin());
Inventory defaultInventory = InventoryFixture.inventory(memberId, nightMageSkin());
ItemType itemType = inventory.getItemType();
given(inventorySearchRepository.findOne(memberId, itemId)).willReturn(Optional.of(inventory));
given(inventorySearchRepository.findDefault(memberId, itemType)).willReturn(Optional.of(defaultInventory));

// when
itemService.selectItem(memberId, itemId);

// then
verify(inventorySearchRepository).findOne(memberId, itemId);
verify(inventorySearchRepository).findDefault(memberId, itemType);
assertFalse(defaultInventory.isDefault());
assertTrue(inventory.isDefault());
}

@DisplayName("인벤토리 아이템이 아니면 예외가 발생한다.")
@Test
void exception() {
// given
Long memberId = 1L;
Long itemId = 1L;
given(inventorySearchRepository.findOne(memberId, itemId)).willReturn(Optional.empty());

// when, then
assertThatThrownBy(() -> itemService.selectItem(memberId, itemId))
.isInstanceOf(NotFoundException.class)
.hasMessage("구매하지 않은 아이템은 적용할 수 없습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.moabam.api.application;

import static com.moabam.fixture.ProductFixture.*;
import static com.moabam.support.fixture.ProductFixture.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/moabam/api/domain/entity/MemberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.junit.jupiter.api.Test;

import com.moabam.api.domain.entity.enums.Role;
import com.moabam.fixture.MemberFixture;
import com.moabam.global.common.util.BaseImageUrl;
import com.moabam.support.fixture.MemberFixture;

class MemberTest {

Expand Down
Loading

0 comments on commit 858ca88

Please sign in to comment.