-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
452 additions
and
11 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
52 changes: 52 additions & 0 deletions
52
src/main/java/school/hei/haapi/endpoint/rest/controller/PaymentController.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,52 @@ | ||
package school.hei.haapi.endpoint.rest.controller; | ||
|
||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import school.hei.haapi.endpoint.rest.mapper.PaymentMapper; | ||
import school.hei.haapi.endpoint.rest.model.CreatePayment; | ||
import school.hei.haapi.endpoint.rest.model.Payment; | ||
import school.hei.haapi.service.PaymentService; | ||
|
||
import static java.util.stream.Collectors.toUnmodifiableList; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
public class PaymentController { | ||
|
||
private final PaymentService paymentService; | ||
private final PaymentMapper paymentMapper; | ||
|
||
@PostMapping("/students/{studentId}/fees/{feeId}/payments") | ||
public List<Payment> createPayments( | ||
@PathVariable String studentId, | ||
@PathVariable String feeId, | ||
@RequestBody List<CreatePayment> toCreate) { | ||
return paymentService | ||
.saveAll(studentId, feeId, toCreate.stream() | ||
.map(paymentMapper::toDomainPayment) | ||
.collect(toUnmodifiableList())) | ||
.stream() | ||
.map(paymentMapper::toRestPayment) | ||
.collect(toUnmodifiableList()); | ||
} | ||
|
||
@GetMapping("/students/{studentId}/fees/{feeId}/payments") | ||
public List<Payment> getFeePaymentsByStudentId( | ||
@PathVariable String studentId, @PathVariable String feeId) { | ||
return paymentService.getByStudentIdAndFeeId(studentId, feeId).stream() | ||
.map(paymentMapper::toRestPayment) | ||
.collect(toUnmodifiableList()); | ||
} | ||
|
||
@GetMapping("/students/{studentId}/fees/payments") | ||
public List<Payment> getPaymentsByStudentId(@PathVariable String studentId) { | ||
return paymentService.getByStudentId(studentId).stream() | ||
.map(paymentMapper::toRestPayment) | ||
.collect(toUnmodifiableList()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package school.hei.haapi.endpoint.rest.mapper; | ||
|
||
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; | ||
|
||
@Component | ||
public class PaymentMapper { | ||
public Payment toRestPayment(school.hei.haapi.model.Payment payment) { | ||
return new Payment() | ||
.id(payment.getId()) | ||
.feeId(payment.getFeeId()) | ||
.type(payment.getType()) | ||
.amount(payment.getAmount()) | ||
.comment(payment.getComment()) | ||
.creationDatetime(payment.getCreationDatetime()); | ||
} | ||
|
||
public school.hei.haapi.model.Payment toDomainPayment(CreatePayment createPayment) { | ||
return school.hei.haapi.model.Payment.builder() | ||
.feeId(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())) { | ||
return Payment.TypeEnum.CASH; | ||
} else if (feeType.equals(Payment.TypeEnum.SCOLARSHIP.toString())) { | ||
return Payment.TypeEnum.SCOLARSHIP; | ||
} else if (feeType.equals(Payment.TypeEnum.FIX.toString())) { | ||
return Payment.TypeEnum.FIX; | ||
} | ||
throw new BadRequestException("Payment type must be valid"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package school.hei.haapi.model; | ||
|
||
import java.io.Serializable; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
import javax.persistence.Entity; | ||
import javax.persistence.EnumType; | ||
import javax.persistence.Enumerated; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.hibernate.Hibernate; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
import org.hibernate.annotations.Type; | ||
import org.hibernate.annotations.TypeDef; | ||
import school.hei.haapi.repository.types.PostgresEnumType; | ||
|
||
import static javax.persistence.GenerationType.IDENTITY; | ||
|
||
@Entity | ||
@Table(name = "\"payment\"") | ||
@Getter | ||
@Setter | ||
@TypeDef(name = "pgsql_enum", typeClass = PostgresEnumType.class) | ||
@ToString | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Payment implements Serializable { | ||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private String id; | ||
|
||
private String feeId; | ||
|
||
@Type(type = "pgsql_enum") | ||
@Enumerated(EnumType.STRING) | ||
private school.hei.haapi.endpoint.rest.model.Payment.TypeEnum type; | ||
|
||
private int amount; | ||
private String comment; | ||
@CreationTimestamp private Instant creationDatetime; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) { | ||
return false; | ||
} | ||
Payment payment = (Payment) o; | ||
return amount == payment.amount | ||
&& Objects.equals(id, payment.id) | ||
&& Objects.equals(feeId, payment.feeId) | ||
&& type == payment.type | ||
&& creationDatetime.compareTo(payment.creationDatetime) == 0; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return getClass().hashCode(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/school/hei/haapi/repository/PaymentRepository.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,18 @@ | ||
package school.hei.haapi.repository; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
import school.hei.haapi.model.Payment; | ||
|
||
@Repository | ||
public interface PaymentRepository extends JpaRepository<Payment, String> { | ||
List<Payment> getByFeeId(String feeId); | ||
|
||
@Query(value = "select p.* from payment p join fee f on f.id = p.fee_id" | ||
+ " where f.user_id = :student_id", | ||
nativeQuery = true) | ||
List<Payment> getByStudentId(@Param("student_id") String studentId); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/school/hei/haapi/service/PaymentService.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,26 @@ | ||
package school.hei.haapi.service; | ||
|
||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import school.hei.haapi.model.Payment; | ||
import school.hei.haapi.repository.PaymentRepository; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
public class PaymentService { | ||
|
||
private final PaymentRepository paymentRepository; | ||
|
||
public List<Payment> getByStudentIdAndFeeId(String studentId, String feeId) { | ||
return paymentRepository.getByFeeId(feeId); | ||
} | ||
|
||
public List<Payment> getByStudentId(String studentId) { | ||
return paymentRepository.getByStudentId(studentId); | ||
} | ||
|
||
public List<Payment> saveAll(String studentId, String feeId, List<Payment> toCreate) { | ||
return paymentRepository.saveAll(toCreate); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/resources/db/migration/V0_4__Create_payment_table.sql
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,21 @@ | ||
do | ||
$$ | ||
begin | ||
if not exists(select from pg_type where typname = 'payment_type') then | ||
create type "payment_type" as enum ('CASH', 'SCHOLARSHIP','FIX'); | ||
end if; | ||
end | ||
$$; | ||
|
||
create table if not exists "payment" | ||
( | ||
id varchar | ||
constraint payment_pk primary key default uuid_generate_v4(), | ||
fee_id varchar not null | ||
constraint payment_fee_id_fk references "fee"(id), | ||
type payment_type not null, | ||
amount integer not null, | ||
comment varchar not null, | ||
creation_datetime timestamp with time zone not null default now() | ||
); | ||
create index if not exists payment_fee_id_index on "payment" (fee_id); |
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 was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/main/resources/db/testdata/V99_4__Test_create_payments.sql
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,8 @@ | ||
insert into "payment" | ||
(id, fee_id, type, comment,amount, creation_datetime) | ||
values | ||
('payment1_id','fee1_id', 'CASH', 'Comment', 2000, '2021-11-08T08:25:24.00Z'); | ||
insert into "payment" | ||
(id, fee_id, type, comment,amount, creation_datetime) | ||
values | ||
('payment2_id','fee2_id', 'CASH', 'Comment', 2000, '2021-11-08T08:25:24.00Z'); |
Oops, something went wrong.