Skip to content

Commit

Permalink
🔗 :: (#634) application attachment 구조 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
tedsoftj1123 authored Apr 15, 2024
2 parents c64a390 + c24a61e commit c0367c5
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface CommandApplicationPort {
void updateFieldTrainDate(LocalDate startDate, LocalDate endDate, List<Long> applicationIds);

void saveAllApplications(List<Application> applications);

void deleteAllAttachmentByApplicationId(Long applicationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void execute(Long applicationId, List<AttachmentRequest> attachmentReques
ApplicationStatus.REJECTED, ApplicationStatus.REQUESTED
);

commandApplicationPort.deleteAllAttachmentByApplicationId(applicationId);
commandApplicationPort.saveApplication(
application.reapply(ApplicationAttachment.from(attachmentRequests))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List<ApplicationVO> queryApplicationByConditions(ApplicationFilter applic
.join(applicationEntity.student, studentEntity)
.join(applicationEntity.recruitment, recruitmentEntity)
.join(recruitmentEntity.company, companyEntity)
.leftJoin(applicationEntity.attachments, applicationAttachmentEntity)
.leftJoin(applicationEntity.applicationAttachments, applicationAttachmentEntity)
.where(
eqRecruitmentId(applicationFilter.getRecruitmentId()),
eqStudentId(applicationFilter.getStudentId()),
Expand Down Expand Up @@ -297,6 +297,13 @@ public void saveAllApplications(List<Application> applications) {
);
}

@Override
public void deleteAllAttachmentByApplicationId(Long applicationId) {
queryFactory.delete(applicationAttachmentEntity)
.where(applicationAttachmentEntity.application.id.eq(applicationId))
.execute();
}

//==conditions==//

private BooleanExpression eqRecruitmentId(Long recruitmentId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package team.retum.jobis.domain.application.persistence.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
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.AllArgsConstructor;
Expand All @@ -14,9 +21,15 @@
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Embeddable
@Table(name = "tbl_application_attachment")
@Entity
public class ApplicationAttachmentEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(columnDefinition = "VARCHAR(400)")
private String attachmentUrl;

Expand All @@ -25,4 +38,19 @@ public class ApplicationAttachmentEntity {
@Column(columnDefinition = "VARCHAR(4)")
private AttachmentType type;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "application_id", nullable = false)
private ApplicationEntity application;

public ApplicationAttachmentEntity(String attachmentUrl, AttachmentType type) {
this.attachmentUrl = attachmentUrl;
this.type = type;
}

protected void setApplication(ApplicationEntity application) {
if (this.application != null) {
return;
}
this.application = application;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package team.retum.jobis.domain.application.persistence.entity;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
Expand All @@ -12,6 +11,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
Expand All @@ -37,14 +37,6 @@ public class ApplicationEntity extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", nullable = false)
private StudentEntity student;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "recruitment_id")
private RecruitmentEntity recruitment;

@NotNull
@Column(columnDefinition = "VARCHAR(11)")
@Enumerated(EnumType.STRING)
Expand All @@ -59,21 +51,32 @@ public class ApplicationEntity extends BaseTimeEntity {
@Column(columnDefinition = "DATE")
private LocalDate endDate;

@ElementCollection
@CollectionTable(name = "tbl_application_attachment", joinColumns = @JoinColumn(name = "application_id"))
private List<ApplicationAttachmentEntity> attachments = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", nullable = false)
private StudentEntity student;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "recruitment_id", nullable = false)
private RecruitmentEntity recruitment;

@OneToMany(mappedBy = "application", cascade = CascadeType.ALL)
private List<ApplicationAttachmentEntity> applicationAttachments = new ArrayList<>();

@Builder
public ApplicationEntity(Long id, StudentEntity studentEntity, RecruitmentEntity recruitmentEntity,
ApplicationStatus applicationStatus, String rejectionReason,
LocalDate startDate, LocalDate endDate, List<ApplicationAttachmentEntity> attachments) {
LocalDate startDate, LocalDate endDate) {
this.id = id;
this.student = studentEntity;
this.recruitment = recruitmentEntity;
this.applicationStatus = applicationStatus;
this.rejectionReason = rejectionReason;
this.startDate = startDate;
this.endDate = endDate;
this.attachments = attachments;
}

public void addApplicationAttachment(ApplicationAttachmentEntity attachment) {
attachment.setApplication(this);
this.applicationAttachments.add(attachment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import team.retum.jobis.domain.application.model.ApplicationAttachment;
import team.retum.jobis.domain.application.persistence.entity.ApplicationAttachmentEntity;
import team.retum.jobis.domain.application.persistence.entity.ApplicationEntity;
import team.retum.jobis.domain.recruitment.exception.RecruitmentNotFoundException;
import team.retum.jobis.domain.recruitment.persistence.entity.RecruitmentEntity;
import team.retum.jobis.domain.recruitment.persistence.repository.RecruitmentJpaRepository;
import team.retum.jobis.domain.student.exception.StudentNotFoundException;
import team.retum.jobis.domain.student.persistence.entity.StudentEntity;
import team.retum.jobis.domain.student.persistence.repository.StudentJpaRepository;

Expand All @@ -23,30 +21,30 @@ public class ApplicationMapper {
private final RecruitmentJpaRepository recruitmentJpaRepository;

public ApplicationEntity toEntity(Application domain) {
StudentEntity student = studentJpaRepository.findById(domain.getStudentId())
.orElseThrow(() -> StudentNotFoundException.EXCEPTION);
StudentEntity student = studentJpaRepository.getReferenceById(domain.getStudentId());
RecruitmentEntity recruitment = recruitmentJpaRepository.getReferenceById(domain.getRecruitmentId());

RecruitmentEntity recruitment = recruitmentJpaRepository.findById(domain.getRecruitmentId())
.orElseThrow(() -> RecruitmentNotFoundException.EXCEPTION);

List<ApplicationAttachmentEntity> attachments = domain.getAttachments().stream()
.map(attachment -> new ApplicationAttachmentEntity(attachment.getAttachmentUrl(), attachment.getType()))
.toList();

return ApplicationEntity.builder()
ApplicationEntity applicationEntity = ApplicationEntity.builder()
.id(domain.getId())
.applicationStatus(domain.getApplicationStatus())
.studentEntity(student)
.recruitmentEntity(recruitment)
.rejectionReason(domain.getRejectionReason())
.startDate(domain.getStartDate())
.endDate(domain.getEndDate())
.attachments(attachments)
.build();

domain.getAttachments().forEach(
attachment -> applicationEntity.addApplicationAttachment(
new ApplicationAttachmentEntity(attachment.getAttachmentUrl(), attachment.getType())
)
);

return applicationEntity;
}

public Application toDomain(ApplicationEntity entity) {
List<ApplicationAttachment> attachments = entity.getAttachments().stream()
List<ApplicationAttachment> attachments = entity.getApplicationAttachments().stream()
.map(attachment -> new ApplicationAttachment(attachment.getAttachmentUrl(), attachment.getType()))
.toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import team.retum.jobis.domain.application.model.ApplicationStatus;
import team.retum.jobis.domain.application.persistence.entity.ApplicationEntity;

import java.util.List;
Expand All @@ -14,8 +13,6 @@ public interface ApplicationJpaRepository extends JpaRepository<ApplicationEntit

void deleteByIdIn(List<Long> applicationIds);

@Query("select a from ApplicationEntity a join fetch a.attachments where a.id=?1")
@Query("select a from ApplicationEntity a join fetch a.applicationAttachments where a.id=?1")
Optional<ApplicationEntity> findByIdFetch(Long applicationId);

int countByApplicationStatusIn(List<ApplicationStatus> applicationStatuses);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
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.OneToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import team.retum.jobis.domain.notification.persistence.entity.NotificationEntity;
import team.retum.jobis.global.entity.BaseTimeEntity;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

@Component
public class FCMUtil {
Expand Down
6 changes: 4 additions & 2 deletions jobis-infrastructure/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ spring:
url: jdbc:mysql://${DB_URL:localhost}:3306/${DB_NAME:jobis}?useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&tinyInt1isBit=false&rewriteBatchedStatements=true
username: ${DB_USERNAME:root}
password: ${DB_PASSWORD:11111111}

flyway:
baseline-version: 1
enabled: true
baseline-on-migrate: true
baseline-version: 0
enabled: ${FLYWAY_ENABLED:true}
fail-on-missing-locations: true

jpa:
database-platform: team.retum.jobis.global.config.MysqlDialectConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE new_tbl_application_attachment
(
id BIGINT AUTO_INCREMENT,
attachment_url VARCHAR(400) NOT NULL,
type VARCHAR(4) NOT NULL,
application_id BIGINT NOT NULL,
PRIMARY KEY (id),
CONSTRAINT FK_NEW_TBL_APPLICATION_ATTACHMENT_ON_APPLICATION
FOREIGN KEY (application_id) REFERENCES tbl_application (id)
);

INSERT INTO new_tbl_application_attachment (attachment_url, type, application_id)
SELECT attachment_url, type, application_id FROM tbl_application_attachment;

DROP TABLE tbl_application_attachment;

RENAME TABLE new_tbl_application_attachment TO tbl_application_attachment;

0 comments on commit c0367c5

Please sign in to comment.