Skip to content

Commit

Permalink
#334 refactor: 냉장고 삭제 API 필요없는 과정 삭제 및 반환값 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
psyeon1120 committed Aug 9, 2024
1 parent 223e014 commit 36574a9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ResponseCustom<Long> addFridge(@Valid @RequestBody AddFridgeReq addFridge
return ResponseCustom.success(fridgeService.addFridge(addFridgeReq, userId));
}

@Operation(summary = "냉장고 정보 수정", description = "냉장고 정보를 수정한다.")
@Operation(summary = "냉장고 정보 수정", description = "주인이 냉장고 정보를 수정한다.")
@SwaggerApiSuccess(implementation = ResponseCustom.class)
@ApiResponses(value = {
@ApiResponse(responseCode = "400", description = "(G0000)잘못된 파라미터입니다.",
Expand All @@ -70,24 +70,22 @@ public ResponseCustom<Void> modifyFridge(@Parameter(description = "냉장고 ID"
return ResponseCustom.success();
}

@Operation(summary = "냉장고 삭제", description = "냉장고를 삭제한다.")
@Operation(summary = "냉장고 삭제", description = "주인이 냉장고를 삭제한다.")
@SwaggerApiSuccess(implementation = ResponseCustom.class)
@ApiResponses(value = {
@ApiResponse(responseCode = "400", description = "(G0000)잘못된 파라미터입니다.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
@ApiResponse(responseCode = "403", description = "(G0001)권한이 없습니다.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
@ApiResponse(responseCode = "404", description = "(U0000)존재하지 않는 사용자입니다.\t\n" +
"(R0000)존재하지 않는 냉장고입니다.\t\n",
@ApiResponse(responseCode = "404", description = "(R0000)존재하지 않는 냉장고입니다.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
@ApiResponse(responseCode = "409", description = "(R0001)해당 냉장고에 사용자가 존재합니다.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
})
@Auth
@PatchMapping("/{fridgeId}/remove")
public ResponseCustom<Long> removeFridge(@Parameter(description = "냉장고 ID") @PathVariable Long fridgeId,
@DeleteMapping("/{fridgeId}")
public ResponseCustom<Void> removeFridge(@Parameter(description = "냉장고 ID") @PathVariable Long fridgeId,
@Parameter(hidden = true) @IsLogin Long userId) {
return ResponseCustom.success(fridgeService.removeFridge(fridgeId, userId));
fridgeService.removeFridge(fridgeId, userId);
return ResponseCustom.success();
}

@Operation(summary = "냉장고 사용자 삭제", description = "냉장고 사용자를 삭제한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public void edit(String fridgeName, String fridgeComment) {
this.fridgeComment = fridgeComment;
}

public void remove() {
this.setIsEnable(false);
}

public static Fridge toEntity(AddFridgeReq addFridgeReq) {
return Fridge.builder()
.fridgeName(addFridgeReq.getFridgeName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface FridgeUserRepository extends JpaRepository<FridgeUser, Long> {

List<FridgeUser> findByFridgeAndIsEnable(Fridge fridge, Boolean isEnable);

boolean existsByFridgeAndRoleAndIsEnable(Fridge fridge, FridgeRole role, boolean isEnable);

List<FridgeUser> findByFridgeAndIsEnableOrderByRoleDesc(Fridge fridge, Boolean isEnable);

List<FridgeUser> findByUserAndIsEnable(User user, Boolean status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface FridgeService {
FridgeMainRes getFoods(Long fridgeId, Long userId, String category);
Long addFridge(AddFridgeReq registerFridgeReq, Long ownerId);
void modifyFridge(Long fridgeId, EditFridgeReq updateFridgeReq, Long userId);
Long removeFridge(Long fridgeId, Long userId);
void removeFridge(Long fridgeId, Long userId);
Long removeFridgeUser(Long fridgeId, Long userId) throws IOException;
List<FridgeFoodsRes> searchFridgeFood(Long fridgeId, Long ownerId, String foodName);
FridgeFoodRes getFridgeFood(Long fridgeId, Long fridgeFoodId, Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,38 +138,30 @@ private void updateFridgeUsers(EditFridgeReq editFridgeReq, Fridge fridge) {
this.fridgeUserRepository.deleteByFridgeAndUserIn(fridge, membersToDelete);
}


for (User user : membersToAdd)
alarmService.sendJoinFridgeAlarm(user, fridge.getFridgeName());

for (User user : membersToDelete)
alarmService.sendWithdrawalAlarm(user, fridge.getFridgeName());

}

private void exchangeFridgeOwner(FridgeUser owner, FridgeUser newOwner) {
owner.changeRoleToMember();
newOwner.changeRoleToOwner();
}

// 냉장고 자체 삭제
// 주인이 냉장고 삭제
@Transactional
public Long removeFridge(Long fridgeId, Long userId) {
User user = userRepository.findByIdAndIsEnable(userId, true).orElseThrow(() -> new BaseException(NOT_FOUND_USER));
Fridge fridge = fridgeRepository.findByIdAndIsEnable(fridgeId, true).orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE));
FridgeUser owner = (FridgeUser) fridgeUserRepository.findByUserAndFridgeAndIsEnable(user, fridge, true).orElseThrow(() -> new BaseException(NO_PERMISSION));
List<FridgeUser> fridgeUsers = fridgeUserRepository.findByFridgeAndIsEnable(fridge, true);
List<FridgeFood> fridgeFoods = fridgeFoodRepository.findByFridgeAndIsEnableOrderByShelfLife(fridge, true);

if (owner.getRole() != FridgeRole.OWNER) throw new BaseException(NO_PERMISSION);
if (fridgeUsers.size() > 1) throw new BaseException(STILL_MEMBER_EXIST);
public void removeFridge(Long fridgeId, Long userId) {
Fridge fridge = fridgeRepository.findByIdAndIsEnable(fridgeId, true)
.orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE));
fridgeUserRepository.findByFridgeAndUserIdAndRoleAndIsEnable(fridge, userId, FridgeRole.OWNER, true)
.orElseThrow(() -> new BaseException(NO_PERMISSION));

fridgeUsers.forEach(FridgeUser::remove);
// fridgeFoods.forEach(FridgeFood::remove);
fridge.remove();
fridgeFoodRepository.removeFridgeFoodByFridge(false, fridge);
if (fridgeUserRepository.existsByFridgeAndRoleAndIsEnable(fridge, FridgeRole.MEMBER, true))
throw new BaseException(STILL_MEMBER_EXIST);

return fridge.getId();
fridgeRepository.delete(fridge);
}

// 냉장고 개별
Expand Down

0 comments on commit 36574a9

Please sign in to comment.