Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ ResponseEntity<ApiResponse<Page<ApplicationFormResponse>>> getAllApplicationForm
);

@Operation(summary = "지원서 업데이트",
description = "일정 : (삭제)-> 요청에 미포함, (업데이트)-> id포함 내용변경, (신규)-> id미포함 내용생성")
description = "일정 : (삭제)-> 삭제할 id를 요청에 미기재, (업데이트)-> id 기재 후 내용변경, (신규)-> id 미기재 내용 생성\n" +
"문서 : (삭제)-> 삭제할 id를 요청에 미기재, (신규)->id 미기재 documentId 기재")
@ApiResponseExplanations(
success = @ApiSuccessResponseExplanation(
responseClass = ApplicationFormResponse.class,
description = "지원서 업데이트 성공"
),
errors = {
@ApiErrorResponseExplanation(exceptionCode = ResponseCode.NOT_FOUND_APPLICATION_FORM),
@ApiErrorResponseExplanation(exceptionCode = ResponseCode.INVALID_SCHEDULE_FORM_ASSOCIATION),
@ApiErrorResponseExplanation(exceptionCode = ResponseCode.FORBIDDEN)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface ApplicationFormRepository extends JpaRepository<ApplicationForm, Long> {
Page<ApplicationForm> findAllByUserId(final Long id, final Pageable pageable);
Page<ApplicationForm> findAllByUserId(final Long userId, final Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.jobnote.domain.applicationform.service;

import com.jobnote.domain.applicationform.domain.ApplicationForm;
import com.jobnote.domain.applicationform.dto.ApplicationFormSimpleResponse;
import com.jobnote.domain.applicationformdocument.domain.ApplicationFormDocument;
import com.jobnote.domain.applicationformdocument.dto.ApplicationFormDocumentRequest;
import com.jobnote.domain.applicationform.repository.ApplicationFormRepository;
import com.jobnote.domain.applicationform.dto.ApplicationFormRequest;
import com.jobnote.domain.applicationform.dto.ApplicationFormResponse;
Expand All @@ -22,9 +19,6 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static com.jobnote.global.common.ResponseCode.NOT_FOUND_APPLICATION_FORM;

Expand Down Expand Up @@ -63,12 +57,6 @@ public Page<ApplicationFormResponse> getAll(final Long userId, final Pageable pa
);
}

public List<ApplicationFormSimpleResponse> getAllSimple(final Long userId) {
Page<ApplicationForm> forms = applicationFormRepository.findAllByUserId(userId, Pageable.unpaged());

return forms.map(ApplicationFormSimpleResponse::from).toList();
}

/* CREATE */
@Transactional
public Long save(final Long userId, final ApplicationFormRequest request) {
Expand Down Expand Up @@ -99,7 +87,7 @@ public void update(final Long userId, final Long formId, final ApplicationFormRe
scheduleService.updateAll(userId, form, request.schedules());

// 연결된 문서 업데이트
updateApplicationFormDocuments(userId, form, request.documents());
applicationFormDocumentService.updateAll(userId, form, request.documents());
}

/* DELETE */
Expand All @@ -118,29 +106,4 @@ public ApplicationForm getByIdOrThrow(final Long formId) {
return applicationFormRepository.findById(formId)
.orElseThrow(() -> new JobNoteException(NOT_FOUND_APPLICATION_FORM));
}

private void updateApplicationFormDocuments(final Long userId, final ApplicationForm form, final List<ApplicationFormDocumentRequest> requests) {
// 기존 연결된 문서 조회
List<ApplicationFormDocument> existsDocuments = applicationFormDocumentService.getAllByApplicationFormId(userId, form.getId());

// 요청 문서 ID 목록
Set<Long> requestsIds = requests.stream()
.map(ApplicationFormDocumentRequest::id)
.filter(Objects::nonNull)
.collect(Collectors.toSet());

// 삭제 : 기존 문서 중 요청에 없는 문서 삭제
for (ApplicationFormDocument document : existsDocuments) {
if (!requestsIds.contains(document.getId())) {
applicationFormDocumentService.delete(document);
}
}

// 신규 생성 (
for (ApplicationFormDocumentRequest req : requests) {
if (req.id() == null) {
applicationFormDocumentService.saveAll(form, List.of(req));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import java.util.List;

public interface ApplicationFormDocumentRepository extends JpaRepository<ApplicationFormDocument, Long> {
// 해당 지원서에 속한 문서(지원서-문서 맵핑 엔티티) 목록 반환
/* 해당 지원서에 속한 문서(지원서-문서 맵핑 엔티티) 목록 반환 */
@Query("select afd from ApplicationFormDocument afd join fetch afd.applicationForm af join fetch af.user where af.user.id = :userId and af.id in :formId")
List<ApplicationFormDocument> findAllByUserIdAndApplicationFormIdIn(final Long userId, final List<Long> formId);

// 해당 지원서에 속한 문서 연결 해제
/* 해당 문서에 속한 지원서(지원서-문서 맵핑 엔티티) 목록 반환 */
@Query("select afd from ApplicationFormDocument afd join fetch afd.document d join fetch d.user where d.user.id = :userId and d.id in :documentId")
List<ApplicationFormDocument> findAllByUserIdAndDocumentId(final Long userId, final Long documentId);

/* 해당 지원서에 속한 문서 연결 해제 */
void deleteAllByApplicationFormId(final Long formId);

// 해당 문서에 속한 지원서 연결 해제
/* 해당 문서에 속한 지원서 연결 해제 */
void deleteAllByDocumentId(final Long documentId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jobnote.domain.applicationformdocument.service;

import com.jobnote.domain.applicationform.domain.ApplicationForm;
import com.jobnote.domain.applicationform.dto.ApplicationFormSimpleResponse;
import com.jobnote.domain.applicationformdocument.domain.ApplicationFormDocument;
import com.jobnote.domain.applicationformdocument.dto.ApplicationFormDocumentRequest;
import com.jobnote.domain.applicationformdocument.repository.ApplicationFormDocumentRepository;
Expand All @@ -14,6 +15,8 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static com.jobnote.global.common.ResponseCode.NOT_FOUND_DOCUMENT;
Expand All @@ -31,6 +34,12 @@ public List<ApplicationFormDocument> getAllByApplicationFormId(final Long userId
return applicationFormDocumentRepository.findAllByUserIdAndApplicationFormIdIn(userId, List.of(formId));
}

public List<ApplicationFormSimpleResponse> getSimpleResponsesByDocumentId(final Long userId, final Long documentId) {
return applicationFormDocumentRepository.findAllByUserIdAndDocumentId(userId, documentId).stream()
.map(afd -> ApplicationFormSimpleResponse.from(afd.getApplicationForm()))
.toList();
}

public List<DocumentSimpleResponse> getSimpleResponsesByApplicationFormId(final Long userId, final Long formId) {
return applicationFormDocumentRepository.findAllByUserIdAndApplicationFormIdIn(userId, List.of(formId)).stream()
.map(afd -> DocumentSimpleResponse.from(afd.getDocument()))
Expand Down Expand Up @@ -62,6 +71,33 @@ public void saveAll(final ApplicationForm form, final List<ApplicationFormDocume
}
}

/* UPDATE */
@Transactional
public void updateAll(final Long userId, final ApplicationForm form, final List<ApplicationFormDocumentRequest> requests) {
// 기존 연결된 문서 조회
List<ApplicationFormDocument> existsDocuments = getAllByApplicationFormId(userId, form.getId());

// 요청 문서 ID 목록
Set<Long> requestsIds = requests.stream()
.map(ApplicationFormDocumentRequest::id)
.filter(Objects::nonNull)
.collect(Collectors.toSet());

// 삭제 : 기존 문서 중 요청에 없는 문서 삭제
for (ApplicationFormDocument document : existsDocuments) {
if (!requestsIds.contains(document.getId())) {
delete(document);
}
}

// 신규 생성
for (ApplicationFormDocumentRequest req : requests) {
if (req.id() == null) {
saveAll(form, List.of(req));
}
}
}

/* DELETE */
@Transactional
public void deleteAllByApplicationFormId(final Long formId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public record DocumentResponse(
LocalDate lastModifiedDate,
List<ApplicationFormSimpleResponse> applicationForms
) {
public static DocumentResponse from(final Document document, final List<ApplicationFormSimpleResponse> applicationForms) {
public static DocumentResponse of(final Document document, final List<ApplicationFormSimpleResponse> applicationForms) {
return DocumentResponse.builder()
.id(document.getId())
.type(document.getType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
import java.util.Optional;

public interface DocumentVersionRepository extends JpaRepository<DocumentVersion, Long> {
@Query("select MAX(v.version) from DocumentVersion v where v.document.id = :documentId")
Optional<Integer> findMaxVersionByDocumentId(final Long documentId);

@Query("select v from DocumentVersion v join fetch v.document d join d.user u where u.id = :userId and d.id = :documentId order by v.version desc")
Page<DocumentVersion> findAllByUserIdAndDocumentId(final Long userId, final Long documentId, final Pageable pageable);

/* 해당 문서의 최신 버전 조회 */
@Query("select MAX(v.version) from DocumentVersion v where v.document.id = :documentId")
Optional<Integer> findLatestVersionByDocumentId(final Long documentId);

/* 해당 유저의 문서 총 이용 용량 조회 */
@Query("select COALESCE(SUM(v.fileSize), 0) from DocumentVersion v join v.document d where d.user.id = :userId")
Long getTotalFileSizeByUserId(final Long userId);

/* 해당 문서에 등록된 문서 버전들 모두 삭제 */
@Modifying(clearAutomatically = true)
void deleteAllByDocumentId(final Long documentId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jobnote.domain.document.service;

import com.jobnote.domain.applicationform.service.ApplicationFormService;
import com.jobnote.domain.applicationformdocument.service.ApplicationFormDocumentService;
import com.jobnote.domain.document.domain.Document;
import com.jobnote.domain.document.domain.DocumentVersion;
Expand Down Expand Up @@ -30,7 +29,6 @@ public class DocumentService {
private final DocumentVersionRepository documentVersionRepository;
private final UserService userService;
private final S3Service s3Service;
private final ApplicationFormService applicationFormService;
private final ApplicationFormDocumentService applicationFormDocumentService;

@Transactional
Expand All @@ -47,14 +45,18 @@ public Long uploadNewVersionDocument(final Long userId, final Long documentId, f
Document document = getByIdOrThrow(documentId);
document.validateOwner(userId);

int version = documentVersionRepository.findMaxVersionByDocumentId(documentId).orElse(0) + 1;
int version = documentVersionRepository.findLatestVersionByDocumentId(documentId).orElse(0) + 1;

return saveDocumentVersion(userId, document, request, version);
}

public Page<DocumentResponse> getAll(final Long userId, final Pageable pageable) {
return documentRepository.findAllByUserId(userId, pageable)
.map(document -> DocumentResponse.from(document, applicationFormService.getAllSimple(userId)));
.map(document ->
DocumentResponse.of(
document,
applicationFormDocumentService.getSimpleResponsesByDocumentId(userId, document.getId())
));
}

public Page<DocumentVersionResponse> getAllVersions(final Long userId, final Long documentId, final Pageable pageable) {
Expand Down
Loading