Skip to content

Commit

Permalink
#334 refactor: FridgeUser toEntity 메소드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
psyeon1120 committed Jul 30, 2024
1 parent 14f6800 commit 10ed2c0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
@NoArgsConstructor
@Schema(name = "냉장고 추가 요청 정보")
public class AddFridgeReq {
@NotBlank
@Schema(description = "냉장고 이름", example = "우리집 냉장고")
private String fridgeName;
@Schema(description = "냉장고 설명", example = "우리집 냉장고입니다~")
private String fridgeComment;
@Schema(description = "냉장고 멤버 ID 리스트", example = "[ 1, 2 ]")
private List<Long> members;
@NotBlank
@Schema(description = "냉장고 이름", example = "우리집 냉장고")
private String fridgeName;
@Schema(description = "냉장고 설명", example = "우리집 냉장고입니다~")
private String fridgeComment;
@Schema(description = "냉장고 멤버 ID 리스트", example = "[ 1, 2 ]")
private List<Long> members;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public FridgeUser(User user, Fridge fridge, FridgeRole role) {
this.role = role;
}

public static FridgeUser toEntity(User user, Fridge fridge, FridgeRole fridgeRole) {
return FridgeUser.builder()
.fridge(fridge)
.user(user)
.role(fridgeRole)
.build();
}

public void changeRoleToOwner(){
this.role = FridgeRole.OWNER;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,12 @@ public Long addFridge(AddFridgeReq addFridgeReq, Long ownerId) {
.map(memberId -> {
User user = userRepository.findByIdAndIsEnable(memberId, true)
.orElseThrow(() -> new BaseException(NOT_FOUND_USER));
return FridgeUser.builder()
.fridge(fridge)
.user(user)
.role(FridgeRole.MEMBER)
.build();
return FridgeUser.toEntity(user, fridge, FridgeRole.MEMBER);
})
.collect(Collectors.toList());

User owner = userRepository.findById(ownerId).orElseThrow(() -> new BaseException(NOT_FOUND_USER));
members.add(FridgeUser.builder()
.fridge(fridge)
.user(owner)
.role(FridgeRole.OWNER)
.build());
members.add(FridgeUser.toEntity(owner,fridge, FridgeRole.OWNER));

fridgeUserRepository.saveAll(members);
cartRepository.save(Cart.toEntity(fridge));
Expand Down

0 comments on commit 10ed2c0

Please sign in to comment.