Skip to content

Commit

Permalink
Merge pull request #46 from Likelion12/20-be-크루상세조회
Browse files Browse the repository at this point in the history
Feat : 크루상세조회
  • Loading branch information
hd0rable authored Aug 2, 2024
2 parents 7b9e1dc + ba67e63 commit 158baf9
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.likelion12.common.exception;

import com.example.likelion12.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class MemberCrewException extends RuntimeException {

private final ResponseStatus exceptionStatus;

public MemberCrewException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}

public MemberCrewException(ResponseStatus exceptionStatus, String message) {
super(message);
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.likelion12.common.exception_handler;

import com.example.likelion12.common.exception.FacilityException;
import com.example.likelion12.common.exception.MemberCrewException;
import com.example.likelion12.common.response.BaseErrorResponse;
import jakarta.annotation.Priority;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Priority(0)
@RestControllerAdvice
public class MemberCrewExceptionControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MemberCrewException.class)
public BaseErrorResponse handle_MemberCrewException(MemberCrewException e) {
log.error("[handle_MemberCrewException]", e);
return new BaseErrorResponse(e.getExceptionStatus(), e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public enum BaseExceptionResponseStatus implements ResponseStatus{
/**
* 7000 : crew 관련
*/
CANNOT_FOUND_CREW_LIST(7000, HttpStatus.BAD_REQUEST.value(), "크루 리스트를 찾을 수 없습니다.");
CANNOT_FOUND_CREW_LIST(7000, HttpStatus.BAD_REQUEST.value(), "크루 리스트를 찾을 수 없습니다."),
CANNOT_FOUND_CREW(7001, HttpStatus.BAD_REQUEST.value(), "크루를 찾을 수 없습니다."),

/**
* 8000 : Member crew 관련
*/
CANNOT_FOUND_MEMBERCREW(8000, HttpStatus.BAD_REQUEST.value(), "멤버_크루를 찾을 수 없습니다."),
CANNOT_FOUND_MEMBERCREW_LIST(7001, HttpStatus.BAD_REQUEST.value(), "멤버_크루 리스트를 찾을 수 없습니다.");


private final int code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.example.likelion12.controller;

import com.example.likelion12.common.response.BaseResponse;
import com.example.likelion12.dto.PostCrewRequest;
import com.example.likelion12.dto.PostCrewResponse;
import com.example.likelion12.dto.crew.GetCrewDetailResponse;
import com.example.likelion12.dto.crew.PostCrewRequest;
import com.example.likelion12.dto.crew.PostCrewResponse;
import com.example.likelion12.service.CrewService;
import com.example.likelion12.util.*;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@AllArgsConstructor
@RequiredArgsConstructor
@RequestMapping("/crew")
public class CrewController {

Expand All @@ -28,4 +29,16 @@ public BaseResponse<PostCrewResponse> createCrew(@RequestHeader("Authorization")
Long memberId = jwtProvider.extractIdFromHeader(authorization);
return new BaseResponse<>(crewService.createCrew(memberId, postCrewRequest));
}

/**
* 크루 상세 조회
*/
@GetMapping("")
public BaseResponse<GetCrewDetailResponse> getCrewDetail(@RequestHeader("Authorization") String authorization,
@RequestParam Long crewId){
log.info("[CrewController.getCrewDetail]");
Long memberId = jwtProvider.extractIdFromHeader(authorization);
return new BaseResponse<>(crewService.getCrewDetail(memberId, crewId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.example.likelion12.dto.HomeResponse;
import com.example.likelion12.service.HomeService;
import com.example.likelion12.util.*;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.example.likelion12.controller;

import com.example.likelion12.common.response.BaseResponse;
import com.example.likelion12.dto.LoginResponse;
import com.example.likelion12.dto.member.LoginResponse;
import com.example.likelion12.service.LoginService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.likelion12.controller;

import com.example.likelion12.common.response.BaseResponse;
import com.example.likelion12.dto.PostSignupRequest;
import com.example.likelion12.dto.PostSignupResponse;
import com.example.likelion12.dto.member.PostSignupRequest;
import com.example.likelion12.dto.member.PostSignupResponse;
import com.example.likelion12.service.MemberService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.likelion12.dto.crew;

import com.example.likelion12.domain.base.BaseRole;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class GetCrewDetailResponse {
/**
* 크루 상세 조회 response dto
*/
private BaseRole memberRole;
private String crewName;
private String crewImg;
private String activityRegionName;
private String exerciseName;
private int totalRecruits;
private int crewCost;
private List<Crews> members;
private List<Recommands> recommands;

@Getter
@AllArgsConstructor
public static class Crews{
private String memberImg;
}

@Getter
@AllArgsConstructor
public static class Recommands{
private Long crewId;
private String crewName;
private String crewImg;
private int crewCost;
private String activityRegionName;
private String exerciseName;
private int currentRecruits;
private int totalRecruits;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.likelion12.dto;
package com.example.likelion12.dto.crew;

import com.example.likelion12.domain.base.BaseGender;
import com.example.likelion12.domain.base.BaseLevel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.likelion12.dto;
package com.example.likelion12.dto.crew;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.likelion12.dto;
package com.example.likelion12.dto.member;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.likelion12.dto;
package com.example.likelion12.dto.member;

import com.example.likelion12.domain.base.BaseGender;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.likelion12.dto;
package com.example.likelion12.dto.member;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.example.likelion12.repository;

import com.example.likelion12.domain.Crew;
import com.example.likelion12.domain.base.BaseStatus;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;
Expand All @@ -12,4 +15,10 @@ public interface CrewRepository extends JpaRepository<Crew, Long> {
// "크루"가 가지고 있는 "크루_멤버"가 많은 순으로 4개 반환 (홈화면 보여주기용)
@Query("SELECT c FROM Crew c LEFT JOIN c.memberCrewList m GROUP BY c ORDER BY COUNT(m) DESC LIMIT 4")
Optional<List<Crew>> findTop4ByMemberCrewListSize();

Optional<Crew> findByCrewIdAndStatus(Long crewId, BaseStatus status);

// 특정 exercise_id 값을 가진 상위 3개의 Crew를 반환하는 쿼리
@Query("SELECT c FROM Crew c WHERE c.exercise.id = :exerciseId AND c.status = :status ORDER BY c.crewId ASC")
List<Crew> findTop3ByExerciseIdAndStatus(@Param("exerciseId") Long exerciseId, @Param("status") BaseStatus status, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.example.likelion12.repository;

import com.example.likelion12.domain.MemberCrew;
import com.example.likelion12.domain.base.BaseStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface MemberCrewRepository extends JpaRepository<MemberCrew, Long> {

Optional<MemberCrew> findByMember_MemberIdAndCrew_CrewIdAndStatus(Long memberId, Long crewId, BaseStatus status);
Optional<List<MemberCrew>> findByCrew_CrewIdAndStatus(Long crewId, BaseStatus status);
}
58 changes: 52 additions & 6 deletions src/main/java/com/example/likelion12/service/CrewService.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package com.example.likelion12.service;

import com.example.likelion12.common.exception.ActivityRegionException;
import com.example.likelion12.common.exception.ExerciseException;
import com.example.likelion12.common.exception.FacilityException;
import com.example.likelion12.common.exception.MemberException;
import com.example.likelion12.common.exception.*;
import com.example.likelion12.domain.*;
import com.example.likelion12.domain.base.BaseGender;
import com.example.likelion12.domain.base.BaseLevel;
import com.example.likelion12.domain.base.BaseStatus;
import com.example.likelion12.dto.PostCrewRequest;
import com.example.likelion12.dto.PostCrewResponse;
import com.example.likelion12.dto.crew.GetCrewDetailResponse;
import com.example.likelion12.dto.crew.PostCrewRequest;
import com.example.likelion12.dto.crew.PostCrewResponse;
import com.example.likelion12.repository.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.undo.CannotRedoException;

import java.util.List;
import java.util.stream.Collectors;

import static com.example.likelion12.common.response.status.BaseExceptionResponseStatus.*;

@Slf4j
Expand All @@ -30,6 +34,7 @@ public class CrewService {
private final CrewRepository crewRepository;
private final MemberRepository memberRepository;
private final MemberCrewService memberCrewService;
private final MemberCrewRepository memberCrewRepository;

/**
* 크루 등록
Expand Down Expand Up @@ -69,4 +74,45 @@ public PostCrewResponse createCrew(Long memberId, PostCrewRequest postCrewReques
memberCrewService.createMemberCrew(member,crew);
return new PostCrewResponse(crew.getCrewId());
}

/**
* 크루 상세 조회
*/
public GetCrewDetailResponse getCrewDetail(Long memberId, Long crewId){
log.info("[CrewService.getCrewDetail]");

Crew crew = crewRepository.findByCrewIdAndStatus(crewId, BaseStatus.ACTIVE)
.orElseThrow(()->new CrewException(CANNOT_FOUND_CREW));
MemberCrew memberCrew = memberCrewRepository.findByMember_MemberIdAndCrew_CrewIdAndStatus(memberId, crewId, BaseStatus.ACTIVE)
.orElseThrow(()-> new CrewException(CANNOT_FOUND_MEMBERCREW));

// 가입한 멤버 리스트 추출
List<MemberCrew> memberCrewList = memberCrewRepository.findByCrew_CrewIdAndStatus(crewId, BaseStatus.ACTIVE)
.orElseThrow(()-> new MemberCrewException(CANNOT_FOUND_MEMBERCREW_LIST));
// 그 멤버 중에서 사진만 추출해서 반환
List<GetCrewDetailResponse.Crews> memberImgList = memberCrewList.stream()
.map(MemberCrew -> new GetCrewDetailResponse.Crews(memberCrew.getMember().getMemberImg()))
.collect(Collectors.toList());

List<GetCrewDetailResponse.Recommands> recommandsList = crewRepository.findTop3ByExerciseIdAndStatus(
crew.getExercise().getExerciseId(),
BaseStatus.ACTIVE,
Pageable.ofSize(3)
).stream().map(crews -> new GetCrewDetailResponse.Recommands(
crews.getCrewId(),
crews.getCrewName(),
crews.getCrewImg(),
crews.getCrewCost(),
crews.getActivityRegion().getActivityRegionName(),
crews.getExercise().getExerciseName(),
crews.getMemberCrewList().size(), // Assuming this returns the current recruits count
crews.getTotalRecruits()
)).collect(Collectors.toList());

GetCrewDetailResponse getCrewDetailResponse = new GetCrewDetailResponse(memberCrew.getRole(), crew.getCrewName(),
crew.getCrewImg(), crew.getActivityRegion().getActivityRegionName(),crew.getExercise().getExerciseName(),
crew.getTotalRecruits(),crew.getCrewCost(),memberImgList,recommandsList);

return getCrewDetailResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.example.likelion12.domain.Member;
import com.example.likelion12.domain.base.BaseStatus;
import com.example.likelion12.dto.LoginResponse;
import com.example.likelion12.dto.member.LoginResponse;
import com.example.likelion12.repository.MemberRepository;
import com.example.likelion12.util.*;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.example.likelion12.domain.Member;
import com.example.likelion12.domain.base.BaseGender;
import com.example.likelion12.domain.base.BaseStatus;
import com.example.likelion12.dto.PostSignupRequest;
import com.example.likelion12.dto.PostSignupResponse;
import com.example.likelion12.dto.member.PostSignupRequest;
import com.example.likelion12.dto.member.PostSignupResponse;
import com.example.likelion12.repository.ExerciseRepository;
import com.example.likelion12.repository.MemberRepository;
import com.example.likelion12.util.*;
Expand Down

0 comments on commit 158baf9

Please sign in to comment.