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 @@ -38,7 +38,7 @@ public ApplicationFormResponse getById(final Long userId, final Long formId) {
form.validateOwner(userId);

List<ScheduleResponse> schedules = scheduleService.getAllByApplicationFormId(userId, formId);
List<DocumentSimpleResponse> documents = applicationFormDocumentService.getSimpleResponsesByApplicationFormId(userId, formId);
List<DocumentSimpleResponse> documents = applicationFormDocumentService.getAllSimpleByApplicationFormId(userId, formId);

return ApplicationFormResponse.from(form, schedules, documents);
}
Expand All @@ -47,8 +47,9 @@ public Page<ApplicationFormResponse> getAll(final Long userId, final Pageable pa
Page<ApplicationForm> forms = applicationFormRepository.findAllByUserId(userId, pageable);
List<Long> formIds = forms.map(ApplicationForm::getId).toList();

// 각 지원서에 대한 일정, 등록된 문서 목록 생성
Map<Long, List<ScheduleResponse>> schedulesByFormId = scheduleService.getAllGroupedByApplicationFormIds(userId, formIds);
Map<Long, List<DocumentSimpleResponse>> documentsByFormId = applicationFormDocumentService.getSimpleResponsesGroupedByApplicationFormIds(userId, formIds);
Map<Long, List<DocumentSimpleResponse>> documentsByFormId = applicationFormDocumentService.getAllSimpleGroupedByApplicationFormIds(userId, formIds);

return forms.map(form -> ApplicationFormResponse.from(
form,
Expand All @@ -66,10 +67,10 @@ public Long save(final Long userId, final ApplicationFormRequest request) {
ApplicationForm form = applicationFormRepository.save(request.toEntity(user));

// 일정 등록
scheduleService.saveAll(userId, form, request.schedules());
scheduleService.saveAll(form, request.schedules());

// 연결된 문서 등록
applicationFormDocumentService.saveAll(form, request.documents());
applicationFormDocumentService.saveAll(userId, form, request.documents());

return form.getId();
}
Expand All @@ -96,8 +97,13 @@ public void delete(final Long userId, final Long formId) {
ApplicationForm form = getByIdOrThrow(formId);
form.validateOwner(userId);

// 일정 삭제
scheduleService.deleteAllByApplicationFormId(form.getId());

// 연결된 문서 삭제
applicationFormDocumentService.deleteAllByApplicationFormId(form.getId());

// 지원서 삭제
applicationFormRepository.delete(form);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@ public class ApplicationFormDocumentService {
private final DocumentRepository documentRepository;

/* READ */
// 해당 지원서의 등록된 문서 목록 반환
public List<ApplicationFormDocument> getAllByApplicationFormId(final Long userId, final Long formId) {
return applicationFormDocumentRepository.findAllByUserIdAndApplicationFormIdIn(userId, List.of(formId));
}

public List<ApplicationFormSimpleResponse> getSimpleResponsesByDocumentId(final Long userId, final Long documentId) {
// 해당 문서와 연결된 지원서 목록 반환
public List<ApplicationFormSimpleResponse> getAllSimpleByDocumentId(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) {
// 해당 지원서와 연결된 문서 목록 반환
public List<DocumentSimpleResponse> getAllSimpleByApplicationFormId(final Long userId, final Long formId) {
return applicationFormDocumentRepository.findAllByUserIdAndApplicationFormIdIn(userId, List.of(formId)).stream()
.map(afd -> DocumentSimpleResponse.from(afd.getDocument()))
.toList();
}

public Map<Long, List<DocumentSimpleResponse>> getSimpleResponsesGroupedByApplicationFormIds(final Long userId, final List<Long> formIds) {
// 각 지원서에 대한 등록된 문서 목록 반환
public Map<Long, List<DocumentSimpleResponse>> getAllSimpleGroupedByApplicationFormIds(final Long userId, final List<Long> formIds) {
List<ApplicationFormDocument> list = applicationFormDocumentRepository.findAllByUserIdAndApplicationFormIdIn(userId, formIds);

return list.stream()
Expand All @@ -63,9 +67,11 @@ public Map<Long, List<DocumentSimpleResponse>> getSimpleResponsesGroupedByApplic

/* CREATE */
@Transactional
public void saveAll(final ApplicationForm form, final List<ApplicationFormDocumentRequest> requests) {
public void saveAll(final Long userId, final ApplicationForm form, final List<ApplicationFormDocumentRequest> requests) {
for (ApplicationFormDocumentRequest request : requests) {
Document document = getByIdOrThrow(request.documentId());
document.validateOwner(userId);

ApplicationFormDocument entity = request.toEntity(form, document);
applicationFormDocumentRepository.save(entity);
}
Expand Down Expand Up @@ -93,7 +99,7 @@ public void updateAll(final Long userId, final ApplicationForm form, final List<
// 신규 생성
for (ApplicationFormDocumentRequest req : requests) {
if (req.id() == null) {
saveAll(form, List.of(req));
saveAll(userId, form, List.of(req));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Page<DocumentResponse> getAll(final Long userId, final Pageable pageable)
DocumentResponse.of(
document,
documentVersionRepository.findLatestVersionByDocumentId(document.getId()),
applicationFormDocumentService.getSimpleResponsesByDocumentId(userId, document.getId())
applicationFormDocumentService.getAllSimpleByDocumentId(userId, document.getId())
));
}

Expand All @@ -77,9 +77,13 @@ public void delete(final Long userId, final Long documentId) {
s3Service.deleteFile(documentVersion.getFileKey());
}

// 엔티티 삭제
documentVersionRepository.deleteAllByDocumentId(documentId);
// 맵핑 엔티티 삭제
applicationFormDocumentService.deleteAllByDocumentId(documentId);

// 개별 문서 삭제
documentVersionRepository.deleteAllByDocumentId(documentId);

// 문서 삭제
documentRepository.delete(document);
}

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

import com.jobnote.auth.config.LoginUser;
import com.jobnote.auth.dto.CustomUserDetails;
import com.jobnote.domain.applicationform.domain.ApplicationForm;
import com.jobnote.domain.applicationform.service.ApplicationFormService;
import com.jobnote.domain.schedule.api.ScheduleApi;
import com.jobnote.domain.schedule.dto.ScheduleRequest;
import com.jobnote.domain.schedule.dto.ScheduleResponse;
Expand All @@ -24,7 +22,6 @@
public class ScheduleController implements ScheduleApi {

private final ScheduleService scheduleService;
private final ApplicationFormService applicationFormService;

/* CREATE */
@Override
Expand All @@ -34,9 +31,7 @@ public ResponseEntity<ApiResponse<Void>> createSchedule(
@RequestBody @Valid final ScheduleRequest request,
@LoginUser final CustomUserDetails principal
) {
ApplicationForm form = applicationFormService.getByIdOrThrow(formId);

Long savedScheduleId = scheduleService.save(principal.getUserId(), form, request);
Long savedScheduleId = scheduleService.save(principal.getUserId(), formId, request);

URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(savedScheduleId).toUri();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jobnote.domain.schedule.service;

import com.jobnote.domain.applicationform.domain.ApplicationForm;
import com.jobnote.domain.applicationform.repository.ApplicationFormRepository;
import com.jobnote.domain.schedule.domain.Schedule;
import com.jobnote.domain.schedule.dto.ScheduleRequest;
import com.jobnote.domain.schedule.dto.ScheduleResponse;
Expand All @@ -19,6 +20,7 @@
import java.util.Set;
import java.util.stream.Collectors;

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

@Service
Expand All @@ -27,6 +29,7 @@
public class ScheduleService {

private final ScheduleRepository scheduleRepository;
private final ApplicationFormRepository applicationFormRepository;

/* READ */
public ScheduleResponse getById(final Long userId, final Long formId, final Long scheduleId) {
Expand All @@ -37,17 +40,20 @@ public ScheduleResponse getById(final Long userId, final Long formId, final Long
return ScheduleResponse.from(schedule);
}

// 해당 기간의 일정 목록 반환
public Page<ScheduleResponse> getAll(final Long userId, final LocalDateTime startDate, final LocalDateTime endDate, final Pageable pageable) {
return scheduleRepository.findAllByUserIdAndDateTimeBetween(userId, startDate, endDate, pageable)
.map(ScheduleResponse::from);
}

// 해당 지원서의 일정 목록 반환
public List<ScheduleResponse> getAllByApplicationFormId(final Long userId, final Long formId) {
return scheduleRepository.findAllByUserIdAndApplicationFormIdIn(userId, List.of(formId)).stream()
.map(ScheduleResponse::from)
.toList();
}

// 각 지원서의 일정 목록 반환
public Map<Long, List<ScheduleResponse>> getAllGroupedByApplicationFormIds(final Long userId, final List<Long> formIds) {
List<Schedule> schedules = scheduleRepository.findAllByUserIdAndApplicationFormIdIn(userId, formIds);

Expand All @@ -62,17 +68,17 @@ public Map<Long, List<ScheduleResponse>> getAllGroupedByApplicationFormIds(final

/* CREATE */
@Transactional
public Long save(final Long userId, final ApplicationForm form, final ScheduleRequest request) {
public Long save(final Long userId, final Long formId, final ScheduleRequest request) {
ApplicationForm form = applicationFormRepository.findById(formId)
.orElseThrow(() -> new JobNoteException(NOT_FOUND_APPLICATION_FORM));
form.validateOwner(userId);

Schedule saved = scheduleRepository.save(request.toEntity(form));
return saved.getId();
}

@Transactional
public void saveAll(final Long userId, final ApplicationForm form, final List<ScheduleRequest> requests) {
form.validateOwner(userId);

public void saveAll(final ApplicationForm form, final List<ScheduleRequest> requests) {
List<Schedule> schedules = requests.stream()
.map(req -> req.toEntity(form))
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/* 예외 응답 정의 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiErrorResponseExplanation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/* 성공, 예외 응답 정의 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiResponseExplanations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/* 성공 응답 형식 정의 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiSuccessResponseExplanation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/* org.springdoc.core.converters.models.PageableAsQueryParam 한글 버전 재정의 */
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Parameters({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void getAllApplicationForms_withDocuments() {
DocumentSimpleResponse document2 = new DocumentSimpleResponse(102L, DocumentType.COVER_LETTER, "네이버 자소서");
DocumentSimpleResponse document3 = new DocumentSimpleResponse(103L, DocumentType.RESUME, "카카오 이력서");

given(applicationFormDocumentService.getSimpleResponsesGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L))))
given(applicationFormDocumentService.getAllSimpleGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L))))
.willReturn(Map.of(
1L, List.of(document1, document2),
2L, List.of(document3)
Expand All @@ -182,7 +182,7 @@ void getAllApplicationForms_withDocuments() {
assertThat(result2.documents().get(0).title()).isEqualTo("카카오 이력서");

then(applicationFormRepository).should().findAllByUserId(eq(userId), any(Pageable.class));
then(applicationFormDocumentService).should().getSimpleResponsesGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L)));
then(applicationFormDocumentService).should().getAllSimpleGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L)));
}

@Nested
Expand Down
Loading