Skip to content

Commit

Permalink
Merge pull request #47 from Likelion12/22-be-크루참여하기
Browse files Browse the repository at this point in the history
Feat : 크루참여하기
  • Loading branch information
hd0rable authored Aug 2, 2024
2 parents 158baf9 + e3e76ee commit 0f2b5f1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ public enum BaseExceptionResponseStatus implements ResponseStatus{
*/
CANNOT_FOUND_CREW_LIST(7000, HttpStatus.BAD_REQUEST.value(), "크루 리스트를 찾을 수 없습니다."),
CANNOT_FOUND_CREW(7001, HttpStatus.BAD_REQUEST.value(), "크루를 찾을 수 없습니다."),
ALREADY_FULL_CREW(7002, HttpStatus.BAD_REQUEST.value(), "더이상 크루에 참여하실 수 없습니다."),

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


private final int code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ public BaseResponse<GetCrewDetailResponse> getCrewDetail(@RequestHeader("Authori
return new BaseResponse<>(crewService.getCrewDetail(memberId, crewId));
}

/**
* 크루 참여하기
*/
@PostMapping("/join")
public BaseResponse<Void> joinCrew(@RequestHeader("Authorization") String authorization,
@RequestParam Long crewId){
log.info("[CrewController.joinCrew]");
Long memberId = jwtProvider.extractIdFromHeader(authorization);
crewService.joinCrew(memberId,crewId);
return new BaseResponse<>(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class GetCrewDetailResponse {
private String exerciseName;
private int totalRecruits;
private int crewCost;
private String comment;
private List<Crews> members;
private List<Recommands> recommands;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ 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);
boolean existsByMember_MemberIdAndCrew_CrewIdAndStatus(Long memberId, Long crewId, BaseStatus status);
}
42 changes: 39 additions & 3 deletions src/main/java/com/example/likelion12/service/CrewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.BaseRole;
import com.example.likelion12.domain.base.BaseStatus;
import com.example.likelion12.dto.crew.GetCrewDetailResponse;
import com.example.likelion12.dto.crew.PostCrewRequest;
Expand All @@ -12,6 +13,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -71,7 +73,7 @@ public PostCrewResponse createCrew(Long memberId, PostCrewRequest postCrewReques
,gender,level,activityRegion,facility, exercise, BaseStatus.ACTIVE);
crewRepository.save(crew);
//크루를 만든 사람이 CAPTAIN 이 되도록
memberCrewService.createMemberCrew(member,crew);
memberCrewService.createMemberCaptain(member,crew);
return new PostCrewResponse(crew.getCrewId());
}

Expand Down Expand Up @@ -105,14 +107,48 @@ public GetCrewDetailResponse getCrewDetail(Long memberId, Long crewId){
crews.getCrewCost(),
crews.getActivityRegion().getActivityRegionName(),
crews.getExercise().getExerciseName(),
crews.getMemberCrewList().size(), // Assuming this returns the current recruits count
crews.getMemberCrewList().size(),
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);
crew.getTotalRecruits(),crew.getCrewCost(),crew.getComment(),memberImgList,recommandsList);

return getCrewDetailResponse;
}

/**
* 크루 참여하기
*/
@Transactional
public void joinCrew(Long memberId, Long crewId){
log.info("[CrewService.joinCrew]");
// 참여하려는 member 찾기
Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE)
.orElseThrow(()-> new MemberException(CANNOT_FOUND_MEMBER));

//크루 아이디로 참여하려는 크루 찾기
if(memberCrewRepository.existsByMember_MemberIdAndCrew_CrewIdAndStatus(memberId,crewId, BaseStatus.ACTIVE)){
throw new MemberCrewException(ALREADY_EXIST);
}
Crew crew = crewRepository.findByCrewIdAndStatus(crewId, BaseStatus.ACTIVE)
.orElseThrow(()->new CrewException(CANNOT_FOUND_CREW));

// 참여하려는 크루의 총 모집 인원 확인하기
int totalRecruits = crew.getTotalRecruits();

// 현재 참여중인 크루원 수 확인하기
List<MemberCrew> memberCrewList = memberCrewRepository.findByCrew_CrewIdAndStatus(crewId, BaseStatus.ACTIVE)
.orElseThrow(()->new MemberCrewException(CANNOT_FOUND_MEMBERCREW_LIST));
int currentCrews = memberCrewList.size();

if(totalRecruits > currentCrews){
// 모집인원이 현재인원보다 많으니까 가입 가능
memberCrewService.createMemberCrew(member,crew); // member_crew 테이블에 crew 로 저장
}else{
// 같으면 전원 모집인 경우라서 가입 불가능
throw new CrewException(ALREADY_FULL_CREW);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ public class MemberCrewService {
* 크루 등록 시 크루 만든 사람이 CAPTAIN 이 되도록
*/
@Transactional
public void createMemberCaptain(Member member, Crew crew){
log.info("[MemberCrewService.createMemberCaptain]");
MemberCrew memberCrew = new MemberCrew(BaseRole.CAPTAIN,crew,member, BaseStatus.ACTIVE);
memberCrewRepository.save(memberCrew);
}

/**
* 크루 참여 시 CREW 로 들어가도록
*/
@Transactional
public void createMemberCrew(Member member, Crew crew){
log.info("[MemberCrewService.createMemberCrew]");
MemberCrew memberCrew = new MemberCrew(BaseRole.CAPTAIN,crew,member, BaseStatus.ACTIVE);
MemberCrew memberCrew = new MemberCrew(BaseRole.CREW ,crew,member, BaseStatus.ACTIVE);
memberCrewRepository.save(memberCrew);
}
}

0 comments on commit 0f2b5f1

Please sign in to comment.