Skip to content

Commit

Permalink
feat: Add filtering memberlist code (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanna29 committed Dec 7, 2023
1 parent 7ab261a commit 3feb687
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.gdscswu_server.server.domain.networking.service.NetworkService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -15,10 +16,12 @@
public class NetworkController {
private final NetworkService networkService;

// 멤버 리스트 응답
@GetMapping("")
public ResponseEntity<Object> getAllMembers() {
return networkService.findAllMembers();
// 필터링 거쳐서 멤버 리스트 응답
// 필터링 할 filterCondition=null 이면 전체 멤버 리스트 return
// null 아니면 필터링 된 일부 멤버 리스트 return
@PostMapping
public ResponseEntity<Object> getAllMembers(@RequestBody FilterOptionsRequestDto filterOptionsRequestDto) {
return networkService.findAllMembers(filterOptionsRequestDto);
}

// 북마크 설정/해제
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdscswu_server.server.domain.networking.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -10,13 +11,14 @@
@Getter
@Setter
public class FilterOptionsRequestDto {
private String department;
private String part;
private String level;
private List<String> departments;
private List<String> parts;
private List<String> levels;

public FilterOptionsRequestDto(String departments, String parts, String levels) {
this.department = departments;
this.part = parts;
this.level = levels;
@Builder
public FilterOptionsRequestDto(List<String> departments, List<String> parts, List<String> levels) {
this.departments = departments;
this.parts = parts;
this.levels = levels;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,28 @@ public class NetworkService {
public final BookmarkRepository bookmarkRepository;

@Transactional
public ResponseEntity<Object> findAllMembers() {
public ResponseEntity<Object> findAllMembers(FilterOptionsRequestDto filterOptionsRequestDto) {
try {
List<Member> members = memberRepository.findAll();

List<MemberResponseDto> memberResponseDtos = members.stream()
.map(this::createMemberResponseDto)
.collect(Collectors.toList());
return ResponseEntity.ok(memberResponseDtos);
List<Member> members = memberRepository.findAll();

List<MemberResponseDto> memberResponseDtos = members.stream()
.map(member -> createMemberResponseDto(member,filterOptionsRequestDto))
.collect(Collectors.toList());
return ResponseEntity.ok(memberResponseDtos);
} catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}

}

@Transactional
public ResponseEntity<Object> bookmarkMember(Long memberIdToBookmark) {
try {
// 해당 id의 멤버를 Member db 에서 찾아오기
Member memberToBookmark = memberRepository.findById(memberIdToBookmark)
.orElseThrow(() -> new EntityNotFoundException("Member not found with id: " + memberIdToBookmark));
// 해당 멤버가 북마크가 되어있는지 검사해오기 (북마크 되어있음 -> true)
boolean hasBookmark = bookmarkRepository.existsByTargetMemberId(memberIdToBookmark);
// 북마크가 되어있다면 삭제
if (hasBookmark) {
bookmarkRepository.deleteByTargetMemberId(memberIdToBookmark);
} else {
bookmarkRepository.save(new Bookmark(memberToBookmark, memberToBookmark));
}
return ResponseEntity.ok(bookmarkRepository.existsByTargetMemberId(memberIdToBookmark));

} catch (Exception e){
return ResponseEntity.badRequest().body(e.getMessage());
}

}

@Transactional
private MemberResponseDto createMemberResponseDto(Member member) {
// 북마크 db 에서 해당 멤버가 존재하는지 확인
private MemberResponseDto createMemberResponseDto(Member member, FilterOptionsRequestDto filterOptionsRequestDto) {
// 북마크 db 에서 해당 멤버가 존재하는지 확인, bookmark 값 정하기
boolean bookmark = bookmarkRepository.existsByTargetMemberId(member.getId());
List<GenerationResponseDto> generationResponseDtoList = createGenerationResponseDtoList(member);
// generationResponseDtoList 만들기 위해 함수 호출
List<GenerationResponseDto> generationResponseDtoList = createGenerationResponseDtoList(member, filterOptionsRequestDto);
// MemberResponseDto 생성
return MemberResponseDto.builder()
.member(member)
.bookmark(bookmark)
Expand All @@ -77,11 +57,17 @@ private MemberResponseDto createMemberResponseDto(Member member) {
}

@Transactional
private List<GenerationResponseDto> createGenerationResponseDtoList(Member member) {
List<Generation> generations = generationRepository.findByMember(member);
return generations.stream()
private List<GenerationResponseDto> createGenerationResponseDtoList(Member member, FilterOptionsRequestDto filterOptionsRequestDto) {
// 해당 멤버 Generation
List<Generation> generationList = generationRepository.findByMember(member);
return generationList.stream()
.filter(generation ->
(filterOptionsRequestDto.getDepartments().isEmpty() || filterOptionsRequestDto.getDepartments().contains(generation.getDepartment())) &&
(filterOptionsRequestDto.getLevels().isEmpty() || filterOptionsRequestDto.getLevels().contains(generation.getLevel()))) // Filter based on department and level
.map(generation -> {
List<ProjectResponseDto> projectResponseDtoList = createProjectResponseDtoList(member, generation);
// projectResponseDtoList 만들기 위해서 함수 호출
List<ProjectResponseDto> projectResponseDtoList = createProjectResponseDtoList(member, generation, filterOptionsRequestDto);
// GenerationResponseDto 생성
return GenerationResponseDto.builder()
.generation(generation)
.projectResponseDtoList(projectResponseDtoList)
Expand All @@ -90,15 +76,66 @@ private List<GenerationResponseDto> createGenerationResponseDtoList(Member membe
.collect(Collectors.toList());
}
@Transactional
private List<ProjectResponseDto> createProjectResponseDtoList(Member member, Generation generation) {
private List<ProjectResponseDto> createProjectResponseDtoList(Member member, Generation generation, FilterOptionsRequestDto filterOptionsRequestDto) {
List<Project> projects = projectRepository.findByMemberAndGeneration(member, generation);

Set<String> existingParts = new HashSet<>();

return projects.stream()
.filter(project -> existingParts.add(project.getPart())) // 중복된 part가 없는 경우만 필터링
// 중복된 part가 있으면 그 project 는 제외
// 필터링 조건 있다면 조건 부합하지 않는 project 제외
.filter(project -> existingParts.add(project.getPart())&&
(filterOptionsRequestDto.getParts().isEmpty() || filterOptionsRequestDto.getParts().contains(project.getPart()))) // Filter based on parts
.map(ProjectResponseDto::new)
.collect(Collectors.toList());
}



@Transactional
public ResponseEntity<Object> bookmarkMember(Long memberIdToBookmark) {
try {
// 해당 id의 멤버를 Member db 에서 찾아오기
Member memberToBookmark = memberRepository.findById(memberIdToBookmark)
.orElseThrow(() -> new EntityNotFoundException("Member not found with id: " + memberIdToBookmark));
// 해당 멤버가 북마크가 되어있는지 검사해오기 (북마크 되어있음 -> true)
boolean hasBookmark = bookmarkRepository.existsByTargetMemberId(memberIdToBookmark);
// 북마크가 되어있다면 삭제
if (hasBookmark) {
bookmarkRepository.deleteByTargetMemberId(memberIdToBookmark);
} else {
bookmarkRepository.save(new Bookmark(memberToBookmark, memberToBookmark));
}
return ResponseEntity.ok(bookmarkRepository.existsByTargetMemberId(memberIdToBookmark));

} catch (Exception e){
return ResponseEntity.badRequest().body(e.getMessage());
}

}
/*
private boolean isMemberMatch(Member member, FilterOptionsRequestDto filterOptionsRequestDto) {
// 필터링 하려고 들어온 부서, 파트, 레벨 값이 전부 비어있으면 (필터링 조건이 없으면)
// 모든 멤버를 포함해줘야 해서 항상 true 리턴
if (filterOptionsRequestDto.getDepartments().isEmpty() &&
filterOptionsRequestDto.getParts().isEmpty() &&
filterOptionsRequestDto.getLevels().isEmpty()){
return true;
}
// 필터링 조건에 부합하는 사람은 true return 하여 포함 시키기
// 필터링 조건에 부합하지 않는 사람은 false return 하여 제외 시키기
}
*/

/* @Transactional
private List<MemberResponseDto> filteringMembers(String filterCondition){
List<Member> members = memberRepository.findAll();
// 조건에 의해 필터링 된 일부 멤버 리스트가 담긴 List<MemberResponseDto>
List<MemberResponseDto> memberResponseDtos;
// 프론트에 조건에 의해 필터링 된 일부 멤버 리스트가 담긴 List<MemberResponseDto> 전달
return memberResponseDtos;
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.gdscswu_server.server.domain.member.dto.LoginResponseDto;
import com.gdscswu_server.server.domain.networking.domain.Bookmark;
import com.gdscswu_server.server.domain.networking.domain.BookmarkRepository;
import com.gdscswu_server.server.domain.networking.dto.FilterOptionsRequestDto;
import com.gdscswu_server.server.domain.networking.dto.GenerationResponseDto;
import com.gdscswu_server.server.domain.networking.dto.MemberResponseDto;
import com.gdscswu_server.server.domain.networking.dto.ProjectResponseDto;
Expand All @@ -20,6 +21,7 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -53,8 +55,8 @@ class NetworkControllerTest {
@Autowired
private ObjectMapper objectMapper;
@Test
@DisplayName("전체 멤버 조회 테스트")
public void findAllMembers() throws Exception{
@DisplayName("전체 + 필터링 멤버 조회 테스트")
public void findFilteredMembers() throws Exception{
// Given
Member member1 = memberRepository.save(Member.builder()
.googleEmail("[email protected]")
Expand Down Expand Up @@ -124,71 +126,41 @@ public void findAllMembers() throws Exception{
.part("Back")
.build());

// 예시로 몇 가지 값을 할당
final List<String> departments = Arrays.asList("Android", "Server");
final List<String> parts = Arrays.asList("PM", "Back");
final List<String> levels = Arrays.asList("Core");

/* ProjectResponseDto partListResponseDto1 = ProjectResponseDto.builder()
.projects(project1)
.build();
ProjectResponseDto partListResponseDto2 = ProjectResponseDto.builder()
.projects(project2)
.build();
ProjectResponseDto partListResponseDto3 = ProjectResponseDto.builder()
.projects(project3)
.build();
ProjectResponseDto partListResponseDto4 = ProjectResponseDto.builder()
.projects(project4)
.build();
ProjectResponseDto partListResponseDto5 = ProjectResponseDto.builder()
.projects(project5)
.build();
GenerationResponseDto generationResponseDto1 = GenerationResponseDto.builder()
.generation(generation1)
.projectResponseDtoList(List.of(partListResponseDto1,partListResponseDto2,partListResponseDto3))
.build();
GenerationResponseDto generationResponseDto2 = GenerationResponseDto.builder()
.generation(generation2)
.projectResponseDtoList(List.of(partListResponseDto4))
.build();
GenerationResponseDto generationResponseDto3 = GenerationResponseDto.builder()
.generation(generation3)
.projectResponseDtoList(List.of(partListResponseDto5))
FilterOptionsRequestDto filterOptionsRequestDto= FilterOptionsRequestDto.builder()
.parts(departments)
.departments(parts)
.levels(levels)
.build();

MemberResponseDto memberResponseDto1 = MemberResponseDto.builder()
.member(member1)
.bookmark(false)
.generationResponseDtoList(List.of(generationResponseDto1,generationResponseDto2))
.build();
MemberResponseDto memberResponseDto2 = MemberResponseDto.builder()
.member(member2)
.bookmark(true)
.generationResponseDtoList(List.of(generationResponseDto3))
.build();*/


// When
// 김슈니 박슈니 전부 저장
generationRepository.saveAll(List.of(generation1, generation2, generation3));
projectRepository.saveAll(List.of(project1, project2, project3, project4, project5));
memberRepository.saveAll(List.of(member1,member2));
// 북마크는 김슈니만 되어있는 상태
bookmarkRepository.save(new Bookmark(member1, member1));

// then
String url = "http://localhost:" + port + "/api/v1/network";
LoginResponseDto loginResponseDto = jwtUtil.generateTokens(member1);

mockMvc.perform(get(url)
.contentType(APPLICATION_JSON).header("Authorization", "Bearer " + loginResponseDto.getAccessToken()))
mockMvc.perform(post(url)
.contentType(APPLICATION_JSON).header("Authorization", "Bearer " + loginResponseDto.getAccessToken())
.content(new ObjectMapper().writeValueAsString(filterOptionsRequestDto)))
.andExpect(status().isOk())
.andExpect(jsonPath("$[1].name").value("김슈니"))
//.andExpect(jsonPath("$[1].memberDetails[0].partList[0].part[0]").value("PM"))
//.andExpect(jsonPath("$[1].memberDetails[0].partList[1].part[0]").value("Front"))
.andDo(print());
}



@Test
@DisplayName("멤버 북마크 기능 설정 테스트")
public void setBookmark() throws Exception{
Expand All @@ -205,7 +177,7 @@ public void setBookmark() throws Exception{
.level("Member")
.build());
Project project1 = projectRepository.save(Project.builder()
.title("김슈니의 1기 첫번째 프젝")
.title("김우동의 프젝")
.generation(generation1)
.member(member1)
.part("PM")
Expand Down Expand Up @@ -245,7 +217,7 @@ public void removeBookmark() throws Exception{
.level("Member")
.build());
Project project1 = projectRepository.save(Project.builder()
.title("김슈니의 1기 첫번째 프젝")
.title("김우동의 프젝")
.generation(generation1)
.member(member1)
.part("PM")
Expand Down

0 comments on commit 3feb687

Please sign in to comment.