Skip to content

Commit

Permalink
[COZY-182] 멤버 상세정보 멤버 ID로 조회 및 일치율 조회 각 OS 별 분리 (#63)
Browse files Browse the repository at this point in the history
* [COZY-182] feat: id로 사용자 정보 조회, 일치율에 상세정보 포함한 API 완료

* [COZY-182] feat: 멤버 존재 여부 검사 추가, 불필요한 import 추가
  • Loading branch information
jpark0506 authored Aug 16, 2024
1 parent 329ecad commit 3dcb649
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.cozymate.cozymate_server.domain.memberstat.controller;

import com.cozymate.cozymate_server.domain.auth.userDetails.MemberDetails;
import com.cozymate.cozymate_server.domain.member.Member;
import com.cozymate.cozymate_server.domain.memberstat.converter.MemberStatConverter;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatResponseDTO.MemberStatEqualityResponseDTO;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatResponseDTO.MemberStatQueryResponseDTO;
import com.cozymate.cozymate_server.domain.memberstat.service.MemberStatCommandService;
import com.cozymate.cozymate_server.domain.memberstat.service.MemberStatQueryService;
Expand Down Expand Up @@ -51,7 +49,7 @@ public class MemberStatController {
ErrorStatus._UNIVERSITY_NOT_FOUND,
ErrorStatus._MEMBERSTAT_MERIDIAN_NOT_VALID
})
@PostMapping("/")
@PostMapping("")
public ResponseEntity<ApiResponse<Long>> createMemberStat(
@AuthenticationPrincipal MemberDetails memberDetails,
@Valid @RequestBody MemberStatRequestDTO.MemberStatCommandRequestDTO memberStatCommandRequestDTO) {
Expand All @@ -73,7 +71,7 @@ public ResponseEntity<ApiResponse<Long>> createMemberStat(
ErrorStatus._MEMBERSTAT_NOT_EXISTS,
ErrorStatus._MEMBERSTAT_MERIDIAN_NOT_VALID
})
@PutMapping("/")
@PutMapping("")
public ResponseEntity<ApiResponse<Long>> modifyMemberStat(
@AuthenticationPrincipal MemberDetails memberDetails,
@Valid @RequestBody MemberStatRequestDTO.MemberStatCommandRequestDTO memberStatCommandRequestDTO) {
Expand All @@ -91,7 +89,7 @@ public ResponseEntity<ApiResponse<Long>> modifyMemberStat(
@SwaggerApiError({
ErrorStatus._MEMBERSTAT_NOT_EXISTS
})
@GetMapping("/")
@GetMapping("")
public ResponseEntity<ApiResponse<MemberStatQueryResponseDTO>> getMemberStat(
@AuthenticationPrincipal MemberDetails memberDetails
) {
Expand All @@ -103,6 +101,26 @@ public ResponseEntity<ApiResponse<MemberStatQueryResponseDTO>> getMemberStat(
));
}

@Operation(
summary = "[포비] 사용자 상세정보 조회",
description = "사용자 토큰을 넣고, memberId를 PathVariable로 사용합니다.\n\n"
+ "시간 관련 처리를 유의해주세요."
)
@SwaggerApiError({
ErrorStatus._MEMBERSTAT_NOT_EXISTS
})
@GetMapping("/{memberId}")
public ResponseEntity<ApiResponse<MemberStatQueryResponseDTO>> getMemberStat(
@PathVariable Long memberId
) {
MemberStatQueryResponseDTO memberStatQueryResponseDTO = MemberStatConverter.toDto(
memberStatQueryService.getMemberStatWithId(memberId));
return ResponseEntity.ok(
ApiResponse.onSuccess(
memberStatQueryResponseDTO
));
}

@Operation(
summary = "[포비] 사용자 상세정보 필터링, 일치율 조회",
description = "사용자의 토큰을 넣어 사용합니다."
Expand Down Expand Up @@ -137,16 +155,65 @@ public ResponseEntity<ApiResponse<MemberStatQueryResponseDTO>> getMemberStat(
ErrorStatus._MEMBERSTAT_FILTER_PARAMETER_NOT_VALID
})
@GetMapping("/search")
public ResponseEntity<ApiResponse<PageResponseDto<List<MemberStatEqualityResponseDTO>>>> getFilteredMemberList(
public ResponseEntity<ApiResponse<PageResponseDto<List<?>>>> getFilteredMemberList(
@AuthenticationPrincipal MemberDetails memberDetails,
@RequestParam(defaultValue = "0") int page,
@RequestParam(required = false) List<String> filterList) {
Pageable pageable = PageRequest.of(page, 5);
return ResponseEntity.ok(
ApiResponse.onSuccess(
memberStatQueryService.getMemberStatList(
memberDetails.getMember(), filterList, pageable)
memberDetails.getMember(), filterList, pageable, false)
));
}

@Operation(
summary = "[포비] 사용자 상세정보 필터링, 일치율 조회(상세 정보 포함)",
description = "사용자의 토큰을 넣어 사용합니다."
+ "filterList = 필터명1,필터명2,...으로 사용하고, 없을 경우 쿼리문에 아예 filterList를 넣지 않으셔도 됩니다.\n\n"
+ "사용 가능한 필터명(20개):\n"
+ "- acceptance : 합격여부\n"
+ "- admissionYear : 학번\n"
+ "- major : 전공\n"
+ "- numOfRoommate : 신청실\n"
+ "- wakeUpTime : 기상시간\n"
+ "- sleepingTime : 취침시간\n"
+ "- turnOffTime : 소등시간\n"
+ "- smoking : 흡연여부\n"
+ "- sleepingHabit : 잠버릇\n"
+ "- airConditioningIntensity : 에어컨 강도\n"
+ "- heatingIntensity : 히터 강도\n"
+ "- lifePattern : 생활패턴\n"
+ "- intimacy : 친밀도\n"
+ "- canShare : 물건공유\n"
+ "- isPlayGame : 게임여부\n"
+ "- isPhoneCall : 전화여부\n"
+ "- studying : 공부여부\n"
+ "- intake : 섭취여부\n"
+ "- cleanSensitivity : 청결예민도\n"
+ "- noiseSensitivity : 소음예민도\n"
+ "- cleaningFrequency : 청소예민도\n"
+ "- personality : 성격\n"
+ "- mbti : mbti"
)
@SwaggerApiError({
ErrorStatus._MEMBERSTAT_NOT_EXISTS,
ErrorStatus._MEMBERSTAT_FILTER_PARAMETER_NOT_VALID
})
@GetMapping("/search/details")
public ResponseEntity<ApiResponse<PageResponseDto<List<?>>>> getFilteredMemberListWithMemberDetails(
@AuthenticationPrincipal MemberDetails memberDetails,
@RequestParam(defaultValue = "0") int page,
@RequestParam(required = false) List<String> filterList) {

Pageable pageable = PageRequest.of(page, 5);

return ResponseEntity.ok(
ApiResponse.onSuccess(
memberStatQueryService.getMemberStatList(
memberDetails.getMember(), filterList, pageable, true)
));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ public static MemberStatQueryResponseDTO toDto(MemberStat memberStat){
.options(memberStat.getOptions())
.build();
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cozymate.cozymate_server.domain.memberstat.dto;

import com.cozymate.cozymate_server.domain.member.Member;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -62,4 +63,21 @@ public static class MemberStatEqualityResponseDTO {
private Integer numOfRoommate;
private Integer equality;
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class MemberStatEqualityDetailResponseDTO {

private MemberStatEqualityResponseDTO info;
private MemberStatQueryResponseDTO detail;

@JsonIgnore
public MemberStatEqualityResponseDTO getMemberStatEqualityResponseDTO() {
return this.info;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.cozymate.cozymate_server.domain.member.Member;
import com.cozymate.cozymate_server.domain.member.repository.MemberRepository;
import com.cozymate.cozymate_server.domain.memberstat.MemberStat;
import com.cozymate.cozymate_server.domain.memberstat.converter.MemberStatConverter;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatResponseDTO.MemberStatEqualityDetailResponseDTO;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatResponseDTO.MemberStatEqualityResponseDTO;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatResponseDTO.MemberStatQueryResponseDTO;
import com.cozymate.cozymate_server.domain.memberstat.repository.MemberStatRepository;
import com.cozymate.cozymate_server.domain.memberstat.util.MemberUtil;
import com.cozymate.cozymate_server.global.common.PageResponseDto;
Expand All @@ -26,6 +29,7 @@
public class MemberStatQueryService {

private final MemberStatRepository memberStatRepository;
private final MemberRepository memberRepository;


public MemberStat getMemberStat(Member member) {
Expand All @@ -35,8 +39,20 @@ public MemberStat getMemberStat(Member member) {
);
}

public PageResponseDto<List<MemberStatEqualityResponseDTO>> getMemberStatList(Member member,
List<String> filterList, Pageable pageable) {
public MemberStat getMemberStatWithId(Long memberId) {

if(!memberRepository.existsById(memberId)){
throw new GeneralException(ErrorStatus._MEMBER_NOT_FOUND);
}

return memberStatRepository.findByMemberId(memberId)
.orElseThrow(
() -> new GeneralException(ErrorStatus._MEMBERSTAT_NOT_EXISTS)
);
}

public PageResponseDto<List<?>> getMemberStatList(Member member,
List<String> filterList, Pageable pageable, boolean needsDetail) {

//일치율의 기준이 되는 MemberStat을 가져오고, 유효성을 검사합니다.
MemberStat criteriaMemberStat = memberStatRepository.findByMemberId(member.getId())
Expand All @@ -52,18 +68,38 @@ public PageResponseDto<List<MemberStatEqualityResponseDTO>> getMemberStatList(Me
return new PageResponseDto<>(pageable.getPageNumber(), false, Collections.emptyList());
}

// 일치율을 계산하고, 정렬합니다.
if (needsDetail) {
List<MemberStatEqualityDetailResponseDTO> result = filteredResult.stream()
.map(memberStat -> {
MemberStatEqualityResponseDTO equalityResponse = MemberUtil.toEqualityResponse(
criteriaMemberStat, memberStat);
MemberStatQueryResponseDTO queryResponse = MemberStatConverter.toDto(
memberStat);
return MemberStatEqualityDetailResponseDTO.builder()
.info(equalityResponse)
.detail(queryResponse)
.build();
})
.sorted((dto1, dto2) -> Integer.compare(
dto2.getMemberStatEqualityResponseDTO().getEquality(),
dto1.getMemberStatEqualityResponseDTO().getEquality()
))
.toList();
return toPageResponseDto(result, pageable);
}

List<MemberStatEqualityResponseDTO> result = filteredResult
.stream()
.map(memberStat -> MemberUtil.toEqualityResponse(criteriaMemberStat, memberStat))
.sorted(Comparator.comparingInt(MemberStatEqualityResponseDTO::getEquality).reversed())
.toList();

// MemberStat 전체 엔티티를 대상으로 하기 때문에, 우선 Page로 구현했습니다.
// List를 Page로 변환하기 위해 아래 코드를 사용합니다.
// 기존에 만들어진 PageResponseDTO를 활용해보려고 노력했습니다.
return toPageResponseDto(result, pageable);

}

private PageResponseDto<List<?>> toPageResponseDto(List<?> result, Pageable pageable) {

// 전체 페이지 수 계산
int totalPages = (int) Math.ceil((double) result.size() / pageable.getPageSize());

// 요청한 페이지가 범위를 벗어났을 경우 빈 페이지 반환
Expand All @@ -78,6 +114,8 @@ public PageResponseDto<List<MemberStatEqualityResponseDTO>> getMemberStatList(Me
// 해당 페이지의 데이터만 추출하여 Page로 반환
return new PageResponseDto<>(pageable.getPageNumber(),
pageable.getPageNumber() + 1 < totalPages, result.subList(start, end));

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public static MemberStatEqualityResponseDTO toEqualityResponse(MemberStat criter
.build();
}



private static int calculateTimeScore(Integer time1, Integer time2) {
int timeDifference = Math.abs(time1 - time2);
return switch (timeDifference) {
Expand Down

0 comments on commit 3dcb649

Please sign in to comment.