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

프로젝트 조회 시 응답 데이터 변경 #83

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public static void setLoginSessionUserId(long userId) {
* @author junsoo
*/
public static long getLoginSessionUserId() {
return Long.valueOf(StringUtils.getStringSafeFromObj(SessionUtils.getSession().getAttribute(LOGIN_USER_ID)));
String userId = (String) SessionUtils.getSession().getAttribute(LOGIN_USER_ID);

return Long.parseLong(userId == null ? "0" : userId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public class LikeCacheRepository {
private final RedisTemplate<String, Object> redisCacheTemplate;
private final StringRedisTemplate stringCacheRedisTemplate;

public Map<Long, Boolean> existLike(long userId, long id) {
Boolean exist = redisCacheTemplate.opsForSet().isMember(redisCacheService.generateKey(KeyPrefix.LIKE, id), userId);

Map<Long, Boolean> result = new HashMap<>();
result.put(id, exist);

return result;
}

public Map<Long, Boolean> existLike(long userId, List<Long> ids) {
RedisSerializer keySerializer = redisCacheTemplate.getKeySerializer();
RedisSerializer valueSerializer = redisCacheTemplate.getValueSerializer();
Expand All @@ -47,6 +56,15 @@ public Map<Long, Boolean> existLike(long userId, List<Long> ids) {
return existMap;
}

public Map<Long, Integer> findLikeCount(long id) {
String count = stringCacheRedisTemplate.opsForValue().get(redisCacheService.generateKey(KeyPrefix.LIKE_COUNT, id));

Map<Long, Integer> countMap = new HashMap<>();
countMap.put(id, count == null ? 0 : Integer.parseInt(count));

return countMap;
}

public Map<Long, Integer> findLikeCount(List<Long> ids) {
List<String> strKeys = redisCacheService.generateKeys(KeyPrefix.LIKE_COUNT, ids);
List<String> counts = stringCacheRedisTemplate.opsForValue().multiGet(strKeys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,16 @@ public class ProjectController {

@GetMapping("/projects")
public ResponseEntity<List<ProjectInfoResponseDto>> getProjects(@RequestBody ProjectSearchRequestDto projectSearchRequestDto) {
List<ProjectInfoResponseDto> response = projectService.getProjects(projectSearchRequestDto).stream()
.map(ProjectInfoResponseDto::of)
.collect(Collectors.toList());
long userId = SessionUtils.getLoginSessionUserId();

return ResponseEntity.ok().body(response);
return ResponseEntity.ok().body(projectService.getProjects(userId, projectSearchRequestDto));
}

@GetMapping("/project/{projectId}")
public ResponseEntity<ProjectInfoResponseDto> getProject(@PathVariable("projectId") long projectId) {
ProjectInfoResponseDto response = ProjectInfoResponseDto.of(projectService.getProject(projectId));
long userId = SessionUtils.getLoginSessionUserId();

return ResponseEntity.ok().body(response);
return ResponseEntity.ok().body(projectService.getProject(userId, projectId));
}

@LoginCheck
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.runningmate.runningmate.image.domain.entity.Image;
import com.runningmate.runningmate.project.dto.request.ProjectUpdateRequestDto;
import com.runningmate.runningmate.user.entity.User;
import java.util.ArrayList;
import lombok.*;

import java.time.LocalDate;
Expand Down Expand Up @@ -30,6 +31,7 @@ public class Project {
private List<ProjectPosition> projectPositions;
private List<ProjectSkill> projectSkills;
private List<ApplyQuestion> applyQuestions;
private List<ProjectMember> projectMembers;

public void updateInfo(ProjectUpdateRequestDto projectUpdateRequestDto) {
this.beginDate = projectUpdateRequestDto.getBeginDate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.runningmate.runningmate.project.dto.response;

import com.runningmate.runningmate.project.domain.entity.Project;
import com.runningmate.runningmate.project.domain.entity.ProjectMember;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -28,11 +30,45 @@ public class ProjectInfoResponseDto {
private LocalDateTime updateDate;
private String imagePath;

private int likeCount;
private boolean likePushed;

private List<ProjectPositionInfoResponseDto> positions;
private List<ProjectSkillInfoResponseDto> skills;
private List<ApplyQuestionInfoResponseDto> applyQuestions;
private List<ProjectMemberInfoResponseDto> members;

public static ProjectInfoResponseDto of(Project project, Map<Long, Integer> likeCountMap) {

return ProjectInfoResponseDto.builder()
.projectId(project.getProjectId())
.leaderEmail(project.getLeader().getEmail())
.leaderNickname(project.getLeader().getNickName())
.beginDate(project.getBeginDate())
.endDate(project.getEndDate())
.title(project.getTitle())
.contents(project.getContents())
.status(project.getStatus().toString())
.createDate(project.getCreateDate())
.updateDate(project.getUpdateDate())
.imagePath(project.getImage().getStorageFileName())
.likeCount(likeCountMap.get(project.getProjectId()))
.positions(project.getProjectPositions().stream()
.map(ProjectPositionInfoResponseDto::of)
.collect(Collectors.toList()))
.skills(project.getProjectSkills().stream()
.map(ProjectSkillInfoResponseDto::of)
.collect(Collectors.toList()))
.applyQuestions(project.getApplyQuestions().stream()
.map(ApplyQuestionInfoResponseDto::of)
.collect(Collectors.toList()))
.members(project.getProjectMembers().stream()
.map(ProjectMemberInfoResponseDto::of)
.collect(Collectors.toList()))
.build();
}

public static ProjectInfoResponseDto of(Project project) {
public static ProjectInfoResponseDto of(Project project, Map<Long, Integer> likeCountMap, Map<Long, Boolean> likeExistMap) {

return ProjectInfoResponseDto.builder()
.projectId(project.getProjectId())
Expand All @@ -46,6 +82,8 @@ public static ProjectInfoResponseDto of(Project project) {
.createDate(project.getCreateDate())
.updateDate(project.getUpdateDate())
.imagePath(project.getImage().getStorageFileName())
.likeCount(likeCountMap.get(project.getProjectId()))
.likePushed(likeExistMap.get(project.getProjectId()))
.positions(project.getProjectPositions().stream()
.map(ProjectPositionInfoResponseDto::of)
.collect(Collectors.toList()))
Expand All @@ -55,6 +93,9 @@ public static ProjectInfoResponseDto of(Project project) {
.applyQuestions(project.getApplyQuestions().stream()
.map(ApplyQuestionInfoResponseDto::of)
.collect(Collectors.toList()))
.members(project.getProjectMembers().stream()
.map(ProjectMemberInfoResponseDto::of)
.collect(Collectors.toList()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.runningmate.runningmate.project.dto.response;

import com.runningmate.runningmate.project.domain.entity.ProjectMember;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProjectMemberInfoResponseDto {
private long projectMemberId;
private long projectPositionId;
private long userId;

public static ProjectMemberInfoResponseDto of(ProjectMember projectMember) {
return ProjectMemberInfoResponseDto.builder()
.projectMemberId(projectMember.getProjectMemberId())
.projectPositionId(projectMember.getProjectPosition().getProjectPositionId())
.userId(projectMember.getUser().getUserId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.runningmate.runningmate.common.exception.NonCreatorException;
import com.runningmate.runningmate.image.domain.entity.Image;
import com.runningmate.runningmate.image.service.ImageUploadService;
import com.runningmate.runningmate.like.domain.repository.LikeCacheRepository;
import com.runningmate.runningmate.position.domain.entity.Position;
import com.runningmate.runningmate.project.domain.entity.*;
import com.runningmate.runningmate.project.domain.repository.ApplyAnswerRepository;
Expand All @@ -23,10 +24,12 @@
import com.runningmate.runningmate.project.dto.request.ProjectSearchRequestDto;
import com.runningmate.runningmate.project.dto.request.ProjectSkillSaveRequestDto;
import com.runningmate.runningmate.project.dto.request.ProjectUpdateRequestDto;
import com.runningmate.runningmate.project.dto.response.ProjectInfoResponseDto;
import com.runningmate.runningmate.skill.domain.entity.Skill;
import com.runningmate.runningmate.user.entity.User;
import com.runningmate.runningmate.user.repository.UserRepository;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
Expand Down Expand Up @@ -55,15 +58,34 @@ public class ProjectService {

private final ImageUploadService awsS3ImageUploadService;

private final LikeCacheRepository likeCacheRepository;

@Transactional(readOnly = true)
public List<Project> getProjects(ProjectSearchRequestDto projectSearchRequestDto) {
return mybatisProjectRepository.findAll(projectSearchRequestDto);
public List<ProjectInfoResponseDto> getProjects(long userId, ProjectSearchRequestDto projectSearchRequestDto) {
List<Project> projects = mybatisProjectRepository.findAll(projectSearchRequestDto);
List<Long> ids = projects.stream().map(Project::getProjectId).collect(Collectors.toList());

Map<Long, Integer> likeCountMap = likeCacheRepository.findLikeCount(ids);
Map<Long, Boolean> likeExistMap = userId == 0 ? null : likeCacheRepository.existLike(userId, ids);

return projects.stream()
.map(project -> likeExistMap == null
? ProjectInfoResponseDto.of(project, likeCountMap)
: ProjectInfoResponseDto.of(project, likeCountMap, likeExistMap))
.collect(Collectors.toList());
}

@Cacheable(value = "project", key = "#projectId")
@Transactional(readOnly = true)
public Project getProject(long projectId) {
return mybatisProjectRepository.findByProjectId(projectId);
public ProjectInfoResponseDto getProject(long userId, long projectId) {
Project project = mybatisProjectRepository.findByProjectId(projectId);

Map<Long, Integer> likeCountMap = likeCacheRepository.findLikeCount(project.getProjectId());
Map<Long, Boolean> likeExistMap = userId == 0 ? null : likeCacheRepository.existLike(userId, project.getProjectId());

return likeExistMap == null
? ProjectInfoResponseDto.of(project, likeCountMap)
: ProjectInfoResponseDto.of(project, likeCountMap, likeExistMap);
}

@Transactional
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/mapper/project/ProjectMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<collection property="projectPositions" resultMap="projectPositionMap"/>
<collection property="projectSkills" resultMap="projectSkillMap"/>
<collection property="applyQuestions" resultMap="applyQuestionMap"/>
<collection property="projectMembers" resultMap="projectMemberMap"/>
</resultMap>

<resultMap id="projectPositionMap" type="com.runningmate.runningmate.project.domain.entity.ProjectPosition">
Expand All @@ -54,6 +55,18 @@
<result column="question" property="question"/>
</resultMap>

<resultMap id="projectMemberMap" type="com.runningmate.runningmate.project.domain.entity.ProjectMember">
<id column="project_member_id" property="projectMemberId"/>

<association property="projectPosition" javaType="com.runningmate.runningmate.project.domain.entity.ProjectPosition">
<id column="m_project_position_id" property="projectPositionId"/>
</association>

<association property="user" javaType="com.runningmate.runningmate.user.entity.User">
<id column="m_user_id" property="userId"/>
</association>
</resultMap>

<select id="selectProjects" resultMap="projectMap">
SELECT
p.project_id
Expand Down Expand Up @@ -134,6 +147,9 @@
, s.skill_name
, a_q.apply_question_id
, a_q.question
, p_m.project_member_id
, p_m.project_position_id m_project_position_id
, p_m.user_id m_user_id
FROM project p
JOIN user u ON p.leader = u.user_id
JOIN image i ON p.image_id = i.image_id
Expand All @@ -142,6 +158,7 @@
JOIN project_skill p_s ON p.project_id = p_s.project_id
JOIN skill s ON p_s.skill_id = s.skill_id
JOIN apply_question a_q ON p.project_id = a_q.project_id
LEFT JOIN project_member p_m ON p_p.project_position_id = p_m.project_position_id
WHERE p.project_id = #{projectId}
</select>

Expand Down
Loading