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 38838ff9..befaf01d 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 @@ -15,6 +15,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.*; @@ -49,7 +51,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,73 +72,56 @@ 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 = "냉장고 사용자를 삭제한다.") + @Operation(summary = "냉장고 탈퇴", description = "냉장고에서 스스로 탈퇴한다.") @SwaggerApiSuccess(implementation = ResponseCustom.class) @ApiResponses(value = { @ApiResponse(responseCode = "403", description = "(G0001)권한이 없습니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), - @ApiResponse(responseCode = "404", description = "(U0000)존재하지 않는 사용자입니다.\t\n" + - "(R0000)존재하지 않는 냉장고입니다.\t\n" + - "(R0003)해당 냉장고에 존재하지 않는 사용자입니다.", + @ApiResponse(responseCode = "404", description = "(R0003)해당 냉장고에 존재하지 않는 사용자입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), }) @Auth - @PatchMapping("/{fridgeId}/remove/each") + @PatchMapping("/{fridgeId}/withdraw") public ResponseCustom removeFridgeUser(@Parameter(description = "냉장고 ID") @PathVariable Long fridgeId, @Parameter(hidden = true) @IsLogin Long userId) { - return ResponseCustom.success(fridgeService.removeFridgeUser(fridgeId, userId)); + fridgeService.removeFridgeUser(fridgeId, userId); + return ResponseCustom.success(); } - @Operation(summary = "냉장고 식품 전체 조회(카테고리별)", description = "냉장고 내 식품을 카테고리 별로 전체조회한다.") - @SwaggerApiSuccess(implementation = FridgeMainRes.class) + @Operation(summary = "냉장고 식품 검색 조회", description = "냉장고 내 식품을 검색한다.") + @SwaggerApiSuccess(implementation = FridgeFoodsRes.class) @ApiResponses(value = { @ApiResponse(responseCode = "400", description = "(F0000)존재하지 않는 카테고리입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), - @ApiResponse(responseCode = "404", description = "(U0000)존재하지 않는 사용자입니다.\t\n" + - "(R0000)존재하지 않는 냉장고입니다.", + @ApiResponse(responseCode = "404", description = "(R0003)해당 냉장고에 존재하지 않는 사용자입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), }) @Auth @GetMapping("/{fridgeId}/foods") - public ResponseCustom getFoods(@Parameter(description = "냉장고 ID") @PathVariable Long fridgeId, - @Parameter(description = "식품 카테고리") @RequestParam(required = false) String category, - @Parameter(hidden = true) @IsLogin Long userId) { - return ResponseCustom.success(fridgeService.getFoods(fridgeId, userId, category)); - } - - - @Operation(summary = "냉장고 식품 검색 조회", description = "냉장고 내 식품을 검색한다.") - @SwaggerApiSuccess(implementation = FridgeFoodsRes.class) - @ApiResponses(value = { - @ApiResponse(responseCode = "404", description = "(R0000)존재하지 않는 냉장고입니다.", - content = @Content(schema = @Schema(implementation = ResponseCustom.class))), - }) - @Auth - @GetMapping("/{fridgeId}/search") - public ResponseCustom> searchFridgeFood(@Parameter(description = "냉장고 ID") @PathVariable Long fridgeId, - @Parameter(description = "식품명") @RequestParam String keyword, - @Parameter(hidden = true) @IsLogin Long userId) { - return ResponseCustom.success(fridgeService.searchFridgeFood(fridgeId, userId, keyword)); + public ResponseCustom> searchFridgeFood(@Parameter(description = "냉장고 ID") @PathVariable Long fridgeId, + @Parameter(description = "식품 카테고리") @RequestParam(required = false) String category, + @Parameter(description = "식품명") @RequestParam(required = false) String word, + Pageable pageable, + @Parameter(hidden = true) @IsLogin Long userId) { + return ResponseCustom.success(fridgeService.searchFridgeFoods(fridgeId, userId, word, category, pageable)); } @Operation(summary = "냉장고 식품 상세 조회", description = "냉장고 내 식품을 상세 조회한다.") @@ -144,9 +129,7 @@ public ResponseCustom> searchFridgeFood(@Parameter(descript @ApiResponses(value = { @ApiResponse(responseCode = "403", description = "(G0001)권한이 없습니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), - @ApiResponse(responseCode = "404", description = "(U0000)존재하지 않는 사용자입니다.\t\n" + - "(R0000)존재하지 않는 냉장고입니다.\t\n" + - "(R0002)해당 냉장고에 존재하지 않는 식품입니다.", + @ApiResponse(responseCode = "404", description = "(R0002)해당 냉장고에 존재하지 않는 식품입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), }) @Auth @@ -224,51 +207,54 @@ public ResponseCustom deleteFridgeFood(@RequestBody DeleteFridgeFoodsReq dele } @Operation(summary = "냉장고 멤버 조회", description = "냉장고의 멤버를 조회한다.") - @SwaggerApiSuccess(implementation = FridgeUserMainRes.class) + @SwaggerApiSuccess(implementation = FridgeUserRes.class) @ApiResponses(value = { - @ApiResponse(responseCode = "404", description = "(R0000)존재하지 않는 냉장고입니다.", - content = @Content(schema = @Schema(implementation = ResponseCustom.class))), + @ApiResponse(responseCode = "403", description = "(G0001)권한이 없습니다.", + content = @Content(schema = @Schema(implementation = ResponseCustom.class))) }) @Auth - @GetMapping("{fridgeId}/members") - public ResponseCustom getMembers( + @GetMapping("/{fridgeId}/members") + public ResponseCustom> getFridgeMembers( @Parameter(description = "냉장고 ID") @PathVariable Long fridgeId, @Parameter(hidden = true) @IsLogin Long userId) { - return ResponseCustom.success(fridgeService.searchMembers(fridgeId, userId)); + return ResponseCustom.success(fridgeService.getFridgeMembers(fridgeId, userId)); } - @Operation(summary = "냉장고 선택목록 조회", description = "냉장고 선택목록을 조회한다.") - @SwaggerApiSuccess(implementation = SelectFridgesMainRes.class) + @Operation(summary = "내 냉장고 조회", description = "사용자의 냉장고를 조회한다.") + @SwaggerApiSuccess(implementation = MyFridgeRes.class) @ApiResponses(value = { @ApiResponse(responseCode = "404", description = "(U0000)존재하지 않는 사용자입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), }) @Auth - @GetMapping("/select") - public ResponseCustom selectFridges( + @GetMapping + public ResponseCustom getMyFridge( @Parameter(hidden = true) @IsLogin Long userId ) { - return ResponseCustom.success(fridgeService.selectFridges(userId)); + return ResponseCustom.success(fridgeService.getMyFridge(userId)); } - @Operation(summary = "냉장고 목록 조회", description = "냉장고 목록을 조회한다.") - @SwaggerApiSuccess(implementation = GetFridgesMainRes.class) + @Operation(summary = "냉장고 정보 조회", description = "냉장고 정보를 조회한다.") + @SwaggerApiSuccess(implementation = FridgeInfoRes.class) @ApiResponses(value = { - @ApiResponse(responseCode = "404", description = "(U0000)존재하지 않는 사용자입니다.\t\n" + - "(R0000)존재하지 않는 냉장고입니다.", + @ApiResponse(responseCode = "404", description = "(R0000)존재하지 않는 냉장고입니다.\t\n" + + "(R0003)해당 냉장고에 존재하지 않는 사용자입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), }) @Auth - @GetMapping("") - public ResponseCustom myFridge(@Parameter(hidden = true) @IsLogin Long userId) { - return ResponseCustom.success(fridgeService.myFridge(userId)); + @GetMapping("/{fridgeId}") + public ResponseCustom getFridgeInfo( + @Parameter(description = "냉장고 ID") @PathVariable Long fridgeId, + @Parameter(hidden = true) @IsLogin Long userId + ) { + return ResponseCustom.success(fridgeService.getFridgeInfo(userId, fridgeId)); } /** * [Get] 냉장고 통계 (낭비/소비) */ @Operation(summary = "냉장고 식품 삭제 통계 조회", description = "냉장고 식품의 삭제 타입별 통계를 조회한다.") - @SwaggerApiSuccess(implementation = GetFridgesMainRes.class) + @SwaggerApiSuccess(implementation = FridgeFoodsStatistics.class) @ApiResponses(value = { @ApiResponse(responseCode = "400", description = "(F0003)존재하지 않는 식품삭제 타입입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))), diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/request/FridgeFoodReq.java b/src/main/java/com/example/icebutler_server/fridge/dto/request/FridgeFoodReq.java index cd0f38f3..a1335acc 100644 --- a/src/main/java/com/example/icebutler_server/fridge/dto/request/FridgeFoodReq.java +++ b/src/main/java/com/example/icebutler_server/fridge/dto/request/FridgeFoodReq.java @@ -18,8 +18,8 @@ public class FridgeFoodReq { private String foodDetailName; @Schema(name = "foodCategory", description = "식품 카테고리") private String foodCategory; - @Schema(name = "shelfLife", description = "식품 소비기한") - private String shelfLife; + @Schema(name = "expirationDate", description = "식품 소비기한") + private String expirationDate; @Schema(name = "ownerId", description = "식품 소유자 ID") private Long ownerId; @Schema(name = "memo", description = "식품 메모") diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeFoodRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeFoodRes.java index 9910f643..5052db02 100644 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeFoodRes.java +++ b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeFoodRes.java @@ -1,6 +1,5 @@ package com.example.icebutler_server.fridge.dto.response; -import com.example.icebutler_server.global.util.FridgeUtils; import com.example.icebutler_server.fridge.entity.FridgeFood; import com.example.icebutler_server.global.util.AwsS3ImageUrlUtil; import io.swagger.v3.oas.annotations.media.Schema; @@ -9,33 +8,33 @@ import lombok.Data; import lombok.NoArgsConstructor; -import java.time.format.DateTimeFormatter; +import java.time.LocalDate; @Data @Builder @AllArgsConstructor @NoArgsConstructor -@Schema(name = "FridgeFoodRes", description = "냉장고 식품 상세 정보") +@Schema(name = "냉장고 식품 상세 정보", description = "FridgeFoodRes") public class FridgeFoodRes { - @Schema(name = "fridgeFoodId", description = "냉장고 ID") + @Schema(description = "냉장고 ID", example = "1") private Long fridgeFoodId; - @Schema(name = "foodId", description = "식품 ID") + @Schema(description = "식품 ID", example = "1") private Long foodId; - @Schema(name = "foodName", description = "식품명") + @Schema(description = "식품명", example = "사과") private String foodName; - @Schema(name = "foodDetailName", description = "식품 상세명") + @Schema(description = "식품 상세명", example = "무농약 사과") private String foodDetailName; - @Schema(name = "foodCategory", description = "식품 카테고리") + @Schema(description = "식품 카테고리", example = "채소") private String foodCategory; - @Schema(name = "shelfLife", description = "식품 소비기한") - private String shelfLife; - @Schema(name = "day", description = "식품 소비기한 디데이") - private int day; - @Schema(name = "owner", description = "식품 소유자") + @Schema(description = "식품 소비기한", example = "2024-01-01") + private LocalDate expirationDate; + @Schema(description = "남은 소비기간", example = "3") + private int shelfLife; + @Schema(description = "식품 소유자", example = "나야나") private String owner; - @Schema(name = "memo", description = "식품 메모") + @Schema(description = "식품 메모", example = "먹지마세요.") private String memo; - @Schema(name = "imgUrl", description = "식품 이미지 URL") + @Schema(description = "식품 이미지 URL", example = "https://~~/apple.jpg") private String imgUrl; public static FridgeFoodRes toDto(FridgeFood fridgeFood) { @@ -45,8 +44,8 @@ public static FridgeFoodRes toDto(FridgeFood fridgeFood) { .foodName(fridgeFood.getFood().getFoodName()) .foodDetailName(fridgeFood.getFoodDetailName()) .foodCategory(fridgeFood.getFood().getFoodCategory().getName()) - .shelfLife(fridgeFood.getShelfLife().format(DateTimeFormatter.ISO_DATE)) - .day(FridgeUtils.calShelfLife(fridgeFood.getShelfLife())) + .expirationDate(fridgeFood.getExpirationDate()) + .shelfLife(fridgeFood.getShelfLife()) .owner(fridgeFood.getOwner() == null ? null : fridgeFood.getOwner().getNickname()) .memo(fridgeFood.getMemo()) .imgUrl(fridgeFood.getFridgeFoodImgKey() == null ? null : AwsS3ImageUrlUtil.toUrl(fridgeFood.getFridgeFoodImgKey())) diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeFoodsRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeFoodsRes.java index 087afed1..c5f4202c 100644 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeFoodsRes.java +++ b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeFoodsRes.java @@ -3,33 +3,40 @@ import com.example.icebutler_server.global.util.FridgeUtils; import com.example.icebutler_server.fridge.entity.FridgeFood; import com.example.icebutler_server.global.util.AwsS3ImageUrlUtil; +import com.querydsl.core.annotations.QueryProjection; import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @Builder -@AllArgsConstructor @NoArgsConstructor @Schema(name = "FridgeFoodsRes", description = "냉장고 식품 정보") public class FridgeFoodsRes { - @Schema(name = "fridgeFoodId", description = "냉장고 식품 ID") - private Long fridgeFoodId; - @Schema(name = "foodName", description = "냉장고 식품 이름") - private String foodName; - @Schema(name = "foodImgUrl", description = "냉장고 식품 이미지 URL") - private String foodImgUrl; - @Schema(name = "shelfLife", description = "식품 유효기한") - private int shelfLife; + @Schema(description = "냉장고 식품 ID", example = "1") + private Long fridgeFoodId; + @Schema(description = "냉장고 식품 이름", example = "사과") + private String foodName; + @Schema(description = "냉장고 식품 이미지 URL", example = "https://~~/apple.jpg") + private String foodImgUrl; + @Schema(description = "남은 소비기간", example = "4") + private int shelfLife; - public static FridgeFoodsRes toDto(FridgeFood fridgeFood) { - return FridgeFoodsRes.builder() - .fridgeFoodId(fridgeFood.getId()) - .foodName(fridgeFood.getFood().getFoodName()) - .foodImgUrl(AwsS3ImageUrlUtil.toUrl(fridgeFood.getFood().getFoodImgKey())) - .shelfLife(FridgeUtils.calShelfLife(fridgeFood.getShelfLife())) - .build(); - } + @QueryProjection + public FridgeFoodsRes(Long fridgeFoodId, String foodName, String foodImgUrl, int shelfLife) { + this.fridgeFoodId = fridgeFoodId; + this.foodName = foodName; + this.foodImgUrl = AwsS3ImageUrlUtil.toUrl(foodImgUrl); + this.shelfLife = shelfLife; + } + + public static FridgeFoodsRes toDto(FridgeFood fridgeFood) { + return FridgeFoodsRes.builder() + .fridgeFoodId(fridgeFood.getId()) + .foodName(fridgeFood.getFood().getFoodName()) + .foodImgUrl(AwsS3ImageUrlUtil.toUrl(fridgeFood.getFood().getFoodImgKey())) + .shelfLife(FridgeUtils.calShelfLife(fridgeFood.getExpirationDate())) + .build(); + } } \ No newline at end of file diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeInfoRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeInfoRes.java new file mode 100644 index 00000000..11fa504e --- /dev/null +++ b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeInfoRes.java @@ -0,0 +1,36 @@ +package com.example.icebutler_server.fridge.dto.response; + +import com.example.icebutler_server.fridge.entity.Fridge; +import com.example.icebutler_server.fridge.entity.FridgeUser; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; +import lombok.Data; + +import java.util.List; +import java.util.stream.Collectors; + +@Data +@Builder +@Schema(name = "가정용 냉장고 정보", description = "FridgeInfoRes") +public class FridgeInfoRes { + @Schema(description = "냉장고 ID", example = "1") + private Long fridgeId; + @Schema(description = "냉장고 이름", example = "우리집 냉장고") + private String fridgeName; + @Schema(description = "냉장고 설명", example = "환영합니다~") + private String comment; + @Schema(description = "냉장고 유저 정보") + private List users; + @Schema(description = "냉장고 유저수", example = "4") + private int userCnt; + + public static FridgeInfoRes toDto(Fridge fridge, List fridgeUsers) { + return FridgeInfoRes.builder() + .fridgeId(fridge.getId()) + .fridgeName(fridge.getFridgeName()) + .comment(fridge.getFridgeComment()) + .users(fridgeUsers.stream().map(FridgeUserRes::toDto).collect(Collectors.toList())) + .userCnt(fridgeUsers.size()) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeMainRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeMainRes.java deleted file mode 100644 index dbcd8a22..00000000 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeMainRes.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.example.icebutler_server.fridge.dto.response; - -import com.example.icebutler_server.global.util.FridgeUtils; -import com.example.icebutler_server.fridge.entity.FridgeFood; -import com.example.icebutler_server.global.util.AwsS3ImageUrlUtil; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -@Schema(name = "FridgeMainRes", description = "냉장고 식품 정보") -public class FridgeMainRes { - @Schema(name = "foodList", description = "냉장고 식품 정보") - private List foodList = new ArrayList<>(); - - public static FridgeMainRes toFridgeDto(List fridgeFoods) { - return new FridgeMainRes(fridgeFoods.stream() - .map(ff -> new FridgeFoodsRes(ff.getId(), ff.getFood().getFoodName(), AwsS3ImageUrlUtil.toUrl(ff.getFood().getFoodImgKey()), FridgeUtils.calShelfLife(ff.getShelfLife()))) - .collect(Collectors.toList())); - } - - private static FridgeDiscardRes isNullFoodDiscardRes(FridgeDiscardRes fridgeDiscard){ - return fridgeDiscard != null ? fridgeDiscard : new FridgeDiscardRes(); - } - -} \ No newline at end of file diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeRes.java deleted file mode 100644 index 8fc12e6a..00000000 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeRes.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.example.icebutler_server.fridge.dto.response; - -import com.example.icebutler_server.fridge.entity.Fridge; -import com.example.icebutler_server.fridge.entity.FridgeUser; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -@Data -@AllArgsConstructor -@NoArgsConstructor -@Schema(name = "FridgeRes", description = "가정용 냉장고 정보") -public class FridgeRes { - @Schema(name = "fridgeId", description = "냉장고 ID") - private Long fridgeId; - @Schema(name = "fridgeName", description = "냉장고 이름") - private String fridgeName; - @Schema(name = "comment", description = "냉장고 설명") - private String comment; - @Schema(name = "users", description = "냉장고 유저 정보") - private List users; - @Schema(name = "userCnt", description = "냉장고 유저수") - private Integer userCnt; - - public static FridgeRes toDto(Fridge fridge, List> fridgeUserList) { - List fridgeUsers = new ArrayList<>(); - for (List fridgeArr : fridgeUserList) { - for (FridgeUser fridgeUser : fridgeArr) { - if (fridgeUser.getFridge().getId().equals(fridge.getId())) fridgeUsers.add(fridgeUser); - } - } - - FridgeRes fridgeRes = new FridgeRes(); - fridgeRes.fridgeId = fridge.getId(); - fridgeRes.fridgeName = fridge.getFridgeName(); - fridgeRes.comment = fridge.getFridgeComment(); - fridgeRes.users = fridgeUsers.stream().map(m -> FridgeUserRes.toDto(m.getUser(), m.getRole())).collect(Collectors.toList()); - fridgeRes.userCnt = fridgeUsers.size(); - return fridgeRes; - } -} \ No newline at end of file diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUserMainRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUserMainRes.java deleted file mode 100644 index dd7aa701..00000000 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUserMainRes.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.icebutler_server.fridge.dto.response; - -import com.example.icebutler_server.fridge.entity.FridgeUser; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.List; -import java.util.stream.Collectors; - -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -@Schema(name = "FridgeUserMainRes", description = "냉장고 멤버 정보") -public class FridgeUserMainRes { - @Schema(name = "fridgeUsers", description = "냉장고 멤버 정보") - List fridgeUsers; - - public static FridgeUserMainRes doDto(List fridgeUsers){ - FridgeUserMainRes fridgeUserMainRes=new FridgeUserMainRes(); - fridgeUserMainRes.fridgeUsers=fridgeUsers.stream().map(cf->FridgeUsersRes.toDto(cf.getUser())).collect(Collectors.toList()); - return fridgeUserMainRes; - } - -} diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUserRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUserRes.java index fc60eacc..8d87b30c 100644 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUserRes.java +++ b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUserRes.java @@ -1,36 +1,31 @@ package com.example.icebutler_server.fridge.dto.response; +import com.example.icebutler_server.fridge.entity.FridgeUser; import com.example.icebutler_server.global.entity.FridgeRole; import com.example.icebutler_server.global.util.AwsS3ImageUrlUtil; -import com.example.icebutler_server.user.entity.User; import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; @Data -@AllArgsConstructor -@NoArgsConstructor @Builder -@Schema(name = "FridgeUserRes", description = "냉장고 유저 정보") +@Schema(name = "냉장고 유저 정보", description = "FridgeUserRes") public class FridgeUserRes { - @Schema(name = "userId", description = "유저 ID") + @Schema(description = "유저 ID", example = "1") private Long userId; - @Schema(name = "nickname", description = "유저 닉네임") + @Schema(description = "유저 닉네임", example = "나야나") private String nickname; - @Schema(name = "role", description = "냉장고 내 유저 역할") + @Schema(description = "냉장고 내 유저 역할", allowableValues = {"OWNER", "MEMBER"}) private FridgeRole role; - @Schema(name = "profileImgUrl", description = "유저 프로필 이미지 URL") + @Schema(description = "유저 프로필 이미지 URL", example = "https://~/asdfeg.jpg") private String profileImgUrl; - public static FridgeUserRes toDto(User user, FridgeRole role) { - FridgeUserRes fridgeUserRes = new FridgeUserRes(); - fridgeUserRes.userId = user.getId(); - fridgeUserRes.nickname = user.getNickname(); - fridgeUserRes.role = role; - fridgeUserRes.profileImgUrl = AwsS3ImageUrlUtil.toUrl(user.getProfileImgKey()); - return fridgeUserRes; + public static FridgeUserRes toDto(FridgeUser fridgeUser) { + return FridgeUserRes.builder() + .userId(fridgeUser.getUser().getId()) + .nickname(fridgeUser.getUser().getNickname()) + .role(fridgeUser.getRole()) + .profileImgUrl(AwsS3ImageUrlUtil.toUrl(fridgeUser.getUser().getProfileImgKey())) + .build(); } - } diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUsersRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUsersRes.java deleted file mode 100644 index 3765b38d..00000000 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/FridgeUsersRes.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.example.icebutler_server.fridge.dto.response; - -import com.example.icebutler_server.global.util.AwsS3ImageUrlUtil; -import com.example.icebutler_server.user.entity.User; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@NoArgsConstructor -@Schema(name = "FridgeUsersRes", description = "냉장고 멤버 정보") -public class FridgeUsersRes { - @Schema(name = "userId", description = "유저 ID") - private Long userId; - @Schema(name = "nickName", description = "유저 닉네임") - private String nickName; - @Schema(name = "profileImageUrl", description = "유저 프로필 이미지 URL") - private String profileImageUrl; - - public static FridgeUsersRes toDto(User user){ - FridgeUsersRes fridgeUsersRes=new FridgeUsersRes(); - fridgeUsersRes.nickName=user.getNickname(); - fridgeUsersRes.userId =user.getId(); - fridgeUsersRes.profileImageUrl=AwsS3ImageUrlUtil.toUrl(user.getProfileImgKey()); - return fridgeUsersRes; - } -} diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/GetFridgesMainRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/GetFridgesMainRes.java deleted file mode 100644 index c5f20d3f..00000000 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/GetFridgesMainRes.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.example.icebutler_server.fridge.dto.response; - -import com.example.icebutler_server.fridge.entity.FridgeUser; -import com.example.icebutler_server.global.exception.BaseException; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.List; -import java.util.stream.Collectors; - -import static com.example.icebutler_server.global.exception.ReturnCode.NOT_FOUND_FRIDGE_USER; - -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -@Schema(name = "GetFridgesMainRes", description = "마이냉장고 정보") -public class GetFridgesMainRes { - @Schema(name = "fridgeList", description = "가정용 냉장고 정보") - List fridgeList; - - public static GetFridgesMainRes toDto(List> fridgeUserListList, Long userId) { - GetFridgesMainRes getFridgesMainRes = new GetFridgesMainRes(); - - List fridgeUsers = fridgeUserListList.stream().map(m -> m.stream().filter(f -> f.getUser().getId().equals(userId)).findAny().orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE_USER))).collect(Collectors.toList()); - getFridgesMainRes.fridgeList = fridgeUsers.stream().map(m -> FridgeRes.toDto(m.getFridge(), fridgeUserListList)).collect(Collectors.toList()); - - return getFridgesMainRes; - } -} diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/MyFridgeRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/MyFridgeRes.java new file mode 100644 index 00000000..1a087f71 --- /dev/null +++ b/src/main/java/com/example/icebutler_server/fridge/dto/response/MyFridgeRes.java @@ -0,0 +1,23 @@ +package com.example.icebutler_server.fridge.dto.response; + +import com.example.icebutler_server.fridge.entity.FridgeUser; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +@Schema(name = "내 냉장고 정보", description = "SelectFridgeRes") +public class MyFridgeRes { + @Schema(description = "냉장고 ID", example = "1") + private Long fridgeId; + @Schema(description = "냉장고 이름", example = "우리집 냉장고") + private String fridgeName; + + public static MyFridgeRes toDto(FridgeUser fridgeUser) { + return MyFridgeRes.builder() + .fridgeId(fridgeUser.getFridge().getId()) + .fridgeName(fridgeUser.getFridge().getFridgeName()) + .build(); + } +} diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/SelectFridgeRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/SelectFridgeRes.java deleted file mode 100644 index 857c3c9d..00000000 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/SelectFridgeRes.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.example.icebutler_server.fridge.dto.response; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; - -@Data -@Schema(name = "SelectFridgeRes", description = "냉장고 정보") -public class SelectFridgeRes { - @Schema(name = "fridgeName", description = "냉장고 이름") - private String fridgeName; - @Schema(name = "fridgeId", description = "냉장고 ID") - private Long fridgeId; - - public static SelectFridgeRes toDto(String fridgeName, Long fridgeId) { - SelectFridgeRes selectFridgeRes = new SelectFridgeRes(); - selectFridgeRes.fridgeId = fridgeId; - selectFridgeRes.fridgeName = fridgeName; - return selectFridgeRes; - } -} diff --git a/src/main/java/com/example/icebutler_server/fridge/dto/response/SelectFridgesMainRes.java b/src/main/java/com/example/icebutler_server/fridge/dto/response/SelectFridgesMainRes.java deleted file mode 100644 index 901352eb..00000000 --- a/src/main/java/com/example/icebutler_server/fridge/dto/response/SelectFridgesMainRes.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.example.icebutler_server.fridge.dto.response; - -import com.example.icebutler_server.fridge.entity.FridgeUser; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.List; -import java.util.stream.Collectors; - -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -public class SelectFridgesMainRes { - - List fridgeList; - - public static SelectFridgesMainRes toDto(List fridgeUsers) { - SelectFridgesMainRes selectFridgesMainRes = new SelectFridgesMainRes(); - - selectFridgesMainRes.fridgeList = fridgeUsers.stream().map(m -> SelectFridgeRes.toDto(m.getFridge().getFridgeName(), m.getFridge().getId())).collect(Collectors.toList()); - - return selectFridgesMainRes; - } -} 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 bf1ad3c3..ba54dabf 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/entity/FridgeFood.java b/src/main/java/com/example/icebutler_server/fridge/entity/FridgeFood.java index d40acf21..67b2fd1e 100644 --- a/src/main/java/com/example/icebutler_server/fridge/entity/FridgeFood.java +++ b/src/main/java/com/example/icebutler_server/fridge/entity/FridgeFood.java @@ -4,6 +4,7 @@ import com.example.icebutler_server.food.entity.FoodDeleteStatus; import com.example.icebutler_server.fridge.dto.request.FridgeFoodReq; import com.example.icebutler_server.global.entity.BaseEntity; +import com.example.icebutler_server.global.util.FridgeUtils; import com.example.icebutler_server.user.entity.User; import lombok.AccessLevel; import lombok.Builder; @@ -25,7 +26,10 @@ public class FridgeFood extends BaseEntity { private Long id; @Column(nullable = false) - private LocalDate shelfLife; + private LocalDate expirationDate; + + @Column(nullable = false) + private int shelfLife; private String fridgeFoodImgKey; @@ -35,7 +39,7 @@ public class FridgeFood extends BaseEntity { private String foodDetailName; @Enumerated(EnumType.STRING) - private FoodDeleteStatus foodDeleteStatus; + private FoodDeleteStatus foodDeleteStatus = null; @ManyToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "food_id") @@ -50,25 +54,26 @@ public class FridgeFood extends BaseEntity { private Fridge fridge; @Builder - public FridgeFood(User owner, Food food, Fridge fridge, String foodDetailName, LocalDate shelfLife, String memo, String fridgeFoodImgKey) { - this.shelfLife = shelfLife; + public FridgeFood(User owner, Food food, Fridge fridge, String foodDetailName, LocalDate expirationDate, String memo, String fridgeFoodImgKey) { + this.expirationDate = expirationDate; + this.shelfLife = FridgeUtils.calShelfLife(expirationDate); this.fridgeFoodImgKey = fridgeFoodImgKey; this.memo = memo; this.foodDetailName = foodDetailName; this.owner = owner; this.food = food; this.fridge = fridge; - this.foodDeleteStatus = null; } public void updateFridgeFoodInfo(Food food) { this.food = food; } - public void updateFridgeFoodInfo(String foodDetailName, String memo, LocalDate shelfLife, String imgUrl) { + public void updateFridgeFoodInfo(String foodDetailName, String memo, LocalDate expirationDate, String imgUrl) { this.foodDetailName = foodDetailName; this.memo = memo; - this.shelfLife = shelfLife; + this.expirationDate = expirationDate; + this.shelfLife = FridgeUtils.calShelfLife(expirationDate); this.fridgeFoodImgKey = imgUrl; } @@ -76,10 +81,6 @@ public void updateFridgeFoodOwner(User newOwner) { this.owner = newOwner; } - public void remove() { - this.setIsEnable(false); - } - public void removeWithStatus(FoodDeleteStatus deleteStatus) { this.setIsEnable(false); this.foodDeleteStatus = deleteStatus; @@ -90,7 +91,7 @@ public static FridgeFood toEntity(User owner, Fridge fridge, Food food, FridgeFo .fridge(fridge) .food(food) .foodDetailName(fridgeFoodReq.getFoodDetailName()) - .shelfLife(LocalDate.parse(fridgeFoodReq.getShelfLife())) + .expirationDate(LocalDate.parse(fridgeFoodReq.getExpirationDate())) .owner(owner) .memo(fridgeFoodReq.getMemo()) .fridgeFoodImgKey(fridgeFoodReq.getImgKey()) diff --git a/src/main/java/com/example/icebutler_server/fridge/entity/FridgeUser.java b/src/main/java/com/example/icebutler_server/fridge/entity/FridgeUser.java index 7f8f49d2..7a45a2f3 100644 --- a/src/main/java/com/example/icebutler_server/fridge/entity/FridgeUser.java +++ b/src/main/java/com/example/icebutler_server/fridge/entity/FridgeUser.java @@ -56,8 +56,4 @@ public void changeRoleToOwner(){ public void changeRoleToMember(){ this.role = FridgeRole.MEMBER; } - - public void remove() { - this.setIsEnable(false); - } } diff --git a/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodCustom.java b/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodCustom.java index 754d9d40..ab8cc3da 100644 --- a/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodCustom.java +++ b/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodCustom.java @@ -4,17 +4,20 @@ import com.example.icebutler_server.food.entity.FoodCategory; import com.example.icebutler_server.food.entity.FoodDeleteStatus; import com.example.icebutler_server.fridge.dto.response.FridgeDiscardRes; +import com.example.icebutler_server.fridge.dto.response.FridgeFoodsRes; import com.example.icebutler_server.fridge.entity.Fridge; import com.example.icebutler_server.fridge.entity.FridgeFood; import com.example.icebutler_server.fridge.entity.FridgeUser; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import java.util.List; public interface FridgeFoodCustom { + Page searchFridgeFoods(Long fridgeId, String word, String category, Pageable pageable); Long findByDeleteCategoryForStatistics(FoodDeleteStatus deleteCategory, Fridge fridge, FoodCategory category, Integer year, Integer month); FridgeDiscardRes findByFridgeForDisCardFood(Fridge fridge); List findByUserForFridgeRecipeFoodList(Fridge fridge); void deleteOwnerByFridgeUser(FridgeUser fridgeUser); - List findByActiveAndShelfLifeLimit3(); } diff --git a/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodRepository.java b/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodRepository.java index 11bc52ec..d7028f0d 100644 --- a/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodRepository.java +++ b/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodRepository.java @@ -1,29 +1,17 @@ package com.example.icebutler_server.fridge.repository.FridgeFood; import com.example.icebutler_server.food.entity.Food; -import com.example.icebutler_server.food.entity.FoodCategory; import com.example.icebutler_server.fridge.entity.Fridge; import com.example.icebutler_server.fridge.entity.FridgeFood; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Modifying; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; -import java.util.List; import java.util.Optional; @Repository public interface FridgeFoodRepository extends JpaRepository, FridgeFoodCustom { Optional findByIdAndFridgeAndIsEnable(Long fridgeFoodId, Fridge fridge, Boolean status); - List findByFridgeAndFood_FoodCategoryAndIsEnableOrderByShelfLife(Fridge fridge, FoodCategory foodCategory, Boolean status); - List findByFridgeAndIsEnableOrderByShelfLife(Fridge fridge, Boolean status); - List findByFoodDetailNameContainingAndFridgeAndIsEnable(String keyword, Fridge fridge, Boolean isEnable); + Optional findByIdAndFridgeIdAndIsEnable(Long fridgeFoodId, Long fridgeId, Boolean status); void deleteByFridge(Fridge fridge); void deleteByFood(Food food); - - //TODO: cascade 처리하면 이거 지우기 - @Modifying - @Query("update FridgeFood f set f.isEnable = :status where f.fridge = :fridge") - void removeFridgeFoodByFridge(@Param("status") Boolean status, @Param("fridge") Fridge fridge); } diff --git a/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodRepositoryImpl.java b/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodRepositoryImpl.java index 412a38bf..52027adc 100644 --- a/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodRepositoryImpl.java +++ b/src/main/java/com/example/icebutler_server/fridge/repository/FridgeFood/FridgeFoodRepositoryImpl.java @@ -4,12 +4,19 @@ import com.example.icebutler_server.food.entity.FoodCategory; import com.example.icebutler_server.food.entity.FoodDeleteStatus; import com.example.icebutler_server.fridge.dto.response.FridgeDiscardRes; +import com.example.icebutler_server.fridge.dto.response.FridgeFoodsRes; import com.example.icebutler_server.fridge.dto.response.QFridgeDiscardRes; +import com.example.icebutler_server.fridge.dto.response.QFridgeFoodsRes; import com.example.icebutler_server.fridge.entity.Fridge; import com.example.icebutler_server.fridge.entity.FridgeFood; import com.example.icebutler_server.fridge.entity.FridgeUser; +import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import java.time.LocalDate; import java.util.List; @@ -20,8 +27,41 @@ import static com.example.icebutler_server.fridge.entity.QFridgeUser.fridgeUser; @RequiredArgsConstructor -public class FridgeFoodRepositoryImpl implements FridgeFoodCustom{ +public class FridgeFoodRepositoryImpl implements FridgeFoodCustom { private final JPAQueryFactory jpaQueryFactory; + + @Override + public Page searchFridgeFoods(Long fridgeId, String word, String category, Pageable p) { + List list = jpaQueryFactory.select(new QFridgeFoodsRes( + fridgeFood.id, + fridgeFood.food.foodName, + fridgeFood.food.foodImgKey, + fridgeFood.shelfLife)) + .from(fridgeFood) + .where(eqCategory(category), containsWord(word), fridgeFood.isEnable.eq(true)) + .orderBy(fridgeFood.shelfLife.asc()) + .offset(p.getOffset()) + .limit(p.getPageSize()) + .fetch(); + + Long count = jpaQueryFactory.select(fridgeFood.count()) + .from(fridgeFood) + .where(eqCategory(category), containsWord(word), fridgeFood.isEnable.eq(true)) + .fetchOne(); + + return new PageImpl<>(list, p, count); + } + + private BooleanExpression eqCategory(String category) { + return StringUtils.isEmpty(category) ? null : + fridgeFood.food.foodCategory.eq(FoodCategory.getFoodCategoryByName(category)); + } + + private BooleanExpression containsWord(String word) { + return StringUtils.isEmpty(word) ? null : + fridgeFood.foodDetailName.contains(word); + } + @Override public Long findByDeleteCategoryForStatistics(FoodDeleteStatus deleteCategory, Fridge fridge, FoodCategory category, Integer year, Integer month) { return jpaQueryFactory.select(fridgeFood.count()) @@ -76,8 +116,6 @@ public List findByUserForFridgeRecipeFoodList(Fridge fridgeEntity) { .fetch(); } - - @Override public void deleteOwnerByFridgeUser(FridgeUser fridgeUser) { jpaQueryFactory.update(fridgeFood) @@ -90,10 +128,9 @@ public void deleteOwnerByFridgeUser(FridgeUser fridgeUser) { public List findByActiveAndShelfLifeLimit3() { LocalDate startDate = LocalDate.now(); LocalDate endDate = startDate.plusDays(3); - return jpaQueryFactory.selectFrom(fridgeFood) + return jpaQueryFactory.selectFrom(fridgeFood) .where(fridgeFood.isEnable.eq(true) - .and(fridgeFood.shelfLife.between(startDate, endDate))) - .fetch(); - + .and(fridgeFood.expirationDate.between(startDate, endDate))) + .fetch(); } } 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 ec093bb0..49845023 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 @@ -15,13 +15,18 @@ public interface FridgeUserRepository extends JpaRepository { Optional findByUserAndFridgeAndIsEnable(User user, Fridge fridge, Boolean isEnable); + Optional findByUserIdAndFridgeIdAndIsEnable(Long userId, Long fridgeId, Boolean isEnable); + Optional findByFridgeAndUserIdAndRoleAndIsEnable(Fridge fridge, Long userId, FridgeRole fridgeRole, Boolean status); List findByFridgeAndIsEnable(Fridge fridge, Boolean isEnable); + List findByFridgeIdAndIsEnable(Long fridgeId, Boolean isEnable); + + boolean existsByFridgeAndRoleAndIsEnable(Fridge fridge, FridgeRole role, boolean isEnable); - List findByFridgeAndIsEnableOrderByRoleDesc(Fridge fridge, Boolean isEnable); + Optional findByFridgeIdAndUserIdAndIsEnable(Long fridgeId, Long userId, boolean isEnable); - List findByUserAndIsEnable(User user, Boolean status); + Optional findByUserIdAndIsEnable(Long userId, Boolean status); Optional findByFridgeAndUserAndIsEnable(Fridge fridge, User user, Boolean isEnable); 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 6c3ddc8c..12db3e49 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 @@ -2,23 +2,39 @@ import com.example.icebutler_server.fridge.dto.request.*; import com.example.icebutler_server.fridge.dto.response.*; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; -import java.io.IOException; import java.util.List; 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); - Long removeFridgeUser(Long fridgeId, Long userId) throws IOException; - List searchFridgeFood(Long fridgeId, Long ownerId, String foodName); - FridgeFoodRes getFridgeFood(Long fridgeId, Long fridgeFoodId, Long userId); - void addFridgeFood(FridgeFoodsReq fridgeFoodsReq, Long fridgeId, Long userId); - void modifyFridgeFood(Long fridgeId, Long fridgeFoodId, FridgeFoodReq fridgeFoodReq, Long userId); - void deleteFridgeFood(DeleteFridgeFoodsReq deleteFridgeFoodsReq, String deleteType, Long fridgeId, Long userId); - FridgeUserMainRes searchMembers(Long fridgeId, Long userId); - FridgeFoodsStatistics getFridgeFoodStatistics(Long fridgeId, String deleteCategory, Long userId, Integer year, Integer month); - RecipeFridgeFoodListsRes getFridgeUserFoodList(Long fridgeId, Long userId); - void notifyFridgeFood(); + Long addFridge(AddFridgeReq registerFridgeReq, Long ownerId); + + void modifyFridge(Long fridgeId, EditFridgeReq updateFridgeReq, Long userId); + + void removeFridge(Long fridgeId, Long userId); + + void removeFridgeUser(Long fridgeId, Long userId); + + Page searchFridgeFoods(Long fridgeId, Long ownerId, String foodName, String category, Pageable pageable); + + FridgeFoodRes getFridgeFood(Long fridgeId, Long fridgeFoodId, Long userId); + + void addFridgeFood(FridgeFoodsReq fridgeFoodsReq, Long fridgeId, Long userId); + + void modifyFridgeFood(Long fridgeId, Long fridgeFoodId, FridgeFoodReq fridgeFoodReq, Long userId); + + void deleteFridgeFood(DeleteFridgeFoodsReq deleteFridgeFoodsReq, String deleteType, Long fridgeId, Long userId); + + List getFridgeMembers(Long fridgeId, Long userId); + + FridgeFoodsStatistics getFridgeFoodStatistics(Long fridgeId, String deleteCategory, Long userId, Integer year, Integer month); + + RecipeFridgeFoodListsRes getFridgeUserFoodList(Long fridgeId, Long userId); + + MyFridgeRes getMyFridge(Long userId); + + FridgeInfoRes getFridgeInfo(Long userId, Long fridgeId); + + void notifyFridgeFood(); } 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 415aff4e..e3757302 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 @@ -23,15 +23,14 @@ import com.example.icebutler_server.user.entity.User; import com.example.icebutler_server.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.time.LocalDate; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; import static com.example.icebutler_server.global.exception.ReturnCode.*; @@ -51,20 +50,6 @@ public class FridgeServiceImpl implements FridgeService { private final AmazonSQSSender amazonSQSSender; private final NotificationServiceImpl alarmService; - @Override - public FridgeMainRes getFoods(Long fridgeId, Long userId, String category) { - User user = this.userRepository.findByIdAndIsEnable(userId, true).orElseThrow(() -> new BaseException(NOT_FOUND_USER)); - Fridge fridge = this.fridgeRepository.findByIdAndIsEnable(fridgeId, true).orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE)); - - if (category == null) { - // 값이 없으면 전체 조회 - return FridgeMainRes.toFridgeDto(this.fridgeFoodRepository.findByFridgeAndIsEnableOrderByShelfLife(fridge, true)); - } else { - // 값이 있으면 특정 값을 불러온 조회 - return FridgeMainRes.toFridgeDto(this.fridgeFoodRepository.findByFridgeAndFood_FoodCategoryAndIsEnableOrderByShelfLife(fridge, FoodCategory.getFoodCategoryByName(category), true)); - } - } - @Override @Transactional public Long addFridge(AddFridgeReq addFridgeReq, Long ownerId) { @@ -85,11 +70,9 @@ public Long addFridge(AddFridgeReq addFridgeReq, Long ownerId) { fridgeUserRepository.saveAll(members); cartRepository.save(Cart.toEntity(fridge)); - for (FridgeUser fridgeUser : members) alarmService.sendJoinFridgeAlarm(fridgeUser.getUser(), fridge.getFridgeName()); - return fridge.getId(); } @@ -134,17 +117,14 @@ private void updateFridgeUsers(EditFridgeReq editFridgeReq, Fridge fridge) { this.fridgeUserRepository.saveAll(newFridgeUser); } - if (!membersToDelete.isEmpty()) { + if (!membersToDelete.isEmpty()) 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,53 +132,45 @@ 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); } - // 냉장고 개별 + // 냉장고 탈퇴 @Override @Transactional - public Long removeFridgeUser(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 fridgeUser = (FridgeUser) fridgeUserRepository.findByUserAndFridgeAndIsEnable(user, fridge, true).orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE_USER)); - + public void removeFridgeUser(Long fridgeId, Long userId) { + FridgeUser fridgeUser = fridgeUserRepository.findByFridgeIdAndUserIdAndIsEnable(userId, fridgeId, true) + .orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE_USER)); if (fridgeUser.getRole() == FridgeRole.OWNER) throw new BaseException(NO_PERMISSION); - fridgeUser.remove(); - return fridge.getId(); + fridgeUserRepository.delete(fridgeUser); } + // 냉장고 식품 검색 @Override - public List searchFridgeFood(Long fridgeId, Long ownerId, String keyword) { - Fridge fridge = fridgeRepository.findByIdAndIsEnable(fridgeId, true).orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE)); - List searchFoods = fridgeFoodRepository.findByFoodDetailNameContainingAndFridgeAndIsEnable(keyword, fridge, true); - return searchFoods.stream().map(FridgeFoodsRes::toDto).collect(Collectors.toList()); + public Page searchFridgeFoods(Long fridgeId, Long userId, String word, String category, Pageable p) { + fridgeUserRepository.findByFridgeIdAndUserIdAndIsEnable(fridgeId, userId, true).orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE_USER)); + return fridgeFoodRepository.searchFridgeFoods(fridgeId, word, category, p); } + // 냉장고 식품 상세 조회 @Override public FridgeFoodRes getFridgeFood(Long fridgeId, Long fridgeFoodId, 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)); - fridgeUserRepository.findByUserAndFridgeAndIsEnable(user, fridge, true).orElseThrow(() -> new BaseException(NO_PERMISSION)); - FridgeFood fridgeFood = fridgeFoodRepository.findByIdAndFridgeAndIsEnable(fridgeFoodId, fridge, true).orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE_FOOD)); + fridgeUserRepository.findByUserIdAndFridgeIdAndIsEnable(userId, fridgeId, true) + .orElseThrow(() -> new BaseException(NO_PERMISSION)); + FridgeFood fridgeFood = fridgeFoodRepository.findByIdAndFridgeIdAndIsEnable(fridgeFoodId, fridgeId, true) + .orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE_FOOD)); return FridgeFoodRes.toDto(fridgeFood); } @@ -254,7 +226,7 @@ public void modifyFridgeFood(Long fridgeId, Long fridgeFoodId, FridgeFoodReq fri modifyFridgeFood.updateFridgeFoodInfo( fridgeFoodReq.getFoodDetailName(), fridgeFoodReq.getMemo(), - LocalDate.parse(fridgeFoodReq.getShelfLife()), + LocalDate.parse(fridgeFoodReq.getExpirationDate()), fridgeFoodReq.getImgKey() ); @@ -291,10 +263,12 @@ public void deleteFridgeFood(DeleteFridgeFoodsReq deleteFridgeFoodsReq, String t @Override //냉장고 내 유저 조회 - public FridgeUserMainRes searchMembers(Long fridgeId, Long userId) { - Fridge fridge = fridgeRepository.findByIdAndIsEnable(fridgeId, true) - .orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE)); - return FridgeUserMainRes.doDto(fridgeUserRepository.findByFridgeAndIsEnable(fridge, true)); + public List getFridgeMembers(Long fridgeId, Long userId) { + fridgeUserRepository.findByFridgeIdAndUserIdAndIsEnable(fridgeId, userId, true) + .orElseThrow(() -> new BaseException(NO_PERMISSION)); + + List fridgeUsers = fridgeUserRepository.findByFridgeIdAndIsEnable(fridgeId, true); + return fridgeUsers.stream().map(FridgeUserRes::toDto).collect(Collectors.toList()); } @Override @@ -328,21 +302,24 @@ private FridgeFoodsStatistics toFoodStatisticsByDeleteStatus(Map new BaseException(NOT_FOUND_USER)); - return SelectFridgesMainRes.toDto(fridgeUserRepository.findByUserAndIsEnable(user, true)); - } - - public GetFridgesMainRes myFridge(Long userId) { - User user = userRepository.findByIdAndIsEnable(userId, true).orElseThrow(() -> new BaseException(NOT_FOUND_USER)); + // 내 냉장고 조회 + public MyFridgeRes getMyFridge(Long userId) { + userRepository.findByIdAndIsEnable(userId, true).orElseThrow(() -> new BaseException(NOT_FOUND_USER)); - // 가정용 냉장고 조회 - List fridgeUsers = fridgeUserRepository.findByUserAndIsEnable(user, true); - List fridges = fridgeUsers.stream().map(m -> fridgeRepository.findByIdAndIsEnable(m.getFridge().getId(), true).orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE))).collect(Collectors.toList()); - List> fridgeUserListList = fridges.stream().map(m -> fridgeUserRepository.findByFridgeAndIsEnableOrderByRoleDesc(m, true)).collect(Collectors.toList()); + FridgeUser fridgeUser = fridgeUserRepository.findByUserIdAndIsEnable(userId, true).orElse(null); + if (fridgeUser == null) return null; + return MyFridgeRes.toDto(fridgeUser); + } - return GetFridgesMainRes.toDto(fridgeUserListList, userId); + // 냉장고 정보 조회 + public FridgeInfoRes getFridgeInfo(Long userId, Long fridgeId) { + fridgeUserRepository.findByFridgeIdAndUserIdAndIsEnable(fridgeId, userId, true) + .orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE_USER)); + Fridge fridge = fridgeRepository.findByIdAndIsEnable(fridgeId, true) + .orElseThrow(() -> new BaseException(NOT_FOUND_FRIDGE)); + List fridgeUser = fridgeUserRepository.findByFridgeAndIsEnable(fridge, true); + return FridgeInfoRes.toDto(fridge, fridgeUser); } // 사용자가 속한 가정용/공용 냉장고 food list diff --git a/src/main/java/com/example/icebutler_server/global/util/FridgeUtils.java b/src/main/java/com/example/icebutler_server/global/util/FridgeUtils.java index 6e518690..9aa7731c 100644 --- a/src/main/java/com/example/icebutler_server/global/util/FridgeUtils.java +++ b/src/main/java/com/example/icebutler_server/global/util/FridgeUtils.java @@ -4,8 +4,8 @@ import java.time.temporal.ChronoUnit; public abstract class FridgeUtils { - public static int calShelfLife(LocalDate shelfLife) { - return (int) (-1 * ChronoUnit.DAYS.between(LocalDate.now(), shelfLife)); + public static int calShelfLife(LocalDate expirationDate) { + return (int) (-1 * ChronoUnit.DAYS.between(LocalDate.now(), expirationDate)); } public static double calPercentage(int val, int sum) {