Skip to content

Commit

Permalink
Merge pull request #38 from DEPthes/feat/initial-character-new
Browse files Browse the repository at this point in the history
아이템 장착 기능 구현
  • Loading branch information
lsn5963 authored Aug 16, 2024
2 parents 74b7af0 + 36468d0 commit 2bfb913
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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> item = character.getItems();
// 캐릭터의 아이템 리스트
List<MyCharacterEquipItemDetailReq> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<MyCharacterEquipItemDetailReq> myCharacterEquipItemDetailReqList;
}

0 comments on commit 2bfb913

Please sign in to comment.