Skip to content

Commit

Permalink
♻️ Feature/87 category 와 recipe category table 분리
Browse files Browse the repository at this point in the history
레시피 등록, 상세조회 등에도 영향 있음
  • Loading branch information
Hanvp committed Sep 1, 2023
1 parent cdc9ea1 commit 4b161d5
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 17 deletions.
39 changes: 29 additions & 10 deletions src/main/java/zipdabang/server/converter/RecipeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import org.springframework.web.multipart.MultipartFile;
//import zipdabang.server.aws.s3.AmazonS3Manager;
import zipdabang.server.aws.s3.AmazonS3Manager;
import zipdabang.server.domain.Category;
import zipdabang.server.domain.etc.Uuid;
import zipdabang.server.domain.member.Member;
import zipdabang.server.domain.recipe.*;
import zipdabang.server.repository.CategoryRepository;
import zipdabang.server.repository.recipeRepositories.LikesRepository;
import zipdabang.server.repository.recipeRepositories.RecipeCategoryMappingRepository;
import zipdabang.server.repository.recipeRepositories.RecipeRepository;
import zipdabang.server.repository.recipeRepositories.ScrapRepository;
import zipdabang.server.repository.recipeRepositories.*;
import zipdabang.server.web.dto.requestDto.RecipeRequestDto;
import zipdabang.server.web.dto.responseDto.RecipeResponseDto;
import zipdabang.server.web.dto.responseDto.RootResponseDto;

