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

프로젝트 기술 스택 및 멤버 카테고리 별로 조회되도록 응답 객체 수정 #235

Merged
merged 19 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
24850c2
feat: projectSkill service에 카테고리 별로 기술 스택을 맵핑하는 메서드 생성
yenzip Mar 21, 2024
b032682
refactor: projectSkill 프로젝트별 조회 시 카테고리 별로 기술 스택 조회되도록 수정
yenzip Mar 21, 2024
e6de090
refactor: project 기술 스택 정보 응답 dto 카테고리 별로 기술 스택 반환하도록 수정
yenzip Mar 21, 2024
1ef1a6f
refactor: project 프로젝트 상세 조회 시 기술 스택 응답 객체 변경에 따른 service 메서드 수정
yenzip Mar 21, 2024
203145b
test: project 프로젝트 상세 조회 시 기술 스택 응답 객체 변경에 따른 테스트 코드 변경
yenzip Mar 21, 2024
7a730a2
refactor: projectSkill 맵핑 메서드 변수 이름 변경
yenzip Mar 21, 2024
cee2d85
refactor: userSummary 회원 및 비회원을 통합하는 정적 팩터리 메서드로 변경
yenzip Mar 21, 2024
5911643
refactor: project 멤버 정보 응답 dto 역할 별로 멤버 반환하도록 수정
yenzip Mar 21, 2024
5803738
feat: member service에 역할 별로 멤버를 맵핑하는 메서드 생성
yenzip Mar 21, 2024
bc5196f
refactor: member 프로젝트 상세 조회 시 역할 별로 멤버 조회되도록 수정
yenzip Mar 21, 2024
95d2809
refactor: project 상세 조회 시 멤버 정보 응답 객체 변경에 따른 service 메서드 수정
yenzip Mar 21, 2024
8cb2021
test: project 상세 조회 시 멤버 정보 응답 객체 변경에 따른 테스트 코드 변경
yenzip Mar 21, 2024
2fe2e4d
test: project 생서 및 삭제 시 기술 스택과 멤버 카테고리 및 역할 별로 응답 객체 변경에 따른 테스트 코드 수정
yenzip Mar 21, 2024
a123dea
style: 연속적인 공백 제거
yenzip Mar 21, 2024
dc6c09f
test: 기술 스택 및 멤버 카테고리별로 개수 구하는 메서드 추가
yenzip Mar 21, 2024
1837e88
test: project 기술 스택 및 멤버 카테고리별로 개수 구하는 메서드 사용하도록 관련 테스트 수정
yenzip Mar 21, 2024
8e9b583
refactor: member repository 사용하지 않는 findAllWithUser 메서드 제거 및 관련 파일 제거
yenzip Mar 21, 2024
0564386
refactor: memberRepository 서브 패키지 제거에 따른 위치 변경
yenzip Mar 21, 2024
da93245
test: memberRepository 서브 패키지 제거에 따른 위치 변경
yenzip Mar 21, 2024
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
@@ -1,28 +1,24 @@
package sixgaezzang.sidepeek.projects.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import lombok.Builder;
import sixgaezzang.sidepeek.projects.domain.member.Member;
import sixgaezzang.sidepeek.users.domain.User;
import sixgaezzang.sidepeek.users.dto.response.UserSummary;

