[57e58a3] | 1 | package com.example.skychasemk.controller;
|
---|
| 2 |
|
---|
| 3 | import com.example.skychasemk.dto.BookingDTO;
|
---|
| 4 | import com.example.skychasemk.model.Booking;
|
---|
| 5 | import com.example.skychasemk.repository.BookingRepository;
|
---|
| 6 | import com.example.skychasemk.services.BookingService;
|
---|
| 7 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 8 | import org.springframework.web.bind.annotation.*;
|
---|
| 9 |
|
---|
| 10 | import java.time.LocalDate;
|
---|
| 11 | import java.util.Optional;
|
---|
| 12 |
|
---|
| 13 | @RestController
|
---|
| 14 | @RequestMapping("/api")
|
---|
| 15 | public class BookingController {
|
---|
| 16 |
|
---|
| 17 | @Autowired
|
---|
| 18 | private BookingService bookingService;
|
---|
| 19 |
|
---|
| 20 | @Autowired
|
---|
| 21 | private BookingRepository bookingRepository;
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | @GetMapping("/flights/id/{id}")
|
---|
| 25 | public Optional<Booking> getBookingById(@PathVariable("id") Integer bookingID) {
|
---|
| 26 | return bookingService.getBookingById(bookingID);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[de83113] | 29 | @PutMapping("/flights/{bookingId}")
|
---|
| 30 | public Booking updateBooking(@PathVariable("bookingId") Integer bookingID, @RequestBody Booking booking) {
|
---|
[57e58a3] | 31 | return bookingService.updateBooking(bookingID, booking);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | @GetMapping("/bookings/getAll/{flightId}")
|
---|
| 35 | public Booking getAllBookings(@PathVariable Integer flightId){
|
---|
| 36 | return (Booking) bookingRepository.findBookingsByFlightId(flightId);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | @PostMapping("/bookings")
|
---|
| 40 | public Booking createBooking(@RequestBody BookingDTO bookingRequest) {
|
---|
| 41 | Booking newBooking = new Booking();
|
---|
[de83113] | 42 | newBooking.setUserId(bookingRequest.getUserId());
|
---|
[57e58a3] | 43 | newBooking.setFlightId(bookingRequest.getFlightId());
|
---|
| 44 | newBooking.setBookingDate(LocalDate.now());
|
---|
| 45 | newBooking.setStatus(Booking.payment_status.PENDING);
|
---|
[de83113] | 46 | newBooking.setTotal_cost(bookingRequest.getTotalCost());
|
---|
[57e58a3] | 47 | newBooking.setSeatNumber(bookingRequest.getSeatNumber());
|
---|
| 48 | System.out.println("Saving Booking: " + newBooking);
|
---|
| 49 |
|
---|
| 50 | return bookingRepository.save(newBooking);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|