Skip to content

Commit

Permalink
Merge pull request #235 from side-peek/refactor/#230-read-projects-by…
Browse files Browse the repository at this point in the history
…-category

프로젝트 기술 스택 및 멤버 카테고리 별로 조회되도록 응답 객체 수정
  • Loading branch information
yenzip authored Mar 21, 2024
2 parents 0b42420 + da93245 commit c82866e
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 147 deletions.
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();
}
}
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();

// 멤버(비회원)
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

0 comments on commit c82866e

Please sign in to comment.