Skip to content

Commit

Permalink
Merge pull request #44 from goormthon-Univ/develop
Browse files Browse the repository at this point in the history
[develop] main merge
  • Loading branch information
JoongHyun-Kim authored Mar 23, 2024
2 parents 8847c29 + 07c9e53 commit dac5689
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public BaseResponse<NotificationListResponse> getNotificationList() throws BaseE
long activeParticipantsCount = notification.getRecruitment().getParticipants().stream()
.filter(participant -> participant.getStatus().equals(ACTIVE)).count();
return new NotificationDto(notification.getNotificationIdx(), notification.getRecruitment().getName(), notification.getRecruitment().getContactUrl(),
notification.getRecruitment().getLeader().getNickname(), activeParticipantsCount+1,
notification.getRecruitment().getLeader().getNickname(), (int) activeParticipantsCount+1,
notification.getRecruitment().getParticipantLimit(), notification.getRecruitment().getDescription());}).toList();
return new BaseResponse<>(new NotificationListResponse(notificationDtoList));
} catch (BaseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public record NotificationDto(Long notificationIdx,
String name,
String contactUrl,
String leader,
long participantNumber,
int participantNumber,
Integer participantLimit,
String description) {}
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,25 @@ public BaseResponse<RecruitmentListResponse> getRecruitmentList(String type, boo
try {
Long userIdx = authService.getUserIdx();
List<RecruitmentDto> recruitmentList = new ArrayList<>();
if (isParticipant) { // 참여중인 띱 목록 조회 //TODO: Participant active 상태인것만
if (userIdx != null) {
if (isParticipant) { // 참여중인 띱 목록 조회
if (userIdx != null) { // 회원
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
// 리더로 있는 recruitment 목록 조회
Stream<Recruitment> leaderRecruitmentStream = user.getRecruitments().stream();

// 참여자로 있는 recruitment 목록 조회
Stream<Recruitment> participantRecruitmentStream = user.getParticipants().stream()
.filter(participant -> participant.getStatus().equals(ACTIVE))
.map(Participant::getRecruitment);

List<RecruitmentDto> sortedRecruitmentList = Stream.concat(leaderRecruitmentStream, participantRecruitmentStream)
.sorted(Comparator.comparing(Recruitment::getCreatedDate).reversed())
.map(recruitment -> new RecruitmentDto(recruitment.getRecruitmentIdx(), recruitment.getType().getDescription(), recruitment.getName(),
recruitment.getLeader().getNickname(), recruitment.getParticipants().size(), recruitment.getParticipantLimit(), recruitment.getDescription(),
recruitment.getLeader().getNickname(), (int) getActiveParticipantCount(recruitment)+1, recruitment.getParticipantLimit(), recruitment.getDescription(),
recruitment.getLeader().equals(user), recruitment.getStatus().equals(DONE))).toList();
recruitmentList.addAll(sortedRecruitmentList);
} else { // 비회원
recruitmentList = null;
}
} else {
if (type == null) { // 모집중인 띱 목록 조회
Expand All @@ -97,6 +100,7 @@ public BaseResponse<RecruitmentListResponse> getRecruitmentList(String type, boo
}
}

// recruiting 상태 모집글 목록 조회
private List<RecruitmentDto> getRecruitmentList(List<Recruitment> recruitmentRepositoryList, Long userIdx) throws BaseException {
List<RecruitmentDto> recruitmentList;
User user = null;
Expand All @@ -112,7 +116,7 @@ private List<RecruitmentDto> getRecruitmentList(List<Recruitment> recruitmentRep
isLeader = recruitment.getLeader().equals(finalUser);
}
return new RecruitmentDto(recruitment.getRecruitmentIdx(), recruitment.getType().getDescription(), recruitment.getName(),
recruitment.getLeader().getNickname(), recruitment.getParticipants().size(), recruitment.getParticipantLimit(), recruitment.getDescription(),
recruitment.getLeader().getNickname(), (int) getActiveParticipantCount(recruitment)+1, recruitment.getParticipantLimit(), recruitment.getDescription(),
isLeader, false);
}).toList();
return recruitmentList;
Expand All @@ -130,8 +134,8 @@ public BaseResponse<RecruitResponse> getRecruitment(Long recruitmentIdx) throws
isLeader = recruitment.getLeader().equals(user);
}
RecruitmentDetailDto recruitmentDetailDto = new RecruitmentDetailDto(recruitment.getRecruitmentIdx(), recruitment.getType().getDescription(), recruitment.getName(),
recruitment.getLeader().getNickname(), recruitment.getParticipants().size()+1, recruitment.getParticipantLimit(), recruitment.getDescription(),
isLeader, recruitment.getStatus().equals(RECRUITING)); //TODO: participantNumber 구할 때 participant 상태가 active인 것만 세기
recruitment.getLeader().getNickname(), (int) getActiveParticipantCount(recruitment)+1, recruitment.getParticipantLimit(), recruitment.getDescription(),
isLeader, recruitment.getStatus().equals(RECRUITING));
Integer commentCount = recruitment.getComments().size();
List<CommentDto> commentList = recruitment.getComments().stream()
.map(comment -> new CommentDto(comment.getCommentIdx(), comment.getWriter().getNickname(), comment.getCreatedDate(), comment.getContent())).toList();
Expand All @@ -155,6 +159,8 @@ public BaseResponse<String> editRecruitment(Long recruitmentIdx, RecruitmentEdit
validateWriter(user, recruitment);
validateRecruitmentStatus(recruitment.getStatus().equals(DONE), NOT_RECRUITING_STATUS);

getActiveParticipantCount(recruitment);

if (recruitmentEditRequest.name() != null) {
if (!recruitmentEditRequest.name().equals("") && !recruitmentEditRequest.name().equals(" "))
recruitment.modifyName(recruitmentEditRequest.name());
Expand All @@ -166,9 +172,9 @@ public BaseResponse<String> editRecruitment(Long recruitmentIdx, RecruitmentEdit
else throw new BaseException(BLANK_RECRUITMENT_TYPE);
}
if (recruitmentEditRequest.participantLimit() != null) {
if (recruitmentEditRequest.participantLimit() < recruitment.getParticipants().size()+1) //TODO: participantNumber 구할 때 participant 상태가 active인 것만 세기
if (recruitmentEditRequest.participantLimit() < (int) getActiveParticipantCount(recruitment)+1)
throw new BaseException(LARGER_THAN_CURRENT_PARTICIPANT);
else if (recruitmentEditRequest.participantLimit() == recruitment.getParticipants().size()) { //TODO: participantNumber 구할 때 participant 상태가 active인 것만 세기
else if (recruitmentEditRequest.participantLimit() == (int) getActiveParticipantCount(recruitment)+1) {
recruitment.setStatus(DONE);
} else recruitment.modifyParticipantLimit(recruitmentEditRequest.participantLimit());
}
Expand All @@ -191,6 +197,12 @@ else if (recruitmentEditRequest.participantLimit() == recruitment.getParticipant
}
}

private static long getActiveParticipantCount(Recruitment recruitment) {
return recruitment.getParticipants().stream()
.filter(participant -> participant.getStatus().equals(ACTIVE))
.count();
}

// [멤버] 띱 참여하기
@Transactional(rollbackFor = Exception.class)
public BaseResponse<String> participate(Long recruitmentIdx) throws BaseException {
Expand All @@ -199,14 +211,14 @@ public BaseResponse<String> participate(Long recruitmentIdx) throws BaseExceptio
Recruitment recruitment = recruitmentRepository.findById(recruitmentIdx).orElseThrow(() -> new BaseException(INVALID_RECRUITMENT_IDX));
validateLeaderRole(recruitment.getLeader().equals(user), LEADER_ROLE);

if (recruitment.getParticipants().size()+2 == recruitment.getParticipantLimit()) { // 멤버 + 리더 + 현재 참여하려는 유저 //TODO: participantNumber 구할 때 participant 상태가 active인 것만 세기
if ((int) getActiveParticipantCount(recruitment)+2 == recruitment.getParticipantLimit()) { // 멤버 + 리더 + 현재 참여하려는 유저
createParticipant(user, recruitment);

recruitment.setStatus(DONE);
recruitmentRepository.save(recruitment);

createNotifications(recruitment);
} else if (recruitment.getParticipants().size()+2 > recruitment.getParticipantLimit()) { //TODO: participantNumber 구할 때 participant 상태가 active인 것만 세기
} else if ((int) getActiveParticipantCount(recruitment)+2 > recruitment.getParticipantLimit()) {
throw new BaseException(ALREADY_DONE_RECRUITMENT);
} else createParticipant(user, recruitment);
return new BaseResponse<>(SUCCESS);
Expand Down Expand Up @@ -286,6 +298,52 @@ public BaseResponse<String> withdrawRecruitment(Long recruitmentIdx) throws Base
}
}

// [홈] 모집중인 띱 목록 조회(3개)
public BaseResponse<RecruitmentListResponse> getRecruitingList() throws BaseException {
try {
Long userIdx = authService.getUserIdx();
List<RecruitmentDto> recruitmentList = getRecruitmentList(recruitmentRepository.findTop3ByStatusOrderByCreatedDateDesc(RECRUITING), userIdx);
return new BaseResponse<>(new RecruitmentListResponse(recruitmentList));
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(INTERNAL_SERVER_ERROR);
}
}

// [홈] 참여중인 띱 목록 조회(3개)
public BaseResponse<RecruitmentListResponse> getParticipatingList() throws BaseException {
try {
Long userIdx = authService.getUserIdx();
List<RecruitmentDto> recruitmentList = new ArrayList<>();
if (userIdx != null) { // 회원
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
// 리더로 있는 recruitment 목록 조회
Stream<Recruitment> leaderRecruitmentStream = user.getRecruitments().stream();

// 참여자로 있는 recruitment 목록 조회
Stream<Recruitment> participantRecruitmentStream = user.getParticipants().stream()
.filter(participant -> participant.getStatus().equals(ACTIVE))
.map(Participant::getRecruitment);

List<RecruitmentDto> sortedRecruitmentList = Stream.concat(leaderRecruitmentStream, participantRecruitmentStream)
.sorted(Comparator.comparing(Recruitment::getCreatedDate).reversed())
.limit(3) // 상위 3개
.map(recruitment -> new RecruitmentDto(recruitment.getRecruitmentIdx(), recruitment.getType().getDescription(), recruitment.getName(),
recruitment.getLeader().getNickname(), (int) getActiveParticipantCount(recruitment)+1, recruitment.getParticipantLimit(), recruitment.getDescription(),
recruitment.getLeader().equals(user), recruitment.getStatus().equals(DONE))).toList();
recruitmentList.addAll(sortedRecruitmentList);
} else { // 비회원
recruitmentList = null;
}
return new BaseResponse<>(new RecruitmentListResponse(recruitmentList));
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(INTERNAL_SERVER_ERROR);
}
}

private static void validateLeaderRole(boolean recruitment, BaseResponseStatus responseStatus) throws BaseException {
if (recruitment) throw new BaseException(responseStatus);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public record RecruitmentDetailDto(Long recruitmentIdx,
String type,
String name,
String leader,
Integer participantNumber,
int participantNumber,
Integer participantLimit,
String description,
boolean isLeader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public record RecruitmentDto(Long recruitmentIdx,
String type,
String name,
String leader,
Integer participantNumber,
int participantNumber,
Integer participantLimit,
String description,
boolean isLeader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,24 @@ public BaseResponse<?> withdrawRecruitment(@PathVariable Long recruitmentIdx) {
return new BaseResponse<>(e.getStatus());
}
}

// 모집중인 띱 목록 조회(3개)
@GetMapping("/recruiting")
public BaseResponse<RecruitmentListResponse> getRecruitingList() {
try {
return recruitmentService.getRecruitingList();
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

// 참여중인 띱 목록 조회(3개)
@GetMapping("/participating")
public BaseResponse<RecruitmentListResponse> getParticipatingList() {
try {
return recruitmentService.getParticipatingList();
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface RecruitmentRepository extends JpaRepository<Recruitment, Long>

List<Recruitment> findByStatusOrderByCreatedDateDesc(String status);
List<Recruitment> findByTypeAndStatusEqualsOrderByCreatedDateDesc(RecruitType type, String status);
List<Recruitment> findTop3ByStatusOrderByCreatedDateDesc(String status);
}
5 changes: 1 addition & 4 deletions src/main/java/com/hatcher/haemo/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.Where;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,12 +36,10 @@ public class User extends BaseEntity {
@Column(nullable = false)
private String nickname;

@OneToMany(mappedBy = "leader")
@Where(clause = "status = 'ACTIVE'")
@OneToMany(mappedBy = "leader", fetch = FetchType.EAGER)
private List<Recruitment> recruitments = new ArrayList<>(); // 해당 user가 leader로 있는 recruitment list

@OneToMany(mappedBy = "writer")
@Where(clause = "status = 'ACTIVE'")
private List<Comment> comments = new ArrayList<>();

@OneToMany(mappedBy = "participant")
Expand Down

0 comments on commit dac5689

Please sign in to comment.