Skip to content

Commit

Permalink
v2.2.0
Browse files Browse the repository at this point in the history
v2.2.0
  • Loading branch information
mikekks authored Nov 8, 2024
2 parents f554e8d + 91f2139 commit 73fd2e9
Show file tree
Hide file tree
Showing 29 changed files with 1,324 additions and 551 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,47 @@

public interface ApplyRepository extends JpaRepository<Apply, Integer>, ApplySearchRepository {

@Query("select a from Apply a join fetch a.meeting m where a.userId = :userId and a.status = :statusValue")
List<Apply> findAllByUserIdAndStatus(@Param("userId") Integer userId,
@Param("statusValue") EnApplyStatus statusValue);

@Query("select a from Apply a join fetch a.meeting m join fetch m.user u where a.userId = :userId")
List<Apply> findAllByUserId(@Param("userId") Integer userId);

@Query("select a "
+ "from Apply a "
+ "join fetch a.user u "
+ "where a.meetingId = :meetingId "
+ "and a.status in :statuses order by :order")
List<Apply> findAllByMeetingIdWithUser(@Param("meetingId") Integer meetingId, @Param("statuses") List<EnApplyStatus> statuses, @Param("order") String order);

List<Apply> findAllByMeetingIdAndStatus(Integer meetingId, EnApplyStatus statusValue);

List<Apply> findAllByMeetingId(Integer meetingId);

List<Apply> findAllByMeetingIdIn(List<Integer> meetingIds);

boolean existsByMeetingIdAndUserId(Integer meetingId, Integer userId);

@Transactional
@Modifying
@Query("delete from Apply a where a.meeting.id = :meetingId and a.userId = :userId")
void deleteByMeetingIdAndUserId(@Param("meetingId") Integer meetingId, @Param("userId") Integer userId);

default Apply findByIdOrThrow(Integer applyId) {
return findById(applyId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_APPLY.getErrorCode()));
}

@Modifying(clearAutomatically = true)
@Transactional
@Query("DELETE FROM Apply a WHERE a.meetingId = :meetingId")
void deleteAllByMeetingIdQuery(Integer meetingId);
@Query("select a from Apply a join fetch a.meeting m where a.userId = :userId and a.status = :statusValue")
List<Apply> findAllByUserIdAndStatus(@Param("userId") Integer userId,
@Param("statusValue") EnApplyStatus statusValue);

@Query("select a "
+ "from Apply a "
+ "join fetch a.meeting m "
+ "join fetch m.user u "
+ "where a.userId = :userId "
+ "ORDER BY a.id DESC ")
List<Apply> findAllByUserIdOrderByIdDesc(@Param("userId") Integer userId);

@Query("select a "
+ "from Apply a "
+ "join fetch a.user u "
+ "where a.meetingId = :meetingId "
+ "and a.status in :statuses order by :order")
List<Apply> findAllByMeetingIdWithUser(@Param("meetingId") Integer meetingId,
@Param("statuses") List<EnApplyStatus> statuses, @Param("order") String order);

List<Apply> findAllByMeetingIdAndStatus(Integer meetingId, EnApplyStatus statusValue);

List<Apply> findAllByMeetingId(Integer meetingId);

List<Apply> findAllByMeetingIdIn(List<Integer> meetingIds);

boolean existsByMeetingIdAndUserId(Integer meetingId, Integer userId);

@Transactional
@Modifying(clearAutomatically = true)
@Query("delete from Apply a where a.meeting.id = :meetingId and a.userId = :userId")
void deleteByMeetingIdAndUserId(@Param("meetingId") Integer meetingId, @Param("userId") Integer userId);

default Apply findByIdOrThrow(Integer applyId) {
return findById(applyId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_APPLY.getErrorCode()));
}

@Modifying(clearAutomatically = true)
@Transactional
@Query("DELETE FROM Apply a WHERE a.meetingId = :meetingId")
void deleteAllByMeetingIdQuery(Integer meetingId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.sopt.makers.crew.main.entity.meeting;

import static org.sopt.makers.crew.main.global.exception.ErrorStatus.*;

import java.util.Objects;

import org.sopt.makers.crew.main.entity.common.BaseTimeEntity;
import org.sopt.makers.crew.main.entity.user.User;
import org.sopt.makers.crew.main.global.exception.BadRequestException;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "co_leader")
public class CoLeader extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "meetingId")
@NotNull
private Meeting meeting;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId")
@NotNull
private User user;

