Skip to content

Commit

Permalink
Merge pull request #139 from devridge-team-project/main
Browse files Browse the repository at this point in the history
Release v0.0.5
  • Loading branch information
yu-heejin authored Apr 18, 2024
2 parents 342a3f9 + be73664 commit aca1ed3
Show file tree
Hide file tree
Showing 96 changed files with 2,774 additions and 210 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package org.devridge.api.application.community.freeboard;

import java.util.List;
import lombok.RequiredArgsConstructor;

import org.devridge.api.common.exception.common.DataNotFoundException;
import org.devridge.api.common.util.SecurityContextHolderUtil;
import org.devridge.api.domain.community.dto.request.CommunityCommentRequest;
import org.devridge.api.domain.community.dto.response.CommunityCommentResponse;
import org.devridge.api.domain.community.entity.Community;
import org.devridge.api.domain.community.entity.CommunityComment;
import org.devridge.api.domain.community.exception.MyCommunityForbiddenException;
import org.devridge.api.domain.member.entity.Member;
import org.devridge.api.infrastructure.community.freeboard.CommunityCommentQuerydslReopsitory;
import org.devridge.api.infrastructure.community.freeboard.CommunityCommentRepository;
import org.devridge.api.infrastructure.community.freeboard.CommunityRepository;
import org.devridge.api.domain.member.entity.Member;
import org.devridge.api.infrastructure.member.MemberRepository;
import org.devridge.api.common.exception.common.DataNotFoundException;
import org.devridge.api.common.util.SecurityContextHolderUtil;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
Expand All @@ -39,7 +37,7 @@ public Long createComment(Long communityId, CommunityCommentRequest commentReque
return communityCommentRepository.save(communityComment).getId();
}

