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

✨ ♻️ Feature/111 댓글 추가 및 조회, 카테고리 조회 로직 수정 #113

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 33 additions & 1 deletion src/main/java/zipdabang/server/converter/RecipeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class RecipeConverter {
// private final CategoryRepository categoryRepository;
private final RecipeCategoryRepository recipeCategoryRepository;
private final RecipeBannerRepository recipeBannerRepository;
private final CommentRepository commentRepository;
private final AmazonS3Manager amazonS3Manager;

private static RecipeRepository staticRecipeRepository;
Expand All @@ -46,10 +47,10 @@ public class RecipeConverter {
// private static CategoryRepository staticCategoryRepository;
private static RecipeCategoryRepository staticRecipeCategoryRepository;
private static RecipeBannerRepository staticRecipeBannerRepository;
private static CommentRepository staticCommentRepository;
private static AmazonS3Manager staticAmazonS3Manager;



@PostConstruct
public void init() {
this.staticRecipeRepository = this.recipeRepository;
Expand Down Expand Up @@ -344,4 +345,35 @@ public static String toKeyName(String imageUrl) {
return extractedString;

}

public static Comment toComment(String content, Recipe findRecipe, Member member) {
return Comment.builder()
.content(content)
.recipe(findRecipe)
.member(member)
.build();
}

public static RecipeResponseDto.CommentDto toCommentDto(Comment createdComment, Member member) {
return RecipeResponseDto.CommentDto.builder()
.content(createdComment.getContent())
.ownerNickname(createdComment.getMember().getNickname())
.ownerImage(createdComment.getMember().getProfileUrl())
.isOwner(createdComment.getMember() == member)
.createdAt(createdComment.getCreatedAt().toLocalDate())
.build();
}

public static RecipeResponseDto.CommentPageListDto toPagingCommentDtoList(Page<Comment> comments, Member member) {
return RecipeResponseDto.CommentPageListDto.builder()
.CommentList(comments.stream()
.map(comment -> toCommentDto(comment,member))
.collect(Collectors.toList()))
.totalElements(comments.getTotalElements())
.currentPageElements(comments.getNumberOfElements())
.totalPage(comments.getTotalPages())
.isFirst(comments.isFirst())
.isLast(comments.isLast())
.build();
}
}
33 changes: 33 additions & 0 deletions src/main/java/zipdabang/server/domain/recipe/BlockedComment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package zipdabang.server.domain.recipe;

import lombok.*;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import zipdabang.server.domain.member.Member;

import javax.persistence.*;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DynamicInsert
@DynamicUpdate
@Entity
public class BlockedComment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "owner_id")
private Member owner;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blocked_id")
private Comment blocked;

public Comment getBlocked(){
return this.blocked;
}
}
33 changes: 33 additions & 0 deletions src/main/java/zipdabang/server/domain/recipe/BlockedRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package zipdabang.server.domain.recipe;

import lombok.*;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import zipdabang.server.domain.member.Member;

import javax.persistence.*;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DynamicInsert
@DynamicUpdate
@Entity
public class BlockedRecipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "owner_id")
private Member owner;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blocked_id")
private Recipe blocked;

