package com.javaTechie.flightserviceexample.service;
import java.util.UUID;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.javaTechie.flightserviceexample.dto.FlightBookingAcknowledgement;
import com.javaTechie.flightserviceexample.dto.FlightBookingRequest;
import com.javaTechie.flightserviceexample.entity.PassengerInfo;
import com.javaTechie.flightserviceexample.entity.PaymentInfo;
import com.javaTechie.flightserviceexample.repo.PassengerInfoRepository;
import com.javaTechie.flightserviceexample.repo.PaymentInfoRepository;
import com.javaTechie.flightserviceexample.utils.PaymentUtils;
@Service
public class FlightBookingService {
@Autowired
private PassengerInfoRepository passengerInfoRepository;
@Autowired
private PaymentInfoRepository paymentInfoRepository;
// Mention the @Transactional annotation over the service layer method
@Transactional
public FlightBookingAcknowledgement bookFlightTicket(FlightBookingRequest request) {
PassengerInfo passengerInfo = request.getPassengerInfo();
passengerInfo = passengerInfoRepository.save(passengerInfo);
PaymentInfo paymentInfo = request.getPaymentInfo();
PaymentUtils.validateCreditLimit(paymentInfo.getAccountNo(), passengerInfo.getFare());
paymentInfo.setPassengerId(passengerInfo.getpId());
paymentInfo.setAmount(passengerInfo.getFare());
paymentInfoRepository.save(paymentInfo);
return new FlightBookingAcknowledgement("SUCCESS", passengerInfo.getFare(),
UUID.randomUUID().toString().split("-")[0], passengerInfo);
}
}
--------------------------------------------------------------------------------
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.javaTechie.flightserviceexample.dto.FlightBookingAcknowledgement;
import com.javaTechie.flightserviceexample.dto.FlightBookingRequest;
import com.javaTechie.flightserviceexample.service.FlightBookingService;
//Mention the @EnableTransactionManagement over the Main class of application
@SpringBootApplication
@RestController
@EnableTransactionManagement
public class FlightServiceExampleApplication {
@Autowired
private FlightBookingService service;
@PostMapping("/bookFlightTicket")
public FlightBookingAcknowledgement bookFlightTicket(@RequestBody FlightBookingRequest request ) {
return service.bookFlightTicket(request);
}
public static void main(String[] args) {
SpringApplication.run(FlightServiceExampleApplication.class, args);
}
}
No comments:
Post a Comment