@Builder
private CoLeader(Meeting meeting, User user) {
if (Objects.equals(meeting.getUserId(), user.getId())) {
throw new BadRequestException(LEADER_CANNOT_BE_CO_LEADER_APPLY.getErrorCode());
}
this.meeting = meeting;
this.user = user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.sopt.makers.crew.main.entity.meeting;

import java.util.List;

import org.sopt.makers.crew.main.entity.user.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CoLeaderRepository extends JpaRepository<CoLeader, Integer> {

void deleteAllByMeetingId(Integer meetingId);

List<CoLeader> findAllByMeetingId(Integer meetingId);

List<CoLeader> findAllByMeetingIdIn(List<Integer> meetingId);

List<CoLeader> findAllByUserId(Integer userId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.sopt.makers.crew.main.entity.meeting;

import static org.sopt.makers.crew.main.global.exception.ErrorStatus.*;

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

import org.sopt.makers.crew.main.global.exception.BadRequestException;

public class CoLeaders {
/**
* Key : MeetingId
* Value : 해당 모임의 공동 모임장 목록
*
* @implNote : List 내에 있는 CoLeader 객체는 fetch join 으로 다른 객체를 불러오지 않은 상태
* @implNote : 해당 자료형을 사용할 때는 'isCoLeaderPresent' 메서드 사용 적극 권장
*
* */
private final Map<Integer, List<CoLeader>> coLeadersMap;

public CoLeaders(List<CoLeader> coLeaders) {
this.coLeadersMap = coLeaders.stream()
.collect(Collectors.groupingBy(coLeader -> coLeader.getMeeting().getId()));
}

public void validateCoLeader(Integer meetingId, Integer requestUserId) {
if (isCoLeader(meetingId, requestUserId)) {
throw new BadRequestException(CO_LEADER_CANNOT_APPLY.getErrorCode());
}
}

public boolean isCoLeader(Integer meetingId, Integer requestUserId) {
if (!isCoLeaderPresent(meetingId)) {
return false;
}

return coLeadersMap.get(meetingId).stream()
.anyMatch(coLeader -> coLeader.getUser().getId().equals(requestUserId));
}

public List<CoLeader> getCoLeaders(Integer meetingId) {
if (!isCoLeaderPresent(meetingId)) {
return Collections.emptyList();
}

return Collections.unmodifiableList(coLeadersMap.get(meetingId));
}

private boolean isCoLeaderPresent(Integer meetingId) {
return coLeadersMap.containsKey(meetingId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ public void validateMeetingCreator(Integer requestUserId) {
}
}

public void validateIsNotMeetingLeader(Integer requestUserId) {
if (checkMeetingLeader(requestUserId)) {
throw new BadRequestException(LEADER_CANNOT_APPLY.getErrorCode());
}
}

public Boolean checkMeetingLeader(Integer userId) {
return this.userId.equals(userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
import java.util.List;

import org.sopt.makers.crew.main.global.exception.BadRequestException;
import org.sopt.makers.crew.main.entity.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface MeetingRepository extends JpaRepository<Meeting, Integer>, MeetingSearchRepository {

List<Meeting> findAllByUserId(Integer userId);

List<Meeting> findAllByUser(User user);
/**
* @implSpec : 특정 유저가 모임장이거나 공동모임장인 모임을 최근에 만들어진 순으로 조회한다.
* **/
@Query("SELECT m "
+ "FROM Meeting m "
+ "JOIN fetch m.user "
+ "WHERE m.user.id =:userId "
+ "OR m.id IN (:coLeaderMeetingIds)"
+ "ORDER BY m.id DESC ")
List<Meeting> findAllByUserIdOrIdInWithUser(Integer userId, List<Integer> coLeaderMeetingIds);

default Meeting findByIdOrThrow(Integer meetingId) {
return findById(meetingId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import static org.sopt.makers.crew.main.global.exception.ErrorStatus.*;

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

import org.sopt.makers.crew.main.global.exception.NotFoundException;
import org.sopt.makers.crew.main.global.exception.UnAuthorizedException;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -16,4 +18,19 @@ default User findByIdOrThrow(Integer userId) {
.orElseThrow(() -> new UnAuthorizedException(UNAUTHORIZED_USER.getErrorCode()));
}

List<User> findAllByIdIn(List<Integer> userIds);

default List<User> findAllByIdInOrThrow(List<Integer> userIds) {
List<User> users = findAllByIdIn(userIds);
List<Integer> foundUserIds = users.stream()
.map(User::getId)
.toList();

if (!foundUserIds.containsAll(userIds)) {
throw new NotFoundException(NOT_FOUND_USER.getErrorCode());
}

return users;
}

}
Loading

0 comments on commit 73fd2e9

Please sign in to comment.