source: src/main/java/com/example/skychasemk/services/PaymentService.java@ 62bba0c

Last change on this file since 62bba0c was de83113, checked in by ste08 <sjovanoska@…>, 4 months ago

Signup,Login,FlightSearch,Booking and Payment working!

  • Property mode set to 100644
File size: 2.3 KB
Line 
1package com.example.skychasemk.services;
2
3import com.example.skychasemk.dto.PaymentDTO;
4import com.example.skychasemk.model.ApplicationUser;
5import com.example.skychasemk.model.Payment;
6import com.example.skychasemk.model.Booking;
7import com.example.skychasemk.repository.ApplicationUserRepository;
8import com.example.skychasemk.repository.BookingRepository;
9import com.example.skychasemk.repository.PaymentRepository;
10import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.stereotype.Service;
12
13import java.time.LocalDate;
14import java.util.List;
15import java.util.Optional;
16
17import static com.example.skychasemk.model.Payment.PaymentMethod.CREDIT;
18import static com.example.skychasemk.model.Payment.PaymentStatus.COMPLETED;
19
20@Service
21public 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
Note: See TracBrowser for help on using the repository browser.