diff --git a/src/main/java/com/example/icebutler_server/fridge/controller/FridgeController.java b/src/main/java/com/example/icebutler_server/fridge/controller/FridgeController.java index 38838ff..6cd4064 100644 --- a/src/main/java/com/example/icebutler_server/fridge/controller/FridgeController.java +++ b/src/main/java/com/example/icebutler_server/fridge/controller/FridgeController.java @@ -49,7 +49,7 @@ public ResponseCustom 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)잘못된 파라미터입니다.", @@ -70,24 +70,22 @@ public ResponseCustom 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 removeFridge(@Parameter(description = "냉장고 ID") @PathVariable Long fridgeId, + @DeleteMapping("/{fridgeId}") + public ResponseCustom 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 = "냉장고 사용자를 삭제한다.") diff --git a/src/main/java/com/example/icebutler_server/fridge/entity/Fridge.java b/src/main/java/com/example/icebutler_server/fridge/entity/Fridge.java index bf1ad3c..ba54dab 100644 --- a/src/main/java/com/example/icebutler_server/fridge/entity/Fridge.java +++ b/src/main/java/com/example/icebutler_server/fridge/entity/Fridge.java @@ -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()) diff --git a/src/main/java/com/example/icebutler_server/fridge/repository/FridgeUserRepository.java b/src/main/java/com/example/icebutler_server/fridge/repository/FridgeUserRepository.java index ec093bb..ce8958c 100644 --- a/src/main/java/com/example/icebutler_server/fridge/repository/FridgeUserRepository.java +++ b/src/main/java/com/example/icebutler_server/fridge/repository/FridgeUserRepository.java @@ -19,6 +19,8 @@ public interface FridgeUserRepository extends JpaRepository { List findByFridgeAndIsEnable(Fridge fridge, Boolean isEnable); + boolean existsByFridgeAndRoleAndIsEnable(Fridge fridge, FridgeRole role, boolean isEnable); + List findByFridgeAndIsEnableOrderByRoleDesc(Fridge fridge, Boolean isEnable); List findByUserAndIsEnable(User user, Boolean status); diff --git a/src/main/java/com/example/icebutler_server/fridge/service/FridgeService.java b/src/main/java/com/example/icebutler_server/fridge/service/FridgeService.java index 6c3ddc8..8b469ed 100644 --- a/src/main/java/com/example/icebutler_server/fridge/service/FridgeService.java +++ b/src/main/java/com/example/icebutler_server/fridge/service/FridgeService.java @@ -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 searchFridgeFood(Long fridgeId, Long ownerId, String foodName); FridgeFoodRes getFridgeFood(Long fridgeId, Long fridgeFoodId, Long userId); diff --git a/src/main/java/com/example/icebutler_server/fridge/service/FridgeServiceImpl.java b/src/main/java/com/example/icebutler_server/fridge/service/FridgeServiceImpl.java index 415aff4..4568bc1 100644 --- a/src/main/java/com/example/icebutler_server/fridge/service/FridgeServiceImpl.java +++ b/src/main/java/com/example/icebutler_server/fridge/service/FridgeServiceImpl.java @@ -138,13 +138,11 @@ 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) { @@ -152,24 +150,18 @@ private void exchangeFridgeOwner(FridgeUser owner, FridgeUser newOwner) { 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 fridgeUsers = fridgeUserRepository.findByFridgeAndIsEnable(fridge, true); - List 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); } // 냉장고 개별