import javax.annotation.PostConstruct;
import java.io.IOException;
Expand All @@ -32,7 +31,8 @@ public class RecipeConverter {
private final RecipeCategoryMappingRepository recipeCategoryMappingRepository;
private final LikesRepository likesRepository;
private final ScrapRepository scrapRepository;
private final CategoryRepository categoryRepository;
// private final CategoryRepository categoryRepository;
private final RecipeCategoryRepository recipeCategoryRepository;
private final AmazonS3Manager amazonS3Manager;

private static RecipeRepository staticRecipeRepository;
Expand All @@ -41,15 +41,17 @@ public class RecipeConverter {
private static LikesRepository staticLikesRepository;
private static ScrapRepository staticScrapRepository;

private static CategoryRepository staticCategoryRepository;
// private static CategoryRepository staticCategoryRepository;
private static RecipeCategoryRepository staticRecipeCategoryRepository;
private static AmazonS3Manager staticAmazonS3Manager;


@PostConstruct
public void init() {
this.staticRecipeRepository = this.recipeRepository;
this.staticRecipeCategoryMappingRepository = this.recipeCategoryMappingRepository;
this.staticCategoryRepository = this.categoryRepository;
// this.staticCategoryRepository = this.categoryRepository;
this.staticRecipeCategoryRepository = this.recipeCategoryRepository;
this.staticAmazonS3Manager = this.amazonS3Manager;
this.staticLikesRepository = this.likesRepository;
this.staticScrapRepository = this.scrapRepository;
Expand Down Expand Up @@ -105,9 +107,9 @@ public static List<Step> toStep(RecipeRequestDto.CreateRecipeDto request, Recipe
.collect(Collectors.toList());
}

public static List<RecipeCategoryMapping> toCategory(RecipeRequestDto.CreateRecipeDto request, Recipe recipe) {
public static List<RecipeCategoryMapping> toRecipeCategory(RecipeRequestDto.CreateRecipeDto request, Recipe recipe) {
return request.getCategoryId().stream()
.map(categoryId -> toRecipeCategoryMappingDto(categoryId, recipe))
.map(recipeCategoryId -> toRecipeCategoryMappingDto(recipeCategoryId, recipe))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -211,7 +213,7 @@ private static String uploadThumbnail(MultipartFile thumbnail) throws IOExceptio

private static RecipeCategoryMapping toRecipeCategoryMappingDto(Long categoryId, Recipe recipe) {
return RecipeCategoryMapping.builder()
.category(staticCategoryRepository.findById(categoryId).get())
.category(staticRecipeCategoryRepository.findById(categoryId).get())
.recipe(recipe)
.build();
}
Expand Down Expand Up @@ -271,4 +273,21 @@ public static Scrap toScrap(Recipe recipe, Member member) {
.member(member)
.build();
}
public static RecipeResponseDto.RecipeCategoryListDto RecipeCategoryListDto(List<RecipeCategory> categoryList) {
List<RecipeResponseDto.RecipeCategoryDto> recipeCategoryDtoList = categoryList.stream()
.map(category -> toRecipeCategoryDto(category)).collect(Collectors.toList());

return RecipeResponseDto.RecipeCategoryListDto.builder()
.beverageCategoryList(recipeCategoryDtoList)
.size(recipeCategoryDtoList.size())
.build();
}
public static RecipeResponseDto.RecipeCategoryDto toRecipeCategoryDto (RecipeCategory category){
return RecipeResponseDto.RecipeCategoryDto.builder()
.categoryName(category.getName())
.imageUrl(category.getImageUrl())
.id(category.getId())
.build();
}

}
28 changes: 28 additions & 0 deletions src/main/java/zipdabang/server/domain/recipe/RecipeCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package zipdabang.server.domain.recipe;

import lombok.*;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DynamicInsert
@DynamicUpdate
@Entity
public class RecipeCategory {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private String imageUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class RecipeCategoryMapping {
private Recipe recipe;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;
@JoinColumn(name = "recipe_category_id", nullable = false)
private RecipeCategory category;

public RecipeCategoryMapping setRecipe(Recipe recipe){
if(this.recipe != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package zipdabang.server.repository.recipeRepositories;

import org.springframework.data.jpa.repository.JpaRepository;
import zipdabang.server.domain.recipe.RecipeCategory;

public interface RecipeCategoryRepository extends JpaRepository<RecipeCategory,Long> {
}
5 changes: 5 additions & 0 deletions src/main/java/zipdabang/server/service/RecipeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.web.multipart.MultipartFile;
import zipdabang.server.domain.member.Member;
import zipdabang.server.domain.recipe.Recipe;
import zipdabang.server.domain.recipe.RecipeCategory;
import zipdabang.server.web.dto.requestDto.RecipeRequestDto;

import java.io.IOException;
Expand All @@ -28,4 +29,8 @@ public interface RecipeService {
Recipe updateLikeOnRecipe(Long recipeId, Member member);

Recipe updateScrapOnRecipe(Long recipeId, Member member);

Page<Recipe> recipeListByCategory(Long categoryId, Integer pageIndex, Member member);

List<RecipeCategory> getAllRecipeCategories();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import zipdabang.server.domain.member.Member;
import zipdabang.server.domain.recipe.Likes;
import zipdabang.server.domain.recipe.Recipe;
import zipdabang.server.domain.recipe.RecipeCategory;
import zipdabang.server.domain.recipe.Scrap;
import zipdabang.server.repository.memberRepositories.BlockedMemberRepository;
import zipdabang.server.repository.recipeRepositories.*;
Expand All @@ -38,6 +39,7 @@ public class RecipeServiceImpl implements RecipeService {

private final RecipeRepository recipeRepository;
private final RecipeCategoryMappingRepository recipeCategoryMappingRepository;
private final RecipeCategoryRepository recipeCategoryRepository;
private final StepRepository stepRepository;
private final IngredientRepository ingredientRepository;
private final LikesRepository likesRepository;
Expand All @@ -57,7 +59,7 @@ public Recipe create(RecipeRequestDto.CreateRecipeDto request, MultipartFile thu
Recipe buildRecipe = RecipeConverter.toRecipe(request, thumbnail, member);
Recipe recipe = recipeRepository.save(buildRecipe);

RecipeConverter.toCategory(request,recipe).stream()
RecipeConverter.toRecipeCategory(request,recipe).stream()
.map(categoryMapping -> recipeCategoryMappingRepository.save(categoryMapping))
.collect(Collectors.toList())
.stream()
Expand Down Expand Up @@ -208,4 +210,15 @@ public Recipe updateScrapOnRecipe(Long recipeId, Member member) {

return recipe;
}

@Override
public List<RecipeCategory> getAllRecipeCategories() {
return recipeCategoryRepository.findAll();
}

@Override
public Page<Recipe> recipeListByCategory(Long categoryId, Integer pageIndex, Member member) {

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import zipdabang.server.base.ResponseDto;
import zipdabang.server.base.exception.handler.RecipeException;
import zipdabang.server.converter.RecipeConverter;
import zipdabang.server.converter.RootConverter;
import zipdabang.server.domain.Category;
import zipdabang.server.domain.member.Member;
import zipdabang.server.domain.recipe.Likes;
import zipdabang.server.domain.recipe.Recipe;
import zipdabang.server.domain.recipe.RecipeCategory;
import zipdabang.server.service.RecipeService;
import zipdabang.server.web.dto.requestDto.RecipeRequestDto;
import zipdabang.server.web.dto.responseDto.RecipeResponseDto;
Expand Down Expand Up @@ -171,6 +174,24 @@ else if (pageIndex < 1)
})
@GetMapping(value = "/members/recipes/categories/{categoryId}")
public ResponseDto<RecipeResponseDto.RecipePageListDto> recipeListByCategory(@PathVariable Long categoryId, @RequestParam(name = "order") String order, @RequestParam(name = "pageIndex", required = false) Integer pageIndex, @AuthMember Member member){
if(pageIndex == null)
pageIndex =1;
else if (pageIndex < 1)
throw new RecipeException(Code.UNDER_PAGE_INDEX_ERROR);

pageIndex -= 1;

// Page<Recipe> recipes= recipeService.recipeListByCategory(categoryId,pageIndex,member);
//
// log.info(recipes.toString());
//
// if(recipes.getTotalElements() == 0)
// throw new RecipeException(Code.RECIPE_NOT_FOUND);
// if(pageIndex >= recipes.getTotalPages())
// throw new RecipeException(Code.OVER_PAGE_INDEX_ERROR);
//
// return ResponseDto.of(RecipeConverter.toPagingRecipeDtoList(recipes, member));

return null;
}

Expand Down Expand Up @@ -292,4 +313,19 @@ public ResponseDto<RecipeResponseDto.RecipeStatusDto> recipeLikeOrCancel(@PathVa
public ResponseDto<RecipeResponseDto.RecipeBannerImageDto> showBanners() {
return null;
}

@Operation(summary = "레시피 카테고리 조회 API 🔑 ✔️", description = "레시피 카테고리 조회 API입니다.")
@ApiResponses({
@ApiResponse(responseCode = "2000",description = "OK 성공"),
@ApiResponse(responseCode = "4003",description = "UNAUTHORIZED, 토큰 모양이 이상함, 토큰 제대로 주세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))),
@ApiResponse(responseCode = "4005",description = "UNAUTHORIZED, 엑세스 토큰 만료, 리프레시 토큰 사용",content = @Content(schema = @Schema(implementation = ResponseDto.class))),
@ApiResponse(responseCode = "4008",description = "UNAUTHORIZED, 토큰 없음, 토큰 줘요",content = @Content(schema = @Schema(implementation = ResponseDto.class))),
@ApiResponse(responseCode = "4052",description = "BAD_REQUEST, 사용자가 없습니다. 이 api에서 이거 생기면 백앤드 개발자 호출",content = @Content(schema = @Schema(implementation = ResponseDto.class))),
@ApiResponse(responseCode = "5000",description = "SERVER ERROR, 백앤드 개발자에게 알려주세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))),
})
@GetMapping("/members/recipes/categories")
public ResponseDto<RecipeResponseDto.RecipeCategoryListDto> showCategoryList(){
List<RecipeCategory> allCategories = recipeService.getAllRecipeCategories();
return ResponseDto.of(RecipeConverter.RecipeCategoryListDto(allCategories));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,23 @@ public static class RecipeBannerImageDto {
List<RecipeBannerDto> bannerList;
Integer size;
}

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class RecipeCategoryDto{
private Long id;
private String categoryName;
private String imageUrl;
}

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class RecipeCategoryListDto{
List<RecipeCategoryDto> beverageCategoryList;
Integer size;
}
}
8 changes: 4 additions & 4 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ spring:
enabled: always

## # local redis
# redis:
# host: localhost

redis:
host: zipdabang-redis.osattk.ng.0001.apn2.cache.amazonaws.com
host: localhost

# redis:
# host: zipdabang-redis.osattk.ng.0001.apn2.cache.amazonaws.com
batch:
jdbc:
initialize-schema: always
Expand Down

0 comments on commit 4b161d5

Please sign in to comment.