Skip to content

Commit

Permalink
REFACTOR: COGO 신청 로직 변경 (#188)
Browse files Browse the repository at this point in the history
* REFACTOR: COGO 신청 로직 변경

* FEAT: Add ApplicationCleanupAspect
  • Loading branch information
oxdjww authored Nov 13, 2024
1 parent ba3b2ea commit 70ea175
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 198 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.soongsil.CoffeeChat.aspect;

import com.soongsil.CoffeeChat.repository.ApplicationRepository;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Aspect
public class ApplicationCleanupAspect {

private final ApplicationRepository applicationRepository;

public ApplicationCleanupAspect(ApplicationRepository applicationRepository) {
this.applicationRepository = applicationRepository;
}

@Scheduled(cron = "0 0 0 * * ?")
@Transactional
public void deleteExpiredApplications() {
// 매일 자정에 현재 시간보다 이전인 COGO 삭제
applicationRepository.deleteExpiredApplications();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ResponseEntity<String> handleResponseStatusException(ResponseStatusExcept

@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
}

@ExceptionHandler(CustomException.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
package com.soongsil.CoffeeChat.dto;

import java.time.LocalDate;
import java.time.LocalTime;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.querydsl.core.annotations.QueryProjection;
import com.soongsil.CoffeeChat.entity.PossibleDate;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import java.time.LocalDate;
import java.time.LocalTime;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PossibleDateCreateGetResponseDto {
@JsonProperty("possible_date_id")
private Long possibledateId;

@JsonProperty("date")
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate date;

@JsonProperty("start_time")
@Schema(type = "string", pattern = "hh:mm:ss")
private LocalTime startTime;

@JsonProperty("end_time")
@Schema(type = "string", pattern = "hh:mm:ss")
private LocalTime endTime;

@JsonIgnore

@QueryProjection
public PossibleDateCreateGetResponseDto(LocalDate date, LocalTime startTime, LocalTime endTime,
Long possibledateId) {
this.date = date;
this.startTime = startTime;
this.endTime = endTime;
this.possibledateId = possibledateId;
}

public static PossibleDateCreateGetResponseDto from(PossibleDate possibleDate) {
return PossibleDateCreateGetResponseDto.builder()
.date(possibleDate.getDate())
.startTime(possibleDate.getStartTime())
.endTime(possibleDate.getEndTime())
.possibledateId(possibleDate.getId())
.build();
}
@JsonProperty("possible_date_id")
private Long possibledateId;

@JsonProperty("date")
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate date;

@JsonProperty("start_time")
@Schema(type = "string", pattern = "hh:mm:ss")
private LocalTime startTime;

@JsonProperty("end_time")
@Schema(type = "string", pattern = "hh:mm:ss")
private LocalTime endTime;

@JsonProperty("is_active")
private boolean isActive;

@JsonIgnore

@QueryProjection
public PossibleDateCreateGetResponseDto(LocalDate date, LocalTime startTime, LocalTime endTime,
Long possibledateId, boolean isActive) {
this.date = date;
this.startTime = startTime;
this.endTime = endTime;
this.possibledateId = possibledateId;
this.isActive = isActive;
}

public static PossibleDateCreateGetResponseDto from(PossibleDate possibleDate) {
return PossibleDateCreateGetResponseDto.builder()
.date(possibleDate.getDate())
.startTime(possibleDate.getStartTime())
.endTime(possibleDate.getEndTime())
.possibledateId(possibleDate.getId())
.isActive(possibleDate.isActive())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Application {
@Column(columnDefinition = "VARCHAR(255) DEFAULT 'UNMATCHED'")
private ApplicationStatus accept;

@OneToOne
@ManyToOne
@JoinColumn(name = "possible_date_id")
private PossibleDate possibleDate;
}
89 changes: 40 additions & 49 deletions src/main/java/com/soongsil/CoffeeChat/entity/PossibleDate.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package com.soongsil.CoffeeChat.entity;

import java.time.LocalDate;
import java.time.LocalTime;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.soongsil.CoffeeChat.dto.PossibleDateCreateRequestDto;
import jakarta.persistence.*;
import lombok.*;

import jakarta.persistence.Column;
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 lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Builder
Expand All @@ -29,36 +18,38 @@
@Setter
@ToString(of = {"id", "date", "startTime", "endTime", "isActive"})
public class PossibleDate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "possible_date_id")
private Long id;

@Setter
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mentor_id")
private Mentor mentor;

@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate date;

@JsonFormat(pattern = "HH:mm") //datetimeformat은 ss까지 전부 다 받아야 오류안남
LocalTime startTime;

@JsonFormat(pattern = "HH:mm")
LocalTime endTime;

@Column
@Setter
@Builder.Default
private boolean isActive = true;

public static PossibleDate from(PossibleDateCreateRequestDto dto) {
return PossibleDate.builder()
.date(dto.getDate())
.startTime(dto.getStartTime())
.endTime(dto.getEndTime())
.isActive(true)
.build();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "possible_date_id")
private Long id;

@Setter
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mentor_id")
private Mentor mentor;

@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate date;

@JsonFormat(pattern = "HH:mm") //datetimeformat은 ss까지 전부 다 받아야 오류안남
LocalTime startTime;

@JsonFormat(pattern = "HH:mm")
LocalTime endTime;

@OneToMany(mappedBy = "possibleDate")
private List<Application> applications = new ArrayList<>();

@Column
@Setter
private boolean isActive = true;

public static PossibleDate from(PossibleDateCreateRequestDto dto) {
return PossibleDate.builder()
.date(dto.getDate())
.startTime(dto.getStartTime())
.endTime(dto.getEndTime())
.isActive(true)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package com.soongsil.CoffeeChat.repository;

import java.util.List;

import com.soongsil.CoffeeChat.entity.Application;
import com.soongsil.CoffeeChat.entity.Mentee;
import com.soongsil.CoffeeChat.entity.Mentor;
import com.soongsil.CoffeeChat.entity.PossibleDate;
import com.soongsil.CoffeeChat.enums.ApplicationStatus;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.soongsil.CoffeeChat.entity.Application;
import com.soongsil.CoffeeChat.entity.Mentor;
import java.util.List;

public interface ApplicationRepository extends JpaRepository<Application, Long> {
List<Application> findApplicationByMentor(Mentor mentor);
List<Application> findApplicationByMentee(Mentee mentee);
List<Application> findApplicationByMentor(Mentor mentor);

List<Application> findApplicationByMentee(Mentee mentee);

List<Application> findByPossibleDateAndAccept(PossibleDate possibleDate, ApplicationStatus accept);

@Modifying
@Transactional
@Query("DELETE FROM Application a WHERE a.possibleDate.date < CURRENT_DATE " +
"OR (a.possibleDate.date = CURRENT_DATE AND a.possibleDate.startTime < CURRENT_TIME)")
void deleteExpiredApplications();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.soongsil.CoffeeChat.entity.Application;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@

@RequiredArgsConstructor
public class PossibleDateRepositoryImpl implements PossibleDateRepositoryCustom {
private final JPAQueryFactory queryFactory;
private final JPAQueryFactory queryFactory;

@Override
public List<PossibleDateCreateGetResponseDto> getPossibleDatesByUsername(String username) {
return queryFactory.
select(new QPossibleDateCreateGetResponseDto(
possibleDate.date,
possibleDate.startTime,
possibleDate.endTime,
possibleDate.id.as("possibleDateId")
))
.from(user)
.join(user.mentor, mentor)
.join(mentor.possibleDates, possibleDate)
.where(user.username.eq(username).and(
possibleDate.isActive.isTrue()
))
.fetch();
}
@Override
public List<PossibleDateCreateGetResponseDto> getPossibleDatesByUsername(String username) {
return queryFactory.
select(new QPossibleDateCreateGetResponseDto(
possibleDate.date,
possibleDate.startTime,
possibleDate.endTime,
possibleDate.id.as("possibleDateId"),
possibleDate.isActive
))
.from(user)
.join(user.mentor, mentor)
.join(mentor.possibleDates, possibleDate)
.where(user.username.eq(username).and(
possibleDate.isActive.isTrue()
))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,6 @@ public ApplicationCreateResponseDto createApplication(ApplicationCreateRequestDt
}
log.info("[*] Found possibleDate is not preempted");

// 가능시간 비활성화
System.out.println("possibleDate.getId() = " + requestedPossibleDate.getId());
requestedPossibleDate.setActive(false);
possibleDateRepository.save(requestedPossibleDate);
log.info(
"[*] PossibleDate(id:" + requestedPossibleDate.getId() + ") is just preempted: "
+ requestedPossibleDate.isActive()
);

// COGO 저장
User user = findUserByUsername(userName);
Mentee findMentee = user.getMentee();
Expand Down Expand Up @@ -284,6 +275,16 @@ public ApplicationMatchResponseDto updateApplicationStatus(Long applicationId, S
ApplicationMatchResponseDto responseDto = ApplicationMatchResponseDto.builder()
.applicationId(applicationId)
.build();
PossibleDate matchedPossibleDate = findApplication.getPossibleDate();

// 가능시간 비활성화
System.out.println("possibleDate.getId() = " + matchedPossibleDate.getId());
matchedPossibleDate.setActive(false);
possibleDateRepository.save(matchedPossibleDate);
log.info(
"[*] PossibleDate(id:" + matchedPossibleDate.getId() + ") is just matched with: "
+ findApplication.getId()
);

switch (decision) {
case "reject" -> {
Expand All @@ -294,6 +295,17 @@ public ApplicationMatchResponseDto updateApplicationStatus(Long applicationId, S
case "accept" -> {
findApplication.setAccept(MATCHED);
responseDto.setStatus(MATCHED.name());

// 매치된 가능시간(PossibleDate)을 가진 다른 'UNMATCHED' 상태의 Application 엔티티 삭제
List<Application> unmatchedApplications = applicationRepository
.findByPossibleDateAndAccept(matchedPossibleDate, ApplicationStatus.UNMATCHED);

unmatchedApplications.forEach(application -> {
// application.setAccept(UNMATCHED);
applicationRepository.delete(application);
log.info("[*] Unmatched Application(id:" + application.getId() + ") with PossibleDate(id:"
+ matchedPossibleDate.getId() + ") has been UNMATCHED");
});
}
default -> throw new CustomException(
INVALID_MATCH_STATUS.getHttpStatusCode(),
Expand Down
Loading

0 comments on commit 70ea175

Please sign in to comment.