Skip to content

Commit

Permalink
BE: [feat] 크루원 프로필 조회 CSID-DGU#83
Browse files Browse the repository at this point in the history
  • Loading branch information
Seoyoung2222 committed Dec 7, 2024
1 parent 9d441e6 commit b2a2797
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import RunningMachines.R2R.domain.crew.common.dto.CrewResponseDto;
import RunningMachines.R2R.domain.crew.common.service.CrewCommandService;
import RunningMachines.R2R.domain.crew.common.service.CrewQueryService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -49,4 +50,16 @@ public ResponseEntity<CrewMemberResponseDto> getCrewMember(@PathVariable Long cr
CrewMemberResponseDto responseDto = crewQueryService.getCrewMember(crewId);
return ResponseEntity.ok(responseDto);
}

@Operation(summary = "크루 멤버 프로필 조회")
@GetMapping("/{crewId}/member/{memberId}/{year}/{month}")
public ResponseEntity<CrewMemberResponseDto> getCrewMember(
@PathVariable Long crewId,
@PathVariable Long memberId,
@PathVariable int year,
@PathVariable int month) {

CrewMemberResponseDto responseDto = crewQueryService.getCrewMemberProfile(crewId, memberId, year, month);
return ResponseEntity.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package RunningMachines.R2R.domain.crew.common.dto;

import RunningMachines.R2R.domain.user.dto.UserDistanceResponseDto;
import RunningMachines.R2R.domain.user.dto.UserRecentResponseDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -16,4 +18,6 @@ public class CrewMemberResponseDto {
private int postCount;
private int memberCount;
private List<MemberDetailsDto> members;
private List<UserDistanceResponseDto> userDistance;
private List<UserRecentResponseDto> recentRuns;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package RunningMachines.R2R.domain.crew.common.service;

import RunningMachines.R2R.domain.course.repository.UserCourseRepository;
import RunningMachines.R2R.domain.crew.common.dto.CrewMemberResponseDto;
import RunningMachines.R2R.domain.crew.common.dto.CrewResponseDto;
import RunningMachines.R2R.domain.crew.common.dto.MemberDetailsDto;
import RunningMachines.R2R.domain.crew.common.entity.Crew;
import RunningMachines.R2R.domain.crew.common.entity.CrewUser;
import RunningMachines.R2R.domain.crew.common.repository.CrewRepository;
import RunningMachines.R2R.domain.crew.common.repository.CrewUserRepository;
import RunningMachines.R2R.domain.user.dto.UserDistanceResponseDto;
import RunningMachines.R2R.domain.user.dto.UserRecentResponseDto;
import RunningMachines.R2R.domain.user.entity.User;
import RunningMachines.R2R.domain.user.service.MyPageQueryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -20,6 +25,7 @@ public class CrewQueryService {

private final CrewRepository crewRepository;
private final CrewUserRepository crewUserRepository;
private final MyPageQueryService myPageQueryService;

public List<CrewResponseDto> getAllCrews() {
return crewRepository.findAll().stream()
Expand Down Expand Up @@ -51,6 +57,45 @@ public CrewMemberResponseDto getCrewMember(Long crewId) {
.build();
}

public CrewMemberResponseDto getCrewMemberProfile(Long crewId, Long memberId, int year, int month) {
Crew crew = crewRepository.findById(crewId)
.orElseThrow(() -> new IllegalArgumentException("해당 크루를 찾을 수 없습니다."));

List<CrewUser> crewUsers = crewUserRepository.findByCrewId(crewId);
CrewUser member = crewUsers.stream()
.filter(crewUser -> crewUser.getUser().getId().equals(memberId))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당 멤버를 찾을 수 없습니다."));

User user = member.getUser();

// 1. 멤버의 달리기 거리 달력 가져오기 (year, month를 기준으로)
List<UserDistanceResponseDto> userDistanceResponseDtos = myPageQueryService.getUserDistance(user.getEmail(), year, month);

// 2. 멤버의 최근 러닝 기록 가져오기
List<UserRecentResponseDto> recentRuns = myPageQueryService.getUserRecentRunning(user.getEmail());

List<MemberDetailsDto> members = crewUsers.stream()
.map(crewUser -> MemberDetailsDto.builder()
.userId(crewUser.getUser().getId())
.name(crewUser.getUser().getNickname())
.profileUrl(crewUser.getUser().getProfileImageUrl())
.membershipDuration(calculationMembershipDuration(crewUser.getCreatedAt()))
.role(crewUser.getRole().toString())
.build())
.toList();

// 최종 응답 DTO 생성
return CrewMemberResponseDto.builder()
.crewTitle(crew.getTitle())
.postCount(crew.getCrewPosts().size())
.memberCount(crewUsers.size())
.members(members)
.userDistance(userDistanceResponseDtos)
.recentRuns(recentRuns)
.build();
}

private String calculationMembershipDuration(LocalDateTime createdAt) {
long months = ChronoUnit.MONTHS.between(createdAt, LocalDateTime.now());
if (months < 12) {
Expand Down

0 comments on commit b2a2797

Please sign in to comment.