@Schema(description = "프로젝트 멤버 정보")
@Builder
public record MemberSummary(
@Schema(description = "프로젝트 멤버 식별자", example = "1")
Long id,
@Schema(description = "프로젝트 멤버 역할", example = "백엔드")
String role,
@Schema(description = "프로젝트 멤버 회원/비회원 상세 정보")
UserSummary userSummary
List<UserSummary> userSummary
) {

public static MemberSummary from(Member member) {
User user = member.getUser();
UserSummary userSummary = (user != null) ? UserSummary.from(user, member.getNickname()) :
UserSummary.from(member.getNickname());

return new MemberSummary(member.getId(), member.getRole(), userSummary);
public static MemberSummary of(String role, List<UserSummary> members) {
return MemberSummary.builder()
.role(role)
.userSummary(members)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package sixgaezzang.sidepeek.projects.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import lombok.Builder;
import sixgaezzang.sidepeek.projects.domain.ProjectSkill;
import sixgaezzang.sidepeek.skill.dto.response.SkillResponse;

@Schema(description = "프로젝트 기술 스택 정보")
@Builder
public record ProjectSkillSummary(
@Schema(description = "프로젝트 기술 스택 식별자", example = "1")
Long id,
@Schema(description = "프로젝트 기술 스택 카테고리", example = "프론트엔드")
String category,
@Schema(description = "프로젝트 기술 스택 상세 정보")
SkillResponse skill
List<SkillResponse> skill
) {

public static ProjectSkillSummary from(ProjectSkill projectSkill) {
public static ProjectSkillSummary of(String category, List<SkillResponse> skill) {
return ProjectSkillSummary.builder()
.id(projectSkill.getId())
.category(projectSkill.getCategory())
.skill(SkillResponse.from(projectSkill.getSkill()))
.category(category)
.skill(skill)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sixgaezzang.sidepeek.projects.repository.member;
package sixgaezzang.sidepeek.projects.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import sixgaezzang.sidepeek.projects.domain.Project;
import sixgaezzang.sidepeek.projects.domain.member.Member;

public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom {
public interface MemberRepository extends JpaRepository<Member, Long> {

List<Member> findAllByProject(Project project);

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package sixgaezzang.sidepeek.projects.service;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static sixgaezzang.sidepeek.projects.util.validation.MemberValidator.validateMembers;
import static sixgaezzang.sidepeek.projects.util.validation.ProjectValidator.validateProject;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,8 +17,9 @@
import sixgaezzang.sidepeek.projects.domain.member.Member;
import sixgaezzang.sidepeek.projects.dto.request.SaveMemberRequest;
import sixgaezzang.sidepeek.projects.dto.response.MemberSummary;
import sixgaezzang.sidepeek.projects.repository.member.MemberRepository;
import sixgaezzang.sidepeek.projects.repository.MemberRepository;
import sixgaezzang.sidepeek.users.domain.User;
import sixgaezzang.sidepeek.users.dto.response.UserSummary;
import sixgaezzang.sidepeek.users.service.UserService;

@Service
Expand All @@ -26,7 +31,8 @@ public class MemberService {
private final UserService userService;

@Transactional
public List<MemberSummary> cleanAndSaveAll(Project project, List<SaveMemberRequest> memberSaveRequests) {
public List<MemberSummary> cleanAndSaveAll(Project project,
List<SaveMemberRequest> memberSaveRequests) {
validateProject(project);
validateMembers(project.getOwnerId(), memberSaveRequests);

Expand All @@ -39,14 +45,13 @@ public List<MemberSummary> cleanAndSaveAll(Project project, List<SaveMemberReque
)
.toList();

return memberRepository.saveAll(members)
.stream()
.map(MemberSummary::from)
.toList();
return generateMemberSummaries(memberRepository.saveAll(members));
}

public List<MemberSummary> findAllWithUser(Project project) {
return memberRepository.findAllWithUser(project);
public List<MemberSummary> findAll(Project project) {
List<Member> members = memberRepository.findAllByProject(project);

return generateMemberSummaries(members);
}

public Optional<User> findFellowMemberByProject(Long userId, Project project) {
Expand All @@ -62,4 +67,15 @@ private void cleanExistingMembersByProject(Project project) {
memberRepository.deleteAllByProject(project);
}
}

private List<MemberSummary> generateMemberSummaries(List<Member> members) {
Map<String, List<UserSummary>> memberMap = members.stream()
.collect(groupingBy(Member::getRole,
mapping(member -> UserSummary.of(member.getUser(), member.getNickname()),
toList())));

return memberMap.entrySet().stream()
.map(entry -> MemberSummary.of(entry.getKey(), entry.getValue()))
.toList();
}
Comment on lines +70 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 감사합니다ㅠㅠ 새로 배워가네요ㅠㅠ

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ public ProjectResponse save(Long loginId, SaveProjectRequest request) {
Project project = request.toEntity();
projectRepository.save(project);

List<ProjectSkillSummary> techStacks = projectSkillService.cleanAndSaveAll(project, request.techStacks());
List<ProjectSkillSummary> techStacks = projectSkillService.cleanAndSaveAll(project,
request.techStacks());
List<MemberSummary> members = memberService.cleanAndSaveAll(project, request.members());
List<OverviewImageSummary> overviewImages = fileService.cleanAndSaveAll(project, request.overviewImageUrls());
List<OverviewImageSummary> overviewImages = fileService.cleanAndSaveAll(project,
request.overviewImageUrls());

return ProjectResponse.from(project, overviewImages, techStacks, members, Collections.emptyList(), null);
return ProjectResponse.from(project, overviewImages, techStacks, members,
Collections.emptyList(), null);
}

public Project getById(Long projectId) {
Expand All @@ -96,12 +99,9 @@ public ProjectResponse findById(String ip, Long loginId, Long projectId) {
.map(OverviewImageSummary::from)
.toList();

List<ProjectSkillSummary> techStacks = projectSkillService.findAll(project)
.stream()
.map(ProjectSkillSummary::from)
.toList();
List<ProjectSkillSummary> techStacks = projectSkillService.findAll(project);

List<MemberSummary> members = memberService.findAllWithUser(project);
List<MemberSummary> members = memberService.findAll(project);

List<CommentResponse> comments = commentService.findAll(loginId, project);

Expand Down Expand Up @@ -151,9 +151,11 @@ public ProjectResponse update(Long loginId, Long projectId, UpdateProjectRequest

project.update(request);

List<ProjectSkillSummary> techStacks = projectSkillService.cleanAndSaveAll(project, request.techStacks());
List<ProjectSkillSummary> techStacks = projectSkillService.cleanAndSaveAll(project,
request.techStacks());
List<MemberSummary> members = memberService.cleanAndSaveAll(project, request.members());
List<OverviewImageSummary> overviewImages = fileService.cleanAndSaveAll(project, request.overviewImageUrls());
List<OverviewImageSummary> overviewImages = fileService.cleanAndSaveAll(project,
request.overviewImageUrls());

List<CommentResponse> comments = commentService.findAll(loginId, project);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package sixgaezzang.sidepeek.projects.service;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static sixgaezzang.sidepeek.common.util.validation.TechStackValidator.validateTechStacks;
import static sixgaezzang.sidepeek.projects.util.validation.ProjectValidator.validateProject;

import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -12,6 +16,7 @@
import sixgaezzang.sidepeek.projects.domain.ProjectSkill;
import sixgaezzang.sidepeek.projects.dto.response.ProjectSkillSummary;
import sixgaezzang.sidepeek.projects.repository.ProjectSkillRepository;
import sixgaezzang.sidepeek.skill.dto.response.SkillResponse;
import sixgaezzang.sidepeek.skill.serivce.SkillService;

@Service
Expand All @@ -22,23 +27,23 @@ public class ProjectSkillService {
private final ProjectSkillRepository projectSkillRepository;
private final SkillService skillService;

public List<ProjectSkill> findAll(Project project) {
return projectSkillRepository.findAllByProject(project);
public List<ProjectSkillSummary> findAll(Project project) {
List<ProjectSkill> skills = projectSkillRepository.findAllByProject(project);

return generateProjectSkillSummaries(skills);
}

@Transactional
public List<ProjectSkillSummary> cleanAndSaveAll(Project project, List<SaveTechStackRequest> techStacks) {
public List<ProjectSkillSummary> cleanAndSaveAll(Project project,
List<SaveTechStackRequest> techStacks) {
validateProject(project);
validateTechStacks(techStacks);

cleanExistingProjectSkillsByProject(project);

List<ProjectSkill> skills = convertAllToEntity(project, techStacks);

return projectSkillRepository.saveAll(skills)
.stream()
.map(ProjectSkillSummary::from)
.toList();
return generateProjectSkillSummaries(projectSkillRepository.saveAll(skills));
}

private void cleanExistingProjectSkillsByProject(Project project) {
Expand All @@ -47,7 +52,8 @@ private void cleanExistingProjectSkillsByProject(Project project) {
}
}

private List<ProjectSkill> convertAllToEntity(Project project, List<SaveTechStackRequest> techStacks) {
private List<ProjectSkill> convertAllToEntity(Project project,
List<SaveTechStackRequest> techStacks) {
return techStacks.stream()
.map(techStack -> techStack.toProjectSkill(
project,
Expand All @@ -56,4 +62,15 @@ private List<ProjectSkill> convertAllToEntity(Project project, List<SaveTechStac
.toList();
}

private List<ProjectSkillSummary> generateProjectSkillSummaries(List<ProjectSkill> skills) {
Map<String, List<SkillResponse>> skillMap = skills.stream()
.collect(groupingBy(ProjectSkill::getCategory,
mapping(skill -> SkillResponse.from(skill.getSkill()),
toList())));

return skillMap.entrySet().stream()
.map(entry -> ProjectSkillSummary.of(entry.getKey(), entry.getValue()))
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
public record UserSummary(
@Schema(description = "회원 식별자(비회원은 null)", nullable = true, example = "1")
Long id,

@Schema(description = "소셜 로그인 회원 여부", nullable = true, example = "false")
@JsonInclude(JsonInclude.Include.NON_NULL)
Boolean isSocialLogin,

@Schema(description = "회원/비회원 닉네임", example = "의진")
String nickname,

@Schema(description = "회원 프로필 이미지, 비회원이거나 없으면 빈 문자열 반환", nullable = true,
example = "https://user-images.githubusercontent.com/uijin.png")
String profileImageUrl
Expand Down Expand Up @@ -48,23 +45,15 @@ public static UserSummary from(User user) {
.build();
}

// 멤버(회원)
public static UserSummary from(User user, String nickname) {
return UserSummary.builder()
.id(user.getId())
.isSocialLogin(null)
.nickname(nickname)
.profileImageUrl(user.getProfileImageUrl())
.build();
}
// 멤버(회원, 비회원)
public static UserSummary of(User user, String nickname) {
Long userId = Objects.isNull(user) ? null : user.getId();
String profileImageUrl = Objects.isNull(user) ? BLANK_STRING : user.getProfileImageUrl();
Comment on lines +48 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드 중복 줄여주셔서 감사합니다ㅠㅠ


// 멤버(비회원)
public static UserSummary from(String nickname) {
return UserSummary.builder()
.id(null)
.isSocialLogin(null)
.id(userId)
.nickname(nickname)
.profileImageUrl(BLANK_STRING)
.profileImageUrl(profileImageUrl)
.build();
}

Expand Down
Loading
Loading