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

BE: [feat] 크루원 프로필 조회 #83 #170

Merged
merged 3 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
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 All @@ -24,7 +25,6 @@ public class CrewController {
public ResponseEntity<Integer> createCrew(@RequestParam String title, @RequestPart MultipartFile certificationImage,@RequestPart MultipartFile profileImage) {
CrewCreateCommandDto crewCreateCommandDto = CrewCreateCommandDto.builder()
.title(title)
.certificationImage(certificationImage)
.profileImage(profileImage)
.build();

Expand All @@ -50,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
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class NoticePostController {
@PostMapping
public ResponseEntity<Long> createNoticePost(@PathVariable Long crewId, @RequestBody NoticePostCreateRequestDto requestDto) {
User currentUser = authCommandService.getCurrentUser();
noticePostCommandService.createNoticePost(crewId, currentUser, requestDto);
return ResponseEntity.ok(crewId);
Long crewPostId = noticePostCommandService.createNoticePost(crewId, currentUser, requestDto);
return ResponseEntity.ok(crewPostId);
}

@GetMapping("/{crewPostId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class NoticePostCommandService {
private final CrewRepository crewRepository;

@Transactional
public void createNoticePost(Long crewId, User user, NoticePostCreateRequestDto noticePostCreateRequestDto) {
public Long createNoticePost(Long crewId, User user, NoticePostCreateRequestDto noticePostCreateRequestDto) {
Crew crew = validateCrew(crewId);
validateLeader(crew, user);

Expand All @@ -34,7 +34,8 @@ public void createNoticePost(Long crewId, User user, NoticePostCreateRequestDto
.user(user)
.build();

crewPostRepository.save(crewPost);
CrewPost savedPost = crewPostRepository.save(crewPost);
return savedPost.getId();
}

private Crew validateCrew(Long crewId) {
Expand Down