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
@@ -0,0 +1,118 @@
package life.mosu.mosuserver.application.application;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import life.mosu.mosuserver.domain.application.ApplicationJpaEntity;
import life.mosu.mosuserver.domain.exam.ExamJpaEntity;
import life.mosu.mosuserver.domain.examapplication.ExamApplicationJpaEntity;
import life.mosu.mosuserver.domain.examapplication.ExamSubjectJpaEntity;
import life.mosu.mosuserver.domain.payment.PaymentJpaEntity;
import life.mosu.mosuserver.domain.payment.PaymentStatus;
import life.mosu.mosuserver.presentation.application.dto.ApplicationResponse;
import life.mosu.mosuserver.presentation.application.dto.ExamApplicationResponse;

public record ApplicationContext(
List<ApplicationJpaEntity> applications,
List<ExamApplicationJpaEntity> examApplications,
Map<Long, ExamJpaEntity> examMap,
Map<Long, List<ExamSubjectJpaEntity>> subjectMap,
Map<Long, PaymentJpaEntity> paymentMap
) {

public ApplicationContext(List<ApplicationJpaEntity> applications,
List<ExamApplicationJpaEntity> examApplications) {
this(applications, examApplications, Map.of(), Map.of(), Map.of());
}

public ApplicationContext fetchExams(Function<List<Long>, List<ExamJpaEntity>> fetcher) {
List<Long> examIds = this.examApplications.stream().map(ExamApplicationJpaEntity::getExamId)
.distinct().toList();
Map<Long, ExamJpaEntity> newExamMap = fetcher.apply(examIds).stream()
.collect(Collectors.toMap(ExamJpaEntity::getId, Function.identity()));
return new ApplicationContext(this.applications, this.examApplications, newExamMap,
this.subjectMap, this.paymentMap);
}

public ApplicationContext fetchSubjects(
Function<List<Long>, List<ExamSubjectJpaEntity>> fetcher) {
List<Long> examApplicationIds = this.examApplications.stream()
.map(ExamApplicationJpaEntity::getId).toList();
Map<Long, List<ExamSubjectJpaEntity>> newSubjectMap = fetcher.apply(examApplicationIds)
.stream()
.collect(Collectors.groupingBy(ExamSubjectJpaEntity::getExamApplicationId));
return new ApplicationContext(this.applications, this.examApplications, this.examMap,
newSubjectMap, this.paymentMap);
}

public ApplicationContext fetchPayments(Function<List<Long>, List<PaymentJpaEntity>> fetcher) {
List<Long> examApplicationIds = this.examApplications.stream()
.map(ExamApplicationJpaEntity::getId).toList();
Map<Long, PaymentJpaEntity> newPaymentMap = fetcher.apply(examApplicationIds).stream()
.filter(payment -> payment.getPaymentStatus() == PaymentStatus.DONE)
.collect(Collectors.toMap(
PaymentJpaEntity::getExamApplicationId,
Function.identity()));

return new ApplicationContext(this.applications, this.examApplications, this.examMap,
this.subjectMap, newPaymentMap);
}

public List<ApplicationResponse> assemble() {
Map<Long, List<ExamApplicationResponse>> groupedExamResponses = this.examApplications.stream()
.map(this::createExamApplicationResponse)
.filter(Objects::nonNull)
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

return this.applications.stream()
.map(app -> ApplicationResponse.of(app.getId(),
groupedExamResponses.getOrDefault(app.getId(), List.of())))
.toList();
}

private Map.Entry<Long, ExamApplicationResponse> createExamApplicationResponse(
ExamApplicationJpaEntity examApp) throws RuntimeException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The throws RuntimeException clause is redundant here. RuntimeException and its subclasses are unchecked exceptions, so you don't need to declare them in the method signature. Removing it will make the code slightly cleaner.

    private Map.Entry<Long, ExamApplicationResponse> createExamApplicationResponse(
            ExamApplicationJpaEntity examApp) {


ExamJpaEntity exam = this.examMap.get(examApp.getExamId());
if (exam == null) {
return null;
}

List<ExamSubjectJpaEntity> subjectEntities = this.subjectMap.getOrDefault(examApp.getId(),
List.of());
Set<String> subjects = subjectEntities.stream().map(es -> es.getSubject().getSubjectName())
.collect(Collectors.toSet());

PaymentJpaEntity payment = this.paymentMap.getOrDefault(examApp.getId(), null);
String paymentStatus = Optional.ofNullable(payment)
.map(p -> p.getPaymentStatus().name())
.orElse(null);

Integer totalAmount = Optional.ofNullable(payment)
.map(p -> p.getPaymentAmount().getTotalAmount())
.orElse(0);

String lunchName;
if (examApp.getIsLunchChecked()) {
lunchName = exam.getLunchName();
} else {
lunchName = null;
}

ExamApplicationResponse response = ExamApplicationResponse.of(
examApp.getId(),
examApp.getCreatedAt(),
paymentStatus, totalAmount,
exam.getSchoolName(),
exam.getExamDate(),
subjects,
lunchName
);
return Map.entry(examApp.getApplicationId(), response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package life.mosu.mosuserver.application.application;

import life.mosu.mosuserver.presentation.common.FileRequest;

public record ApplicationProcessingContext(
Long applicationId,
FileRequest fileRequest
) {

public static ApplicationProcessingContext of(
Long applicationId,
FileRequest fileRequest
) {
return new ApplicationProcessingContext(
applicationId,
fileRequest
);
}

}
Loading
Loading