diff --git a/src/main/java/com/noplanb/domain/character/application/CharacterService.java b/src/main/java/com/noplanb/domain/character/application/CharacterService.java index bdab105..80571ef 100644 --- a/src/main/java/com/noplanb/domain/character/application/CharacterService.java +++ b/src/main/java/com/noplanb/domain/character/application/CharacterService.java @@ -2,6 +2,8 @@ import com.noplanb.domain.character.domain.Character; // import com.noplanb.domain.character.dto.request.NewCharacterReq; +import com.noplanb.domain.character.dto.request.MyCharacterEquipItemDetailReq; +import com.noplanb.domain.character.dto.request.MyCharacterEquipItemReq; import com.noplanb.domain.character.dto.request.NewCharacterReq; import com.noplanb.domain.character.dto.request.UpdateNameReq; import com.noplanb.domain.character.dto.response.InitialCharacterInfoRes; @@ -12,6 +14,7 @@ import com.noplanb.domain.item.domain.Item; import com.noplanb.domain.item.repository.ItemRepository; import com.noplanb.domain.item_image.domain.ItemImage; +import com.noplanb.domain.item_image.domain.ItemType; import com.noplanb.domain.item_image.domain.repository.ItemImageRepository; import com.noplanb.domain.user.domain.User; import com.noplanb.domain.user.repository.UserRepository; @@ -196,4 +199,46 @@ public ResponseEntity createInitialCharacter(UserPrincipal userPrincipal, New return ResponseEntity.ok(apiResponse); } + + public ResponseEntity myCharacterEquipItem(UserPrincipal userPrincipal, MyCharacterEquipItemReq myCharacterEquipItemReq) { + Character character = characterRepository.findByUserId(userPrincipal.getId()).orElseThrow(() -> new IllegalArgumentException("캐릭터를 찾을 수 없습니다.")); + List item = character.getItems(); + // 캐릭터의 아이템 리스트 + List itemIdList = myCharacterEquipItemReq.getMyCharacterEquipItemDetailReqList(); + + // 캐릭터의 아이템 리스트 중 장착 여부를 변경할 아이템 찾기 (아이템 타입, 아이템 id) + // 해당 타입의 기존 장착 아이템은 장착 해제 + + for (MyCharacterEquipItemDetailReq myCharacterEquipItemDetailReq : itemIdList) { + ItemType itemType = myCharacterEquipItemDetailReq.getItemType(); + Long itemId = myCharacterEquipItemDetailReq.getItemId(); + +// // 장착 해제할 아이템 찾기 후 isEquipped = false로 변경 +// Item itemToUnequip = item.stream() +// .filter(i -> i.getItemImage().getItemType().equals(itemType) && i.isEquipped()) +// .findFirst() +// .orElseThrow(() -> new IllegalArgumentException("장착 해제할 아이템을 찾을 수 없습니다.")); +// itemToUnequip.updateEquipped(false); + + // 모든 아이템 장착 상태를 isEquipped = false로 초기화 + for (Item i : item) { + i.updateEquipped(false); + } + + // 장착할 아이템 isEquipped = true로 변경 + Item itemToEquip = item.stream() + .filter(i -> i.getItemImage().getId().equals(itemId)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("장착할 아이템을 찾을 수 없습니다.")); + itemToEquip.updateEquipped(true); + + } + + ApiResponse apiResponse = ApiResponse.builder() + .check(true) + .information(Message.builder().message("아이템 장착이 완료되었습니다.").build()) + .build(); + + return ResponseEntity.ok(apiResponse); + } } diff --git a/src/main/java/com/noplanb/domain/character/controller/CharacterController.java b/src/main/java/com/noplanb/domain/character/controller/CharacterController.java index b78aeb9..058ba5e 100644 --- a/src/main/java/com/noplanb/domain/character/controller/CharacterController.java +++ b/src/main/java/com/noplanb/domain/character/controller/CharacterController.java @@ -1,6 +1,7 @@ package com.noplanb.domain.character.controller; import com.noplanb.domain.character.application.CharacterService; +import com.noplanb.domain.character.dto.request.MyCharacterEquipItemReq; import com.noplanb.domain.character.dto.request.NewCharacterReq; import com.noplanb.domain.character.dto.request.UpdateNameReq; import com.noplanb.domain.character.dto.response.InitialCharacterInfoRes; @@ -83,9 +84,14 @@ public ResponseEntity createInitialCharacter(@Parameter @CurrentUser UserPrin return characterService.createInitialCharacter(userPrincipal, newCharacterReq); } - - - - + @Operation(summary = "캐릭터 아이템 장착 API", description = "캐릭터가 아이템을 장착하는 API입니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "아이템 장착 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ), + @ApiResponse(responseCode = "400", description = "아이템 장착 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ), + }) + @PostMapping("/equip") + public ResponseEntity equipItem(@Parameter @CurrentUser UserPrincipal userPrincipal, @RequestParam MyCharacterEquipItemReq myCharacterEquipItemReq){ + return characterService.myCharacterEquipItem(userPrincipal, myCharacterEquipItemReq); + } } diff --git a/src/main/java/com/noplanb/domain/character/dto/request/MyCharacterEquipItemDetailReq.java b/src/main/java/com/noplanb/domain/character/dto/request/MyCharacterEquipItemDetailReq.java new file mode 100644 index 0000000..091fc35 --- /dev/null +++ b/src/main/java/com/noplanb/domain/character/dto/request/MyCharacterEquipItemDetailReq.java @@ -0,0 +1,19 @@ +package com.noplanb.domain.character.dto.request; + +import com.noplanb.domain.item_image.domain.ItemType; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Getter +public class MyCharacterEquipItemDetailReq { + @Schema(type = "string", example = "CLOTHES", description="아이템 타입") + private ItemType itemType; + @Schema(type = "long", example = "10", description="아이템 아이디") + private Long itemId; +} diff --git a/src/main/java/com/noplanb/domain/character/dto/request/MyCharacterEquipItemReq.java b/src/main/java/com/noplanb/domain/character/dto/request/MyCharacterEquipItemReq.java new file mode 100644 index 0000000..73c927a --- /dev/null +++ b/src/main/java/com/noplanb/domain/character/dto/request/MyCharacterEquipItemReq.java @@ -0,0 +1,18 @@ +package com.noplanb.domain.character.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Getter +public class MyCharacterEquipItemReq { + @Schema(type = "List", description="캐릭터 장착 아이템 리스트") + private List myCharacterEquipItemDetailReqList; +}