-
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.
Browse files
Browse the repository at this point in the history
회원이 아이템을 착용한다.
- Loading branch information
Showing
10 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
api/src/main/java/com/dnd/sbooky/api/docs/spec/SwitchEquippedItemApiSpec.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,15 @@ | ||
package com.dnd.sbooky.api.docs.spec; | ||
|
||
import com.dnd.sbooky.api.item.request.SwitchEquippedItemRequest; | ||
import com.dnd.sbooky.api.support.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
@Tag(name = "[Item API]", description = "아이템에 관련된 API") | ||
@SecurityRequirement(name = "access-token") | ||
public interface SwitchEquippedItemApiSpec { | ||
@Operation(summary = "아이템 착용", description = "새로운 아이템을 착용한다.") | ||
ApiResponse<?> switchEqiuppedItem(UserDetails user, SwitchEquippedItemRequest request); | ||
} |
31 changes: 31 additions & 0 deletions
31
api/src/main/java/com/dnd/sbooky/api/item/SwitchEquippedItemController.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,31 @@ | ||
package com.dnd.sbooky.api.item; | ||
|
||
import com.dnd.sbooky.api.docs.spec.SwitchEquippedItemApiSpec; | ||
import com.dnd.sbooky.api.item.request.SwitchEquippedItemRequest; | ||
import com.dnd.sbooky.api.support.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api") | ||
@RequiredArgsConstructor | ||
public class SwitchEquippedItemController implements SwitchEquippedItemApiSpec { | ||
|
||
private final SwitchEquippedItemUsecase switchEquippedItemUsecase; | ||
|
||
@PatchMapping("/items") | ||
public ApiResponse<?> switchEqiuppedItem( | ||
@Parameter(hidden = true) @AuthenticationPrincipal UserDetails userDetails, | ||
@Valid @RequestBody SwitchEquippedItemRequest request) { | ||
Long memberId = Long.parseLong(userDetails.getUsername()); | ||
switchEquippedItemUsecase.switchEquippedItem(memberId, request); | ||
return ApiResponse.success(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
api/src/main/java/com/dnd/sbooky/api/item/SwitchEquippedItemUsecase.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,40 @@ | ||
package com.dnd.sbooky.api.item; | ||
|
||
import static com.dnd.sbooky.api.support.error.ErrorType.*; | ||
|
||
import com.dnd.sbooky.api.item.exception.MemberHasNotItemException; | ||
import com.dnd.sbooky.api.item.request.SwitchEquippedItemRequest; | ||
import com.dnd.sbooky.core.item.MemberItemEntity; | ||
import com.dnd.sbooky.core.item.MemberItemRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class SwitchEquippedItemUsecase { | ||
|
||
private final MemberItemRepository memberItemRepository; | ||
|
||
public void switchEquippedItem(Long memberId, SwitchEquippedItemRequest request) { | ||
unEquipItem(memberId, request.equippedItemId()); | ||
equipItem(memberId, request.toEquipItemId()); | ||
} | ||
|
||
private void unEquipItem(Long memberId, Long equippedItemId) { | ||
MemberItemEntity equippedMemberItem = | ||
memberItemRepository | ||
.findMemberItemByMemberIdAndItemId(memberId, equippedItemId) | ||
.orElseThrow(() -> new MemberHasNotItemException(MEMBER_HAS_NOT_ITEM)); | ||
equippedMemberItem.unEquip(); | ||
} | ||
|
||
private void equipItem(Long memberId, Long toEquipItemId) { | ||
MemberItemEntity unEquippedMemberItem = | ||
memberItemRepository | ||
.findMemberItemByMemberIdAndItemId(memberId, toEquipItemId) | ||
.orElseThrow(() -> new MemberHasNotItemException(MEMBER_HAS_NOT_ITEM)); | ||
unEquippedMemberItem.equip(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
api/src/main/java/com/dnd/sbooky/api/item/exception/MemberHasNotItemException.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,10 @@ | ||
package com.dnd.sbooky.api.item.exception; | ||
|
||
import com.dnd.sbooky.api.support.error.ApiException; | ||
import com.dnd.sbooky.api.support.error.ErrorType; | ||
|
||
public class MemberHasNotItemException extends ApiException { | ||
public MemberHasNotItemException(ErrorType errorType) { | ||
super(errorType); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
api/src/main/java/com/dnd/sbooky/api/item/request/SwitchEquippedItemRequest.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.dnd.sbooky.api.item.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
@Schema(description = "아이템 착용 요청 DTO") | ||
public record SwitchEquippedItemRequest( | ||
@Schema(description = "기존 착용하고 있는 아이템") @NotNull Long equippedItemId, | ||
@Schema(description = "앞으로 착용할 아이템") @NotNull Long toEquipItemId) {} |
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 |
---|---|---|
|
@@ -27,6 +27,7 @@ public enum ErrorCode { | |
|
||
// Item Error | ||
ITEM_404, | ||
ITEM_404_2, | ||
|
||
// Evaluation Error | ||
EVALUATION_404_1, | ||
|
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
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