public Slice<CommunityCommentResponse> getAllCommunityComment(Long communityId, Long lastId, Pageable pageable) {
public List<CommunityCommentResponse> getAllCommunityComment(Long communityId, Long lastId, Pageable pageable) {
return communityCommentQuerydslReopsitory.searchBySlice(communityId, lastId, pageable);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package org.devridge.api.application.community.freeboard;

import static org.devridge.api.common.util.MemberUtil.toMember;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.devridge.api.common.dto.UserInformation;
import org.devridge.api.domain.community.dto.request.CreateCommunityRequest;
import org.devridge.api.domain.community.dto.response.CommunityDetailResponse;
import org.devridge.api.domain.community.dto.response.CommunityListResponse;
import org.devridge.api.domain.community.entity.Community;
import org.devridge.api.domain.member.entity.Member;

import org.springframework.stereotype.Component;

import static org.devridge.api.common.util.MemberUtil.toMember;

@Component
public class CommunityMapper {

Expand All @@ -26,9 +24,9 @@ public CommunityDetailResponse toCommunityDetailResponse(Community community) {
.userInformation(memberInfoResponse)
.title(community.getTitle())
.content(community.getContent())
.likeCount(community.getLikeCount())
.dislikeCount(community.getDislikeCount())
.viewCount(community.getViewCount() + 1)
.likes(community.getLikes())
.dislikes(community.getDislikes())
.views(community.getViews() + 1)
.createdAt(community.getCreatedAt())
.updatedAt(community.getUpdatedAt())
.hashtags(toHashtags(community))
Expand All @@ -47,9 +45,9 @@ public CommunityListResponse toCommunityListResponse(Community community) {
return CommunityListResponse.builder()
.id(community.getId())
.title(community.getTitle())
.commentCount(Long.valueOf(community.getComments().size()))
.viewCount(community.getViewCount())
.likeCount(community.getLikeCount())
.comments(Long.valueOf(community.getComments().size()))
.views(community.getViews())
.likes(community.getLikes())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;

import org.devridge.api.application.s3.S3Service;
import org.devridge.api.common.exception.common.DataNotFoundException;
import org.devridge.api.common.util.SecurityContextHolderUtil;
import org.devridge.api.domain.community.dto.request.CreateCommunityRequest;
import org.devridge.api.domain.community.dto.response.CommunityDetailResponse;
import org.devridge.api.domain.community.dto.response.CommunitySliceResponse;
Expand All @@ -19,19 +20,13 @@
import org.devridge.api.domain.community.entity.Hashtag;
import org.devridge.api.domain.community.entity.id.CommunityHashtagId;
import org.devridge.api.domain.community.exception.MyCommunityForbiddenException;
import org.devridge.api.domain.member.entity.Member;
import org.devridge.api.infrastructure.community.freeboard.CommunityHashtagRepository;
import org.devridge.api.infrastructure.community.freeboard.CommunityQuerydslRepository;
import org.devridge.api.infrastructure.community.freeboard.CommunityRepository;
import org.devridge.api.infrastructure.community.hashtag.HashtagRepository;
import org.devridge.api.domain.member.entity.Member;
import org.devridge.api.infrastructure.member.MemberRepository;
import org.devridge.api.application.s3.S3Service;
import org.devridge.api.common.exception.common.DataNotFoundException;
import org.devridge.api.common.util.SecurityContextHolderUtil;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -94,25 +89,11 @@ public void deleteCommunity(Long communityId) {
}
}

public Slice<CommunitySliceResponse> getAllCommunity(Long lastId, Pageable pageable) {
public List<CommunitySliceResponse> getAllCommunity(Long lastId, Pageable pageable) {
List<CommunitySliceResponse> communitySliceResponses = communityQuerydslRepository.searchByCommunity(lastId, pageable);
List<Long> communityIds = toCommunityIds(communitySliceResponses);
List<CommunityHashtag> communityHashtags = communityQuerydslRepository.findCommunityHashtagsInCommunityIds(communityIds);
List<CommunitySliceResponse> groupedByCommunitySliceResponses = groupByCommunityId(communityHashtags, communitySliceResponses);
return checkLastPage(pageable, groupedByCommunitySliceResponses);
}

private Slice<CommunitySliceResponse> checkLastPage(Pageable pageable, List<CommunitySliceResponse> results) {

boolean hasNext = false;

// 조회한 결과 개수가 요청한 페이지 사이즈보다 크면 뒤에 더 있음, next = true
if (results.size() > pageable.getPageSize()) {
hasNext = true;
results.remove(pageable.getPageSize());
}

return new SliceImpl<>(results, pageable, hasNext);
return groupByCommunityId(communityHashtags, communitySliceResponses);
}

private List<CommunitySliceResponse> groupByCommunityId(List<CommunityHashtag> communityHashtags, List<CommunitySliceResponse> communitySliceResponses) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.devridge.api.application.community.project;

import org.devridge.api.domain.community.entity.LikeStatus;
import org.devridge.api.domain.community.entity.ProjectComment;
import org.devridge.api.domain.community.entity.ProjectCommentLikeDislike;
import org.devridge.api.domain.member.entity.Member;
import org.springframework.stereotype.Component;

@Component
public class ProjectCommentLikeDislikeMapper {

public ProjectCommentLikeDislike toProjectCommentLikeDislike(
Member member,
ProjectComment comment,
LikeStatus status
) {
return ProjectCommentLikeDislike.builder()
.projectComment(comment)
.member(member)
.status(status)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.devridge.api.application.community.project;

import javax.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.devridge.api.common.exception.common.DataNotFoundException;
import org.devridge.api.common.util.SecurityContextHolderUtil;
import org.devridge.api.domain.community.entity.LikeStatus;
import org.devridge.api.domain.community.entity.Project;
import org.devridge.api.domain.community.entity.ProjectComment;
import org.devridge.api.domain.community.entity.ProjectCommentLikeDislike;
import org.devridge.api.domain.community.entity.id.ProjectCommentLikeDislikeId;
import org.devridge.api.domain.community.exception.MyCommunityForbiddenException;
import org.devridge.api.domain.member.entity.Member;
import org.devridge.api.infrastructure.community.project.ProjectCommentLikeDislikeRepository;
import org.devridge.api.infrastructure.community.project.ProjectCommentRepository;
import org.devridge.api.infrastructure.community.project.ProjectRepository;
import org.devridge.api.infrastructure.member.MemberRepository;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class ProjectCommentLikeDislikeService {

private final ProjectCommentLikeDislikeRepository projectCommentLikeDislikeRepository;
private final ProjectCommentRepository projectCommentRepository;
private final MemberRepository memberRepository;
private final ProjectCommentLikeDislikeMapper projectCommentLikeDislikeMapper;
private final ProjectRepository projectRepository;

@Transactional
public void createProjectCommentLike(Long projectId, Long commentId) {
Long accessMemberId = SecurityContextHolderUtil.getMemberId();
Member member = getMemberById(accessMemberId);
Project project = getProjectById(projectId);
ProjectComment comment = getCommentById(commentId);
ProjectCommentLikeDislikeId projectCommentLikeDislikeId =
new ProjectCommentLikeDislikeId(member.getId(), comment.getId());

if (accessMemberId.equals(comment.getMember().getId())) {
throw new MyCommunityForbiddenException(403, "내가 작성한 글은 추천할 수 없습니다.");
}

projectCommentLikeDislikeRepository.findById(projectCommentLikeDislikeId).ifPresentOrElse(
ProjectCommentLikeDislike -> {
LikeStatus status = ProjectCommentLikeDislike.getStatus();

if (status == LikeStatus.G) {
changeIsDeletedStatus(ProjectCommentLikeDislike);
}

if (status == LikeStatus.B) {
if (ProjectCommentLikeDislike.getIsDeleted()) {
projectCommentLikeDislikeRepository.restoreById(projectCommentLikeDislikeId);
}
projectCommentLikeDislikeRepository.updateLikeDislike(projectCommentLikeDislikeId,
LikeStatus.G);
}
},
() -> {
ProjectCommentLikeDislike commentLikeDislike =
projectCommentLikeDislikeMapper.toProjectCommentLikeDislike(member, comment, LikeStatus.G);
projectCommentLikeDislikeRepository.save(commentLikeDislike);
}
);
updateLikeDislike(projectCommentLikeDislikeId.getCommentId());
}

@Transactional
public void createProjectCommentDislike(Long projectId, Long commentId) {
Long accessMemberId = SecurityContextHolderUtil.getMemberId();
Member member = getMemberById(accessMemberId);
Project project = getProjectById(projectId);
ProjectComment comment = getCommentById(commentId);
ProjectCommentLikeDislikeId projectCommentLikeDislikeId =
new ProjectCommentLikeDislikeId(member.getId(), comment.getId());

if (accessMemberId.equals(comment.getMember().getId())) {
throw new MyCommunityForbiddenException(403, "내가 작성한 글은 비추천할 수 없습니다.");
}

projectCommentLikeDislikeRepository.findById(projectCommentLikeDislikeId).ifPresentOrElse(
ProjectCommentLikeDislike -> {
LikeStatus status = ProjectCommentLikeDislike.getStatus();

if (status == LikeStatus.B) {
changeIsDeletedStatus(ProjectCommentLikeDislike);
}

if (status == LikeStatus.G) {
if (ProjectCommentLikeDislike.getIsDeleted()) {
projectCommentLikeDislikeRepository.restoreById(projectCommentLikeDislikeId);
}
projectCommentLikeDislikeRepository.updateLikeDislike(projectCommentLikeDislikeId, LikeStatus.B);
}
},
() -> {
ProjectCommentLikeDislike commentLikeDislike =
projectCommentLikeDislikeMapper.toProjectCommentLikeDislike(member, comment, LikeStatus.B);
projectCommentLikeDislikeRepository.save(commentLikeDislike);
}
);
updateLikeDislike(projectCommentLikeDislikeId.getCommentId());
}

private void updateLikeDislike(Long projectId) {
Long likes = Long.valueOf(projectCommentLikeDislikeRepository.countProjectLikeDislikeById(projectId, LikeStatus.G));
Long disLikes = Long.valueOf(projectCommentLikeDislikeRepository.countProjectLikeDislikeById(projectId, LikeStatus.B));
projectCommentRepository.updateLikeDislike(likes, disLikes, projectId);
}

private void changeIsDeletedStatus(ProjectCommentLikeDislike projectCommentLikeDislike) {
if (projectCommentLikeDislike.getIsDeleted()) {
projectCommentLikeDislikeRepository.restoreById(projectCommentLikeDislike.getId());
}
if (!projectCommentLikeDislike.getIsDeleted()) {
projectCommentLikeDislikeRepository.deleteById(projectCommentLikeDislike.getId());
}
}

private Member getMemberById(Long memberId) {
return memberRepository.findById(memberId).orElseThrow(() -> new DataNotFoundException());
}

private ProjectComment getCommentById(Long commentId) {
return projectCommentRepository.findById(commentId).orElseThrow(() -> new DataNotFoundException());
}

private Project getProjectById(Long projectId) {
return projectRepository.findById(projectId).orElseThrow(() -> new DataNotFoundException());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.devridge.api.application.community.project;

import org.devridge.api.domain.community.dto.request.ProjectCommentRequest;
import org.devridge.api.domain.community.entity.Project;
import org.devridge.api.domain.community.entity.ProjectComment;
import org.devridge.api.domain.member.entity.Member;
import org.springframework.stereotype.Component;

@Component
public class ProjectCommentMapper {

public ProjectComment toProjectComment(Project project, Member member, ProjectCommentRequest Request) {
return ProjectComment.builder()
.project(project)
.content(Request.getContent())
.member(member)
.build();
}

}
Loading

0 comments on commit aca1ed3

Please sign in to comment.