public Recipe getBlocked(){
return this.blocked;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package zipdabang.server.repository.recipeRepositories;

import org.springframework.data.jpa.repository.JpaRepository;
import zipdabang.server.domain.member.Member;
import zipdabang.server.domain.recipe.BlockedComment;

import java.util.List;

public interface BlockedCommentRepository extends JpaRepository<BlockedComment,Long> {
List<BlockedComment> findByOwner(Member member);
}
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.BlockedComment;

public interface BlockedRecipeRepository extends JpaRepository<BlockedComment,Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package zipdabang.server.repository.recipeRepositories;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import zipdabang.server.domain.member.Member;
import zipdabang.server.domain.recipe.Comment;
import zipdabang.server.domain.recipe.Recipe;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {

Page<Comment> findByMemberNotIn(List<Member> blockedMember, PageRequest createdAt);

Page<Comment> findByIdNotIn(List<Long> blockedComment, PageRequest createdAt);

Page<Comment> findByIdNotInAndMemberNotIn(List<Long> blockedComment, List<Member> blockedMember, PageRequest createdAt);
}
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 @@ -3,6 +3,7 @@
import org.springframework.data.domain.Page;
import org.springframework.web.multipart.MultipartFile;
import zipdabang.server.domain.member.Member;
import zipdabang.server.domain.recipe.Comment;
import zipdabang.server.domain.recipe.Recipe;
import zipdabang.server.domain.recipe.RecipeBanner;
import zipdabang.server.domain.recipe.RecipeCategory;
Expand Down Expand Up @@ -43,4 +44,8 @@ public interface RecipeService {
boolean checkRecipeCategoryExist(Long categoryId);

Boolean deleteRecipe(Long recipeId, Member member);

Comment createComment(String content, Long recipeId, Member member);

Page<Comment> commentList(Integer pageIndex, Long recipeId, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public class RecipeServiceImpl implements RecipeService {
private final LikesRepository likesRepository;
private final ScrapRepository scrapRepository;
private final AmazonS3Manager amazonS3Manager;
private final AmazonConfig amazonConfig;

private final BlockedMemberRepository blockedMemberRepository;
private final CommentRepository commentRepository;
private final BlockedCommentRepository blockedCommentRepository;

@Value("${paging.size}")
Integer pageSize;
Expand Down Expand Up @@ -120,9 +121,7 @@ public Boolean getScrap(Recipe recipe, Member member) {
@Override
public Page<Recipe> searchRecipe(Long categoryId, String keyword, Integer pageIndex, Member member) {

List<Member> blockedMember= blockedMemberRepository.findByOwner(member).stream()
.map(blockedInfo -> blockedInfo.getBlocked())
.collect(Collectors.toList());
List<Member> blockedMember = getBlockedMembers(member);

List<RecipeCategory> recipeCategory = recipeCategoryRepository.findAllById(categoryId);

Expand All @@ -146,9 +145,7 @@ public Page<Recipe> searchRecipe(Long categoryId, String keyword, Integer pageIn
public List<Recipe> getWrittenByRecipePreview(String writtenby, Member member) {
List<Recipe> recipeList = new ArrayList<>();

List<Member> blockedMember= blockedMemberRepository.findByOwner(member).stream()
.map(blockedInfo -> blockedInfo.getBlocked())
.collect(Collectors.toList());
List<Member> blockedMember = getBlockedMembers(member);

if (!blockedMember.isEmpty()) {
if (writtenby.equals("all")) {
Expand Down Expand Up @@ -226,18 +223,25 @@ public List<RecipeCategory> getAllRecipeCategories() {
@Override
public Page<Recipe> recipeListByCategory(Long categoryId, Integer pageIndex, Member member, String order) {

List<Member> blockedMember= blockedMemberRepository.findByOwner(member).stream()
.map(blockedInfo -> blockedInfo.getBlocked())
.collect(Collectors.toList());
List<Member> blockedMember = getBlockedMembers(member);

List<RecipeCategory> recipeCategory = recipeCategoryRepository.findAllById(categoryId);
List<Long> recipeIdList = new ArrayList<>();

if(recipeCategory.isEmpty())
throw new RecipeException(Code.RECIPE_NOT_FOUND);
if (categoryId == 0){
recipeIdList = recipeRepository.findAll().stream()
.map(recipe -> recipe.getId())
.collect(Collectors.toList());
}
else{
List<RecipeCategory> recipeCategory = recipeCategoryRepository.findAllById(categoryId);

List<Long> recipeIdList = recipeCategoryMappingRepository.findByCategoryIn(recipeCategory).stream()
.map(categoryMapping -> categoryMapping.getRecipe().getId())
.collect(Collectors.toList());
if(recipeCategory.isEmpty())
throw new RecipeException(Code.RECIPE_NOT_FOUND);

recipeIdList = recipeCategoryMappingRepository.findByCategoryIn(recipeCategory).stream()
.map(categoryMapping -> categoryMapping.getRecipe().getId())
.collect(Collectors.toList());
}

String orderBy = null;

Expand Down Expand Up @@ -268,11 +272,9 @@ public boolean checkRecipeCategoryExist(Long categoryId) {

@Override
public List<List<Recipe>> searchRecipePreview(String keyword, Member member) {
Long recipeCategorySize = recipeCategoryRepository.count();
Long recipeCategorySize = recipeCategoryRepository.count()-1;

List<Member> blockedMember = blockedMemberRepository.findByOwner(member).stream()
.map(blockedInfo -> blockedInfo.getBlocked())
.collect(Collectors.toList());
List<Member> blockedMember = getBlockedMembers(member);

List<List<Recipe>> recipeList = new ArrayList<>();

Expand All @@ -292,6 +294,13 @@ public List<List<Recipe>> searchRecipePreview(String keyword, Member member) {
return recipeList;
}

private List<Member> getBlockedMembers(Member member) {
List<Member> blockedMember = blockedMemberRepository.findByOwner(member).stream()
.map(blockedInfo -> blockedInfo.getBlocked())
.collect(Collectors.toList());
return blockedMember;
}

public List<RecipeBanner> getRecipeBannerList() {
return recipeBannerRepository.findAll();
}
Expand All @@ -312,4 +321,40 @@ public Boolean deleteRecipe(Long recipeId, Member member) {

return recipeRepository.existsById(recipeId) == false;
}

@Transactional(readOnly = false)
@Override
public Comment createComment(String content, Long recipeId, Member member) {
Recipe findRecipe = recipeRepository.findById(recipeId).orElseThrow(() -> new RecipeException(Code.NO_RECIPE_EXIST));

Comment buildComment = RecipeConverter.toComment(content, findRecipe, member);
return commentRepository.save(buildComment);
}

@Override
public Page<Comment> commentList(Integer pageIndex, Long recipeId, Member member) {
recipeRepository.findById(recipeId).orElseThrow(() -> new RecipeException(Code.NO_RECIPE_EXIST));

List<Member> blockedMember = getBlockedMembers(member);
List<Long> blockedComment = getBlockedComment(member);

if(blockedMember.isEmpty() && blockedComment.isEmpty())
return commentRepository.findAll(
PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")));
else if(!blockedMember.isEmpty() && blockedComment.isEmpty())
return commentRepository.findByMemberNotIn(blockedMember, PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")));
else if(blockedMember.isEmpty() && !blockedComment.isEmpty())
return commentRepository.findByIdNotIn(blockedComment, PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")));
else
return commentRepository.findByIdNotInAndMemberNotIn(blockedComment, blockedMember, PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")));
}

private List<Long> getBlockedComment(Member member) {

List<Long> blockedCommentIdList = blockedCommentRepository.findByOwner(member).stream()
.map(blockedInfo -> blockedInfo.getBlocked().getId())
.collect(Collectors.toList());

return blockedCommentIdList;
}
}
Loading