-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from ucmo-cs/add-payments
Added payments to backend
- Loading branch information
Showing
7 changed files
with
177 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ | |
|
||
import com.avengers.example.domain.Account; | ||
import com.avengers.example.domain.Loan; | ||
import com.avengers.example.domain.Payment; | ||
import com.avengers.example.repository.AccountRepository; | ||
import com.avengers.example.repository.LoanRepository; | ||
import com.avengers.example.repository.PaymentRepository; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.CommandLineRunner; | ||
|
@@ -18,12 +20,14 @@ public class TestDatabaseConfig { | |
private static final Logger logger = LoggerFactory.getLogger(TestDatabaseConfig.class); | ||
|
||
@Bean | ||
CommandLineRunner initDatabase(AccountRepository repository, LoanRepository loanRepository) { | ||
CommandLineRunner initDatabase(AccountRepository repository, LoanRepository loanRepository, PaymentRepository paymentRepository) { | ||
return args -> { | ||
logger.info("Preloading {}", repository.save(new Account(true, "Tony Stark", "test", "[email protected]", "123-456-7890"))); | ||
logger.info("Preloading {}", repository.save(new Account(false, "Steve Rogers", "1234", "[email protected]", "123-123-1234"))); | ||
logger.info("Preloading {}", loanRepository.save(new Loan(1000, 0.05, 12f, new Date(), repository.findById(2L).orElse(null)))); | ||
logger.info("Preloading {}", loanRepository.save(new Loan(50000, 3270.75, 6.99f, new Date(), repository.findById(2L).orElse(null)))); | ||
logger.info("Preloading {}", paymentRepository.save(new Payment(100, new Date(), loanRepository.findById(1L).orElse(null)))); | ||
logger.info("Preloading {}", paymentRepository.save(new Payment(80, new Date(), loanRepository.findById(1L).orElse(null)))); | ||
}; | ||
} | ||
|
||
|
45 changes: 45 additions & 0 deletions
45
src/main/java/com/avengers/example/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,45 @@ | ||
package com.avengers.example.controller; | ||
|
||
import com.avengers.example.domain.Payment; | ||
import com.avengers.example.service.PaymentService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
public class PaymentController | ||
{ | ||
private final PaymentService paymentService; | ||
|
||
public PaymentController(PaymentService paymentService) | ||
{ | ||
this.paymentService = paymentService; | ||
} | ||
|
||
/** | ||
* Creates a new payment object and saves it to the database. | ||
* | ||
* @param payment the payment instance to be added to the database. | ||
* @return The response from the server. | ||
*/ | ||
@PostMapping("/payment") | ||
public ResponseEntity<?> save(@RequestBody Payment payment) | ||
{ | ||
return new ResponseEntity<>(paymentService.create(payment), HttpStatus.CREATED); | ||
} | ||
|
||
/** | ||
* @return A response object containing all payments from the database. | ||
*/ | ||
@GetMapping("/payments") | ||
public ResponseEntity<?> findAll() | ||
{ | ||
return new ResponseEntity<>(paymentService.findAll(), HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("/payments/{loanId}") | ||
public ResponseEntity<?> findAllByLoan(@PathVariable Long loanId) | ||
{ | ||
return new ResponseEntity<>(paymentService.findAllByLoan(loanId), HttpStatus.OK); | ||
} | ||
} |
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,43 @@ | ||
package com.avengers.example.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.Date; | ||
|
||
@Entity | ||
@Data | ||
@Getter | ||
@Setter | ||
public class Payment | ||
{ | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private float paymentAmount; | ||
private Date paymentDate; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "loan_id") | ||
private Loan loan; | ||
|
||
public Payment(float paymentAmount, Date paymentDate, Loan loan) | ||
{ | ||
this.paymentAmount = paymentAmount; | ||
this.paymentDate = paymentDate; | ||
this.loan = loan; | ||
} | ||
|
||
public Payment(float paymentAmount, Date paymentDate) | ||
{ | ||
this.paymentAmount = paymentAmount; | ||
this.paymentDate = paymentDate; | ||
} | ||
|
||
public Payment() | ||
{ | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/avengers/example/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,11 @@ | ||
package com.avengers.example.repository; | ||
|
||
import com.avengers.example.domain.Payment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface PaymentRepository extends JpaRepository<Payment, Long> | ||
{ | ||
List<Payment> findAllByLoanId(Long loanId); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/avengers/example/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,31 @@ | ||
package com.avengers.example.service; | ||
|
||
import com.avengers.example.domain.Payment; | ||
import com.avengers.example.repository.PaymentRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@AllArgsConstructor | ||
@Service | ||
public class PaymentService | ||
{ | ||
private final PaymentRepository paymentRepository; | ||
|
||
@Transactional | ||
public Payment create(Payment payment) | ||
{ | ||
return paymentRepository.save(payment); | ||
} | ||
|
||
public List<Payment> findAllByLoan(Long loanId) { | ||
return paymentRepository.findAllByLoanId(loanId); | ||
} | ||
|
||
public List<Payment> findAll() | ||
{ | ||
return paymentRepository.findAll(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/com/avengers/example/domain/PaymentTests.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,23 @@ | ||
package com.avengers.example.domain; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.Date; | ||
|
||
@SpringBootTest | ||
public class PaymentTests | ||
{ | ||
private Payment testPayment; | ||
private float testPaymentAmount = 1.10f; | ||
private Date testPaymentDate = new Date(); | ||
|
||
@Test | ||
public void testConstructor() | ||
{ | ||
testPayment = new Payment(testPaymentAmount, testPaymentDate); | ||
|
||
assert testPayment.getPaymentAmount() == testPaymentAmount; | ||
assert testPayment.getPaymentDate() == testPaymentDate; | ||
} | ||
} |