Changeset 07fe0be for src/main


Ignore:
Timestamp:
02/24/25 22:49:01 (3 months ago)
Author:
ste08 <sjovanoska@…>
Branches:
master
Children:
c064a42
Parents:
fda671c
Message:

Wishlist fully working, can book and pay for the booking.

Location:
src/main
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/skychasemk/controller/FlightController.java

    rfda671c r07fe0be  
    3333    }
    3434
    35     @GetMapping("/flights/{id}")
    36     public Optional<Flight> getFlightById(@PathVariable("id") Long flightID) {
     35    @GetMapping("/{flightId}")
     36    public Optional<Flight> getFlightById(@PathVariable("flightId") Long flightID) {
    3737        return flightService.getFlightById(flightID);
    3838    }
  • src/main/java/com/example/skychasemk/controller/WishlistController.java

    rfda671c r07fe0be  
    22
    33import com.example.skychasemk.dto.WishlistDTO;
     4import com.example.skychasemk.model.Flight;
    45import com.example.skychasemk.model.Wishlist;
     6import com.example.skychasemk.repository.BookingRepository;
     7import com.example.skychasemk.repository.FlightRepository;
    58import com.example.skychasemk.repository.WishlistRepository;
    69import com.example.skychasemk.services.WishlistService;
     
    1013import org.springframework.web.bind.annotation.*;
    1114
     15import java.util.Collections;
    1216import java.util.List;
     17import java.util.Optional;
    1318
    1419@RestController
     
    2126    @Autowired
    2227    private WishlistRepository wishlistRepository;
     28
     29    @Autowired
     30    private FlightRepository flightRepository;
    2331
    2432    @GetMapping
     
    4351    }
    4452
     53    @GetMapping("/flight/{wishlistId}")
     54    public ResponseEntity<List<Optional<Wishlist>>> getFlight(@PathVariable Long wishlistId){
     55        Optional<Wishlist> wishlist = wishlistRepository.getFlightFromWishlist(wishlistId);
     56        return ResponseEntity.ok(Collections.singletonList(wishlist));
     57    }
     58
    4559    @PostMapping
    4660    public ResponseEntity<String> updateWishlist(@RequestBody WishlistDTO wishlistRequest) {
  • src/main/java/com/example/skychasemk/repository/FlightRepository.java

    rfda671c r07fe0be  
    1919
    2020
     21    @Query(value="SELECT * FROM flight  WHERE flightId=:flightId",nativeQuery = true)
     22    List<Flight> getFlightByFlightId(Long flightId);
    2123}
  • src/main/java/com/example/skychasemk/repository/WishlistRepository.java

    rfda671c r07fe0be  
    1414
    1515import java.util.List;
     16import java.util.Optional;
    1617
    1718@Repository
     
    2930    void deleteByUserIdAndTargetId(@Param("userId") Integer userId, @Param("targetId") Integer targetId);
    3031
     32    @Query("SELECT w.targetId FROM Wishlist w WHERE w.wishlistID = :wishlistId")
     33    Optional<Wishlist> getFlightFromWishlist(@Param("wishlistId") Long wishlistId);
    3134}
  • src/main/resources/static/Wishlist.html

    rfda671c r07fe0be  
    120120<body>
    121121<div class="header">
    122     <img src="/images/home.png" alt="Home Icon">
     122    <img src="/images/home.png" alt="Home Icon" @click="home">
    123123    <h1>SkyChase</h1>
    124124    <button @click="home">Log Out</button>
     
    133133            <br>
    134134            <br>
    135             <button @click="bookFlights"  class="book">
     135            <button @click="bookFlights(item.wishlistID)"  class="book">
    136136                Book
    137137            </button>
     
    145145        data: {
    146146            userId: '',
    147             wishlist: [] // Array to hold the wishlist items
     147            wishlist: [],
     148            wishlistId:''
    148149        },
    149150        mounted() {
     
    156157                try {
    157158                    const response = await fetch(`/api/wishlists/${this.userId}`);
    158                     this.wishlist = await response.json(); // Directly assign the result to the wishlist data
     159                    this.wishlist = await response.json();
    159160                } catch (error) {
    160161                    console.error('Error fetching wishlist:', error);
    161162                }
    162163            },
    163             bookFlights() {
    164                 if (!this.selectedFlights.length) {
    165                     alert("Please select at least one flight.");
    166                     return;
    167                 }
    168 
    169                 const flight = this.selectedFlights[0];
    170                 console.log(flight);
    171                 const totalCost = flight.price;
    172 
    173                 const bookingData = {
    174                     flightId: flight.flightID,
    175                     bookingDate: new Date().toISOString().split('T')[0],
    176                     status: 'PENDING',
    177                     totalCost: totalCost,
    178                     userId:this.userId
    179                 };
    180                 axios.post('/api/bookings', bookingData)
     164            bookFlights(wishlistID) {
     165                console.log(wishlistID);
     166
     167                axios.get(`/api/wishlists/flight/${encodeURIComponent(wishlistID)}`)
    181168                    .then(response => {
    182                         const bookingID = response.data.bookingId;
    183                         alert("Booked successfully!");
    184                         window.location.href = `/transaction?amount=${encodeURIComponent(totalCost)}&bookingId=${encodeURIComponent(bookingID)}&flightId=${encodeURIComponent(flight.flightID)}&userId=${encodeURIComponent(this.userId)}`;
     169                        const flightId = response.data;
     170                        console.log(flightId);
     171                        axios.get(`/api/flights/${encodeURIComponent(flightId)}`)
     172                            .then(response => {
     173                                const flightData = response.data;
     174                                console.log(flightId);
     175                                const bookingData = {
     176                                    flightId: flightData.flightID,
     177                                    bookingDate: new Date().toISOString().split('T')[0],
     178                                    status: 'PENDING',
     179                                    totalCost: flightData.price,
     180                                    userId:this.userId
     181                                };
     182                                axios.post('/api/bookings', bookingData)
     183                                    .then(response => {
     184                                        const bookingID = response.data.bookingId;
     185                                        alert("Booked successfully!");
     186                                        window.location.href = `/transaction?amount=${encodeURIComponent(flightData.price)}&bookingId=${encodeURIComponent(bookingID)}&flightId=${encodeURIComponent(flightData.flightID)}&userId=${encodeURIComponent(this.userId)}`;
     187                                    })
     188                                    .catch(error => {
     189                                        console.error("Error booking flight", error);
     190                                        alert("There was an error creating your booking. Please try again.");
     191                                    });
     192                            })
     193                            .catch(error =>{
     194                                console.error(error)
     195                                }
     196                            )
    185197                    })
    186198                    .catch(error => {
    187                         console.error("Error booking flight", error);
    188                         alert("There was an error creating your booking. Please try again.");
     199                        console.error("Error fetching flight", error);
    189200                    });
     201
     202            },
     203            home() {
     204                window.location.href = '/';
    190205            }
    191206        }
Note: See TracChangeset for help on using the changeset viewer.