1 | package com.example.skychasemk.services;
|
---|
2 |
|
---|
3 | import com.example.skychasemk.dto.PaymentDTO;
|
---|
4 | import com.example.skychasemk.model.ApplicationUser;
|
---|
5 | import com.example.skychasemk.model.Payment;
|
---|
6 | import com.example.skychasemk.model.Booking;
|
---|
7 | import com.example.skychasemk.repository.ApplicationUserRepository;
|
---|
8 | import com.example.skychasemk.repository.BookingRepository;
|
---|
9 | import com.example.skychasemk.repository.PaymentRepository;
|
---|
10 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | import java.time.LocalDate;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Optional;
|
---|
16 |
|
---|
17 | import static com.example.skychasemk.model.Payment.PaymentMethod.CREDIT;
|
---|
18 | import static com.example.skychasemk.model.Payment.PaymentStatus.COMPLETED;
|
---|
19 |
|
---|
20 | @Service
|
---|
21 | public class PaymentService {
|
---|
22 |
|
---|
23 | @Autowired
|
---|
24 | private PaymentRepository paymentRepository;
|
---|
25 | @Autowired
|
---|
26 | ApplicationUserRepository userRepository;
|
---|
27 | @Autowired
|
---|
28 | BookingRepository bookingRepository;
|
---|
29 |
|
---|
30 | // Get all payments
|
---|
31 | public List<Payment> getAllPayments() {
|
---|
32 | return paymentRepository.findAll();
|
---|
33 | }
|
---|
34 |
|
---|
35 | // Get payment by ID
|
---|
36 | public Optional<Payment> getPaymentById(Integer paymentID) {
|
---|
37 | return paymentRepository.findById(paymentID);
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | // Update an existing payment
|
---|
42 | public Payment updatePayment(Integer paymentID, Payment payment) {
|
---|
43 | if (paymentRepository.existsById(paymentID)) {
|
---|
44 | payment.setPaymentID(paymentID);
|
---|
45 | return paymentRepository.save(payment);
|
---|
46 | } else {
|
---|
47 | throw new RuntimeException("Payment not found with id " + paymentID);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Delete a payment
|
---|
52 | public void deletePayment(Integer paymentID) {
|
---|
53 | paymentRepository.deleteById(paymentID);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public Payment createTransaction(PaymentDTO dto){
|
---|
57 | System.out.println( dto);
|
---|
58 | Payment payment = new Payment();
|
---|
59 | payment.setAmount(dto.getAmount());
|
---|
60 | payment.setBookingId(dto.getBookingId());
|
---|
61 | payment.setUserId(dto.getUserId());
|
---|
62 | payment.setStatus(COMPLETED);
|
---|
63 | payment.setMethod(CREDIT);
|
---|
64 | payment.setTransactionDate(LocalDate.now());
|
---|
65 | return paymentRepository.save(payment);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public Payment savePayment(Payment payment) {
|
---|
69 | return paymentRepository.save(payment);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|