Skip to content

Commit

Permalink
refactor: 모임 배너 조회 로직 개선 (#347)
Browse files Browse the repository at this point in the history
* feat: Map 방어 코드 작성

* refactor: 배너 조회 로직 쿼리 개선
  • Loading branch information
mikekks authored Aug 30, 2024
1 parent 11771c3 commit ae8fb04
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,45 @@ public Applies(List<Apply> applies) {
.collect(Collectors.groupingBy(Apply::getMeetingId));
}

public int getAppliedCount(Integer meetingId){
public int getAppliedCount(Integer meetingId) {
List<Apply> applies = appliesMap.get(meetingId);

if(applies == null){
if (applies == null) {
return 0;
}
return applies.size();
}

public long getApprovedCount(Integer meetingId) {
List<Apply> applies = appliesMap.get(meetingId);

if (applies == null) {
return 0;
}

return appliesMap.get(meetingId).stream()
.filter(apply -> apply.getStatus().equals(EnApplyStatus.APPROVE))
.count();
}

public Boolean isApply(Integer meetingId, Integer userId) {
List<Apply> applies = appliesMap.get(meetingId);

if (applies == null) {
return false;
}

return appliesMap.get(meetingId).stream()
.anyMatch(apply -> apply.getUserId().equals(userId));
}

public Boolean isApproved(Integer meetingId, Integer userId) {
List<Apply> applies = appliesMap.get(meetingId);

if (applies == null) {
return false;
}

return appliesMap.get(meetingId).stream()
.anyMatch(apply -> apply.getUserId().equals(userId)
&& apply.getStatus().equals(EnApplyStatus.APPROVE));
Expand Down
182 changes: 92 additions & 90 deletions main/src/main/java/org/sopt/makers/crew/main/entity/apply/Apply.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

import java.time.LocalDateTime;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -36,94 +38,94 @@
@Table(name = "apply")
public class Apply {

/**
* Primary Key
*/
@Id
@GeneratedValue(strategy = IDENTITY)
private Integer id;

/**
* 지원 타입
*/
@Column(name = "type", nullable = false, columnDefinition = "integer default 0")
@Convert(converter = ApplyTypeConverter.class)
private EnApplyType type;

/**
* 지원한 모임
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "meetingId", nullable = false)
private Meeting meeting;

/**
* 지원한 모임 ID
*/
@Column(insertable = false, updatable = false)
private Integer meetingId;

/**
* 지원자
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false)
private User user;

/**
* 지원자 ID
*/
@Column(insertable = false, updatable = false)
private Integer userId;

/**
* 지원 동기
*/
@Column(name = "content", nullable = false)
private String content;

/**
* 지원한 날짜
*/
@Column(name = "appliedDate", nullable = false, columnDefinition = "TIMESTAMP")
@CreatedDate
private LocalDateTime appliedDate;

/**
* 지원 상태
*/
@Column(name = "status", nullable = false, columnDefinition = "integer default 0")
@Convert(converter = ApplyStatusConverter.class)
private EnApplyStatus status;

@Builder
public Apply(EnApplyType type, Meeting meeting, Integer meetingId, User user, Integer userId,
String content) {
this.type = type;
this.meeting = meeting;
this.meetingId = meetingId;
this.user = user;
this.userId = userId;
this.content = content;
this.appliedDate = LocalDateTime.now();
this.status = EnApplyStatus.WAITING;
}

public void updateApplyStatus(EnApplyStatus status) {
this.status = status;
}

public void validateDuplicateUpdateApplyStatus(EnApplyStatus updatedApplyStatus){
if(updatedApplyStatus.equals(this.getStatus())){
throw new BadRequestException(ALREADY_PROCESSED_APPLY.getErrorCode());
}
}

public String getContent(Integer requestUserId){
if(!this.userId.equals(requestUserId)){
return "";
}

return this.content;
}
/**
* Primary Key
*/
@Id
@GeneratedValue(strategy = IDENTITY)
private Integer id;

/**
* 지원 타입
*/
@Column(name = "type", nullable = false, columnDefinition = "integer default 0")
@Convert(converter = ApplyTypeConverter.class)
private EnApplyType type;

/**
* 지원한 모임
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "meetingId", nullable = false)
private Meeting meeting;

/**
* 지원한 모임 ID
*/
@Column(insertable = false, updatable = false)
private Integer meetingId;

/**
* 지원자
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false)
private User user;

/**
* 지원자 ID
*/
@Column(insertable = false, updatable = false)
private Integer userId;

/**
* 지원 동기
*/
@Column(name = "content", nullable = false)
private String content;

/**
* 지원한 날짜
*/
@Column(name = "appliedDate", nullable = false, columnDefinition = "TIMESTAMP")
@CreatedDate
private LocalDateTime appliedDate;

/**
* 지원 상태
*/
@Column(name = "status", nullable = false, columnDefinition = "integer default 0")
@Convert(converter = ApplyStatusConverter.class)
private EnApplyStatus status;

@Builder
public Apply(EnApplyType type, Meeting meeting, Integer meetingId, User user, Integer userId,
String content) {
this.type = type;
this.meeting = meeting;
this.meetingId = meetingId;
this.user = user;
this.userId = userId;
this.content = content;
this.appliedDate = LocalDateTime.now();
this.status = EnApplyStatus.WAITING;
}

public void updateApplyStatus(EnApplyStatus status) {
this.status = status;
}

public void validateDuplicateUpdateApplyStatus(EnApplyStatus updatedApplyStatus) {
if (updatedApplyStatus.equals(this.getStatus())) {
throw new BadRequestException(ALREADY_PROCESSED_APPLY.getErrorCode());
}
}

public String getContent(Integer requestUserId) {
if (!this.userId.equals(requestUserId)) {
return "";
}

return this.content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
import static org.sopt.makers.crew.main.common.exception.ErrorStatus.NOT_FOUND_MEETING;

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

import org.sopt.makers.crew.main.common.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);

Optional<Meeting> findById(Integer meetingId);

default Meeting findByIdOrThrow(Integer meetingId) {
return findById(meetingId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_MEETING.getErrorCode()));
}

@Query("SELECT m FROM Meeting m JOIN FETCH m.user ORDER BY m.id DESC LIMIT 20")
List<Meeting> findTop20ByOrderByIdDesc();
}
Loading

0 comments on commit ae8fb04

Please sign in to comment.