-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from TEAM-ALOM/5-feat-상점-구현
5 feat 상점 구현
- Loading branch information
Showing
13 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/alom/dorundorunbe/domain/item/controller/ItemController.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,55 @@ | ||
package com.alom.dorundorunbe.domain.item.controller; | ||
|
||
import com.alom.dorundorunbe.domain.item.domain.ItemCategory; | ||
import com.alom.dorundorunbe.domain.item.dto.EquippedItemResponseDto; | ||
import com.alom.dorundorunbe.domain.item.dto.ItemResponseDto; | ||
import com.alom.dorundorunbe.domain.item.service.ItemService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/items") | ||
public class ItemController { | ||
|
||
private final ItemService itemService; | ||
|
||
@GetMapping("/{itemCategory}") | ||
@Operation(summary = "아이템 목록 조회", description = "카테고리별로 아이템 목록을 반환합니다(소유한 아이템 우선 정렬)") | ||
public ResponseEntity<List<ItemResponseDto>> fetchItemByCategory(@PathVariable("itemCategory") ItemCategory itemCategory, | ||
@RequestParam("userId") Long userId) { | ||
return ResponseEntity.ok(itemService.findItemByCategory(itemCategory, userId)); | ||
} | ||
|
||
@PostMapping("/{itemId}") | ||
@Operation(summary = "아이템 구매", description = "캐시로 아이템을 구매합니다") | ||
public ResponseEntity<Void> purchaseItem(@PathVariable("itemId") Long itemId, | ||
@RequestParam("userId") Long userId) { | ||
itemService.purchaseItem(itemId, userId); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping("/{itemId}/equipped") | ||
@Operation(summary = "아이템 착용", description = "아이템 착용 후 착용 중인 아이템 목록을 반환합니다") | ||
public ResponseEntity<List<EquippedItemResponseDto>> equippedItem(@PathVariable("itemId") Long itemId, | ||
@RequestParam("userId") Long userId) { | ||
return ResponseEntity.ok(itemService.equippedItem(itemId, userId)); | ||
} | ||
|
||
@PostMapping("/{itemId}/unequipped") | ||
@Operation(summary = "아이템 착용 해제", description = "아이템 착용 해제 후 착용 중인 아이템 목록을 반환합니다") | ||
public ResponseEntity<List<EquippedItemResponseDto>> unequippedItem(@PathVariable("itemId") Long itemId, | ||
@RequestParam("userId") Long userId) { | ||
return ResponseEntity.ok(itemService.unequippedItem(itemId, userId)); | ||
} | ||
|
||
@GetMapping("/equipped") | ||
@Operation(summary = "착용한 아이템 목록 조회", description = "착용한 아이템 목록을 반환합니다") | ||
public ResponseEntity<List<EquippedItemResponseDto>> fetchEquippedItem(@RequestParam("userId") Long userId) { | ||
return ResponseEntity.ok(itemService.findEquippedItem(userId)); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/alom/dorundorunbe/domain/item/domain/ItemCategory.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,17 @@ | ||
package com.alom.dorundorunbe.domain.item.domain; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum ItemCategory { | ||
CLOTHES("옷"), | ||
HAIR("머리"), | ||
ACCESSORY("악세사리"), | ||
BACKGROUND("배경"); | ||
|
||
private final String description; | ||
|
||
ItemCategory(String description) { | ||
this.description = description; | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/alom/dorundorunbe/domain/item/dto/EquippedItemResponseDto.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,13 @@ | ||
package com.alom.dorundorunbe.domain.item.dto; | ||
|
||
import com.alom.dorundorunbe.domain.item.domain.ItemCategory; | ||
|
||
public record EquippedItemResponseDto( | ||
Long itemId, | ||
String name, | ||
ItemCategory itemCategory | ||
) { | ||
public static EquippedItemResponseDto of(Long itemId, String name, ItemCategory itemCategory) { | ||
return new EquippedItemResponseDto(itemId, name, itemCategory); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/alom/dorundorunbe/domain/item/dto/ItemResponseDto.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,12 @@ | ||
package com.alom.dorundorunbe.domain.item.dto; | ||
|
||
public record ItemResponseDto( | ||
Long itemId, | ||
String name, | ||
Long cost, | ||
Boolean owned | ||
) { | ||
public static ItemResponseDto of(Long itemId, String name, Long cost, Boolean owned) { | ||
return new ItemResponseDto(itemId, name, cost, owned); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/alom/dorundorunbe/domain/item/repository/ItemRepository.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,12 @@ | ||
package com.alom.dorundorunbe.domain.item.repository; | ||
|
||
import com.alom.dorundorunbe.domain.item.domain.Item; | ||
import com.alom.dorundorunbe.domain.item.domain.ItemCategory; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface ItemRepository extends JpaRepository<Item, Long> { | ||
|
||
List<Item> findAllByItemCategory(ItemCategory itemCategory); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/alom/dorundorunbe/domain/item/repository/UserItemRepository.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.alom.dorundorunbe.domain.item.repository; | ||
|
||
import com.alom.dorundorunbe.domain.item.domain.Item; | ||
import com.alom.dorundorunbe.domain.item.domain.UserItem; | ||
import com.alom.dorundorunbe.domain.user.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface UserItemRepository extends JpaRepository<UserItem, Long> { | ||
Boolean existsByUserAndItem(User user, Item item); | ||
UserItem findByUserAndItem(User user, Item item); | ||
List<UserItem> findAllByUserAndEquipped(User user, Boolean equipped); | ||
} |
118 changes: 118 additions & 0 deletions
118
src/main/java/com/alom/dorundorunbe/domain/item/service/ItemService.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,118 @@ | ||
package com.alom.dorundorunbe.domain.item.service; | ||
|
||
import com.alom.dorundorunbe.domain.item.domain.Item; | ||
import com.alom.dorundorunbe.domain.item.domain.ItemCategory; | ||
import com.alom.dorundorunbe.domain.item.domain.UserItem; | ||
import com.alom.dorundorunbe.domain.item.dto.EquippedItemResponseDto; | ||
import com.alom.dorundorunbe.domain.item.dto.ItemResponseDto; | ||
import com.alom.dorundorunbe.domain.item.repository.ItemRepository; | ||
import com.alom.dorundorunbe.domain.item.repository.UserItemRepository; | ||
import com.alom.dorundorunbe.domain.user.domain.User; | ||
import com.alom.dorundorunbe.domain.user.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ItemService { | ||
|
||
private final ItemRepository itemRepository; | ||
private final UserItemRepository userItemRepository; | ||
private final UserService userService; | ||
|
||
public List<ItemResponseDto> findItemByCategory(ItemCategory itemCategory, Long userId) { | ||
|
||
User user = userService.findById(userId); | ||
List<Item> itemList = itemRepository.findAllByItemCategory(itemCategory); | ||
|
||
return itemList.stream() | ||
.map(item -> ItemResponseDto.of( | ||
item.getId(), | ||
item.getName(), | ||
item.getCost(), | ||
userItemRepository.existsByUserAndItem(user, item) | ||
)) | ||
.sorted(Comparator.comparing(ItemResponseDto::owned).reversed()) | ||
.toList(); | ||
} | ||
|
||
public void purchaseItem(Long itemId, Long userId) { | ||
User user = userService.findById(userId); | ||
Item item = itemRepository.findById(itemId).orElseThrow(); | ||
|
||
if (userItemRepository.existsByUserAndItem(user, item)) { | ||
throw new IllegalArgumentException("이미 소유한 아이템입니다"); | ||
} | ||
if (user.getCash()<item.getCost()) { | ||
throw new IllegalArgumentException("잔액이 부족합니다"); | ||
} | ||
|
||
user.updateCash(user.getCash()-item.getCost()); | ||
UserItem userItem = UserItem.builder() | ||
.user(user) | ||
.item(item) | ||
.build(); | ||
userItemRepository.save(userItem); | ||
} | ||
|
||
public List<EquippedItemResponseDto> equippedItem(Long itemId, Long userId) { | ||
User user = userService.findById(userId); | ||
Item item = itemRepository.findById(itemId).orElseThrow(); | ||
UserItem userItem = userItemRepository.findByUserAndItem(user, item); | ||
|
||
if (userItem == null) { | ||
throw new IllegalArgumentException("구매가 필요합니다"); | ||
} | ||
if (userItem.getEquipped()) { | ||
throw new IllegalArgumentException("이미 착용된 아이템입니다"); | ||
} | ||
|
||
userItem.updateEquipped(true); | ||
|
||
return userItemRepository.findAllByUserAndEquipped(user, true).stream() | ||
.map(equippedUserItem -> EquippedItemResponseDto.of( | ||
equippedUserItem.getItem().getId(), | ||
equippedUserItem.getItem().getName(), | ||
equippedUserItem.getItem().getItemCategory() | ||
)) | ||
.toList(); | ||
} | ||
|
||
public List<EquippedItemResponseDto> unequippedItem(Long itemId, Long userId) { | ||
User user = userService.findById(userId); | ||
Item item = itemRepository.findById(itemId).orElseThrow(); | ||
UserItem userItem = userItemRepository.findByUserAndItem(user, item); | ||
|
||
if (userItem == null) { | ||
throw new IllegalArgumentException("구매가 필요합니다"); | ||
} | ||
if (!userItem.getEquipped()) { | ||
throw new IllegalArgumentException("이미 미착용된 아이템입니다"); | ||
} | ||
|
||
userItem.updateEquipped(false); | ||
|
||
return userItemRepository.findAllByUserAndEquipped(user, true).stream() | ||
.map(equippedUserItem -> EquippedItemResponseDto.of( | ||
equippedUserItem.getItem().getId(), | ||
equippedUserItem.getItem().getName(), | ||
equippedUserItem.getItem().getItemCategory() | ||
)) | ||
.toList(); | ||
} | ||
|
||
public List<EquippedItemResponseDto> findEquippedItem(Long userId) { | ||
User user = userService.findById(userId); | ||
|
||
return userItemRepository.findAllByUserAndEquipped(user, true).stream() | ||
.map(equippedUserItem -> EquippedItemResponseDto.of( | ||
equippedUserItem.getItem().getId(), | ||
equippedUserItem.getItem().getName(), | ||
equippedUserItem.getItem().getItemCategory() | ||
)) | ||
.toList(); | ||
} | ||
} |
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
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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/alom/dorundorunbe/domain/user/service/UserService.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,17 @@ | ||
package com.alom.dorundorunbe.domain.user.service; | ||
|
||
import com.alom.dorundorunbe.domain.user.domain.User; | ||
import com.alom.dorundorunbe.domain.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public User findById(Long userId) { | ||
return userRepository.findById(userId).orElseThrow(); | ||
} | ||
} |