Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#334 냉장고 관련 API 클린코드 및 동적쿼리 적용 #347

Merged
merged 11 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -49,7 +51,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,83 +72,64 @@ 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 = "냉장고 사용자를 삭제한다.")
@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<Long> 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<FridgeMainRes> 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<List<FridgeFoodsRes>> 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<Page<FridgeFoodsRes>> 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 = "냉장고 내 식품을 상세 조회한다.")
@SwaggerApiSuccess(implementation = FridgeFoodsRes.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" +
"(R0002)해당 냉장고에 존재하지 않는 식품입니다.",
@ApiResponse(responseCode = "404", description = "(R0002)해당 냉장고에 존재하지 않는 식품입니다.",
content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
})
@Auth
Expand Down Expand Up @@ -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<FridgeUserMainRes> getMembers(
@GetMapping("/{fridgeId}/members")
public ResponseCustom<List<FridgeUserRes>> 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<SelectFridgesMainRes> selectFridges(
@GetMapping
public ResponseCustom<MyFridgeRes> 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<GetFridgesMainRes> myFridge(@Parameter(hidden = true) @IsLogin Long userId) {
return ResponseCustom.success(fridgeService.myFridge(userId));
@GetMapping("/{fridgeId}")
public ResponseCustom<FridgeInfoRes> 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))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "식품 메모")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Comment on lines +29 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변수명 바꾼 거 직관적이라 넘 좋으네요 마음이 편안 😌

@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) {
Expand All @@ -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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Loading
Loading