Skip to content
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
8 changes: 6 additions & 2 deletions src/main/java/NextLevel/demo/follow/FollowRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public interface FollowRepository extends JpaRepository<FollowEntity, Long> {
@Query("select f from FollowEntity f where f.user.id = :userId and f.target.id = :targetId")
Optional<FollowEntity> findByUserIdAndTargetId(@Param("userId") Long userId, @Param("targetId") Long targetId);

@Query("select count(f.id) from FollowEntity f where f.target.id = :userId")
Long followCount(@Param("userId") Long userId);
@Query("select " +
"(select count(f1) from FollowEntity f1 where f1.target.id = :targetUserId) as followCount, " +
"(select count(f2) from FollowEntity f2 where f2.target.id = :targetUserId and f2.user.id = :userId) as isFollow " +
"from TagEntity t " +
"where t.id = 1 ")
SelectFollowCountAndIsFollowDao selectFollowCountAndFollowDao(@Param("targetUserId") Long targetUserId, @Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package NextLevel.demo.follow;

public interface SelectFollowCountAndIsFollowDao {
Long getIsFollow();
Long getFollowCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ public class SelectSocialProfileService {
private final FollowRepository followRepository;
private final UserValidateService validateService;

public UserSocialProfileDto selectUserSocialProfile(long userId) {
UserEntity user = validateService.findUserWithUserId(userId);
return selectUserSocialProfile(user);
public UserSocialProfileDto selectUserSocialProfile(long targetUserId, Long userId) {
UserEntity user = validateService.findUserWithUserId(targetUserId);
return selectUserSocialProfile(user, userId);
}

public UserSocialProfileDto selectUserSocialProfile(UserEntity user) {
return UserSocialProfileDto.of(user, followRepository.followCount(user.getId()));
public UserSocialProfileDto selectUserSocialProfile(UserEntity targetUser, Long userId) {
SelectFollowCountAndIsFollowDao dao = followRepository.selectFollowCountAndFollowDao(targetUser.getId(), userId);
return UserSocialProfileDto.of(targetUser, dao.getFollowCount(), dao.getIsFollow());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import NextLevel.demo.project.project.entity.ProjectEntity;

import java.time.LocalDate;
import java.util.List;

import NextLevel.demo.project.tag.entity.ProjectTagEntity;
import NextLevel.demo.project.tag.entity.TagEntity;
import NextLevel.demo.user.dto.user.response.UserSocialProfileDto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -24,13 +29,15 @@ public class ResponseProjectDetailDto {

private ImgDto titleImg;

private UserSocialProfileDto user;

private List<String> tag;

private LocalDate createdAt;
private LocalDate startAt;
private LocalDate expiredAt;
private String status;

private String authorNickName;

private Boolean isAuthor;

private Long goal;
Expand All @@ -39,27 +46,43 @@ public class ResponseProjectDetailDto {

private int likeCount;
private long fundingCount;
private Long userCount;
private Long viewCount; // 조회수

public static ResponseProjectDetailDto of(ProjectEntity entity, Long fundingPrice, Long fundingCount, Long userId) {
@JsonProperty("isLike")
private boolean isLike;

public static ResponseProjectDetailDto of(
ProjectEntity entity,
Long fundingPrice,
Long fundingCount,
Long userId,
Long likeCount,
Long isLike,
Long viewCount,
UserSocialProfileDto userSocialProfileDto
) {
ResponseProjectDetailDto dto = new ResponseProjectDetailDto();
dto.setId(entity.getId());
dto.setTitle(entity.getTitle());
dto.setContent(entity.getContent());
dto.setTitleImg(new ImgDto(entity.getTitleImg()));
dto.setCreatedAt(entity.getCreatedAt().toLocalDate());
dto.setAuthorNickName(entity.getUser().getNickName());
dto.setGoal(entity.getGoal());
dto.setSum(fundingPrice);
dto.setCompletionRate(FundingUtil.getCompletionRate(dto.sum, dto.goal));
dto.setLikeCount(entity.getLikes().size());
dto.setIsAuthor(entity.getUser().getId().equals(userId));
dto.id = entity.getId();
dto.title = entity.getTitle();
dto.content = entity.getContent();
dto.titleImg = new ImgDto(entity.getTitleImg());
dto.createdAt = entity.getCreatedAt().toLocalDate();
dto.goal = entity.getGoal();
dto.sum = fundingPrice;
dto.completionRate = FundingUtil.getCompletionRate(dto.sum, dto.goal);
dto.likeCount = likeCount!=null?likeCount.intValue():0;
dto.isAuthor = entity.getUser().getId().equals(userId);
dto.status = entity.getProjectStatus().name();
dto.startAt = entity.getStartAt();
dto.expiredAt = entity.getExpiredAt();
dto.setFundingCount(fundingCount); // 펀딩의 총 갯수

dto.setUserCount(null); // 조회한 조회 수, 아직 추가 예정
dto.viewCount = viewCount !=null ? viewCount : 0L; // 조회한 조회 수

dto.isLike = isLike!=null?isLike.equals(1L):false;
dto.user = userSocialProfileDto;

dto.tag = entity.getTags().stream().map(ProjectTagEntity::getTag).map(TagEntity::getName).toList();

return dto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ public interface ProjectRepository extends JpaRepository<ProjectEntity, Long> {
+ "where p.id = :id")
Optional<ProjectEntity> findProjectDetailById(@Param("id") Long id);

@Query("select count(my_like) as isLike, count(total_like) as likeCount, count(view) as viewCount " +
"from ProjectEntity p " +
"left join LikeEntity my_like on my_like.project.id = p.id and my_like.user.id = :userId " +
"left join LikeEntity total_like on total_like.project.id = p.id " +
"left join ProjectViewEntity view on view.project.id = p.id " +
"where p.id = :projectId " +
"group by p ")
SelectProjectDetailDao selectProjectDetailDao(@Param("projectId") Long projectId, @Param("userId") Long userId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package NextLevel.demo.project.project.repository;

public interface SelectProjectDetailDao {
Long getIsLike();
Long getLikeCount();
Long getViewCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import NextLevel.demo.exception.CustomException;
import NextLevel.demo.exception.ErrorCode;
import NextLevel.demo.follow.SelectSocialProfileService;
import NextLevel.demo.funding.service.FundingRollbackService;
import NextLevel.demo.funding.service.FundingValidateService;
import NextLevel.demo.img.entity.ImgEntity;
Expand All @@ -14,9 +15,11 @@
import NextLevel.demo.project.project.entity.ProjectEntity;
import NextLevel.demo.project.project.repository.ProjectDslRepository;
import NextLevel.demo.project.project.repository.ProjectRepository;
import NextLevel.demo.project.project.repository.SelectProjectDetailDao;
import NextLevel.demo.project.story.service.ProjectStoryService;
import NextLevel.demo.project.tag.service.TagService;
import NextLevel.demo.project.view.ProjectViewService;
import NextLevel.demo.user.dto.user.response.UserSocialProfileDto;
import NextLevel.demo.user.entity.UserEntity;
import NextLevel.demo.user.service.UserValidateService;

Expand Down Expand Up @@ -46,6 +49,7 @@ public class ProjectService {
private final ProjectDslRepository projectDslRepository;
private final FundingRollbackService fundingRollbackService;
private final ProjectValidateService projectValidateService;
private final SelectSocialProfileService selectSocialProfileService;

// 추가
@ImgTransaction
Expand Down Expand Up @@ -132,8 +136,13 @@ public ResponseProjectDetailDto getProjectDetailById(Long id, Long userId) {
projectViewService.save(project, userId);
Long fundingPrice = fundingValidateService.getTotalFundingPrice(project.getId());
Long fundingCount = fundingValidateService.getTotalFundingCount(project.getId());
UserSocialProfileDto userSocialProfileDto = selectSocialProfileService.selectUserSocialProfile(project.getUser(), userId);
SelectProjectDetailDao dao = projectRepository.selectProjectDetailDao(project.getId(), userId);

return ResponseProjectDetailDto.of(project, fundingPrice, fundingCount, userId);
return ResponseProjectDetailDto.of(
project, fundingPrice, fundingCount, userId,
dao.getLikeCount(), dao.getIsLike(), dao.getViewCount(),
userSocialProfileDto);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public SocialListDto list(Long targetUserId, Long userId) {
isSocialLikedMap.get(entity.getId()).getMyLikeCount()
)).toList();

UserSocialProfileDto user = selectSocialProfileService.selectUserSocialProfile(targetUser);
UserSocialProfileDto user = selectSocialProfileService.selectUserSocialProfile(targetUser, userId);
return SocialListDto.of(user, socials);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import NextLevel.demo.img.ImgDto;
import NextLevel.demo.user.entity.UserEntity;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -16,13 +17,16 @@ public class UserSocialProfileDto {
private String nickName;
private Long followCount;
private ImgDto img;
@JsonProperty("isFollow")
private boolean isFollow;

public static UserSocialProfileDto of(UserEntity user, Long followCount) {
public static UserSocialProfileDto of(UserEntity user, Long followCount, Long isFollow) {
UserSocialProfileDto dto = new UserSocialProfileDto();
dto.followCount = followCount;
dto.name = user.getName();
dto.nickName = user.getNickName();
dto.img = new ImgDto(user.getImg());
dto.isFollow = isFollow!=null?isFollow.equals(1L):false;
return dto;
}

Expand Down
Loading