-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup: connections and tests between fees and payments
- Loading branch information
Showing
17 changed files
with
280 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 15 additions & 7 deletions
22
src/main/java/school/hei/haapi/endpoint/rest/mapper/PaymentMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,48 @@ | ||
package school.hei.haapi.endpoint.rest.mapper; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import school.hei.haapi.endpoint.rest.model.CreatePayment; | ||
import school.hei.haapi.endpoint.rest.model.Payment; | ||
import school.hei.haapi.model.exception.BadRequestException; | ||
import school.hei.haapi.service.FeeService; | ||
|
||
@Component | ||
@AllArgsConstructor | ||
public class PaymentMapper { | ||
private final FeeService feeService; | ||
|
||
public Payment toRestPayment(school.hei.haapi.model.Payment payment) { | ||
return new Payment() | ||
.id(payment.getId()) | ||
.feeId(payment.getFeeId()) | ||
.feeId(payment.getFee().getId()) | ||
.type(payment.getType()) | ||
.amount(payment.getAmount()) | ||
.comment(payment.getComment()) | ||
.creationDatetime(payment.getCreationDatetime()); | ||
} | ||
|
||
public school.hei.haapi.model.Payment toDomainPayment(CreatePayment createPayment) { | ||
if (createPayment.getAmount() == null) { | ||
throw new BadRequestException("Amount is mandatory"); | ||
} | ||
return school.hei.haapi.model.Payment.builder() | ||
.feeId(createPayment.getFeeId()) | ||
.fee(feeService.getById(createPayment.getFeeId())) | ||
.type(toDomainPaymentType(createPayment.getType())) | ||
.amount(createPayment.getAmount()) | ||
.comment(createPayment.getComment()) | ||
.build(); | ||
} | ||
|
||
private Payment.TypeEnum toDomainPaymentType(CreatePayment.TypeEnum createPaymentType) { | ||
String feeType = createPaymentType.toString(); | ||
if (feeType.equals(Payment.TypeEnum.CASH.toString())) { | ||
String paymentType = createPaymentType.toString(); | ||
if (paymentType.equals(Payment.TypeEnum.CASH.toString())) { | ||
return Payment.TypeEnum.CASH; | ||
} else if (feeType.equals(Payment.TypeEnum.SCOLARSHIP.toString())) { | ||
} else if (paymentType.equals(Payment.TypeEnum.SCOLARSHIP.toString())) { | ||
return Payment.TypeEnum.SCOLARSHIP; | ||
} else if (feeType.equals(Payment.TypeEnum.FIX.toString())) { | ||
} else if (paymentType.equals(Payment.TypeEnum.FIX.toString())) { | ||
return Payment.TypeEnum.FIX; | ||
} | ||
throw new BadRequestException("Payment type must be valid"); | ||
throw new BadRequestException("Unexpected paymentFee: " + paymentType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/school/hei/haapi/model/validator/PaymentValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package school.hei.haapi.model.validator; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import school.hei.haapi.model.Payment; | ||
import school.hei.haapi.model.exception.BadRequestException; | ||
|
||
@Component | ||
@AllArgsConstructor | ||
public class PaymentValidator implements Consumer<Payment> { | ||
|
||
public void accept(List<Payment> payments) { | ||
payments.forEach(this::accept); | ||
} | ||
|
||
@Override public void accept(Payment payment) { | ||
Set<String> violationMessages = new HashSet<>(); | ||
if (payment.getFee() == null) { | ||
violationMessages.add("Fee is mandatory"); | ||
} | ||
if (payment.getAmount() < 0) { | ||
violationMessages.add("Amount must be positive"); | ||
} | ||
if (!violationMessages.isEmpty()) { | ||
String formattedViolationMessages = violationMessages.stream() | ||
.map(String::toString) | ||
.collect(Collectors.joining(". ")); | ||
throw new BadRequestException(formattedViolationMessages); | ||
} | ||
} | ||
} |
Oops, something went wrong.