- Timestamp:
- 02/24/25 22:49:01 (3 months ago)
- Branches:
- master
- Children:
- c064a42
- Parents:
- fda671c
- Location:
- src/main
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/skychasemk/controller/FlightController.java
rfda671c r07fe0be 33 33 } 34 34 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) { 37 37 return flightService.getFlightById(flightID); 38 38 } -
src/main/java/com/example/skychasemk/controller/WishlistController.java
rfda671c r07fe0be 2 2 3 3 import com.example.skychasemk.dto.WishlistDTO; 4 import com.example.skychasemk.model.Flight; 4 5 import com.example.skychasemk.model.Wishlist; 6 import com.example.skychasemk.repository.BookingRepository; 7 import com.example.skychasemk.repository.FlightRepository; 5 8 import com.example.skychasemk.repository.WishlistRepository; 6 9 import com.example.skychasemk.services.WishlistService; … … 10 13 import org.springframework.web.bind.annotation.*; 11 14 15 import java.util.Collections; 12 16 import java.util.List; 17 import java.util.Optional; 13 18 14 19 @RestController … … 21 26 @Autowired 22 27 private WishlistRepository wishlistRepository; 28 29 @Autowired 30 private FlightRepository flightRepository; 23 31 24 32 @GetMapping … … 43 51 } 44 52 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 45 59 @PostMapping 46 60 public ResponseEntity<String> updateWishlist(@RequestBody WishlistDTO wishlistRequest) { -
src/main/java/com/example/skychasemk/repository/FlightRepository.java
rfda671c r07fe0be 19 19 20 20 21 @Query(value="SELECT * FROM flight WHERE flightId=:flightId",nativeQuery = true) 22 List<Flight> getFlightByFlightId(Long flightId); 21 23 } -
src/main/java/com/example/skychasemk/repository/WishlistRepository.java
rfda671c r07fe0be 14 14 15 15 import java.util.List; 16 import java.util.Optional; 16 17 17 18 @Repository … … 29 30 void deleteByUserIdAndTargetId(@Param("userId") Integer userId, @Param("targetId") Integer targetId); 30 31 32 @Query("SELECT w.targetId FROM Wishlist w WHERE w.wishlistID = :wishlistId") 33 Optional<Wishlist> getFlightFromWishlist(@Param("wishlistId") Long wishlistId); 31 34 } -
src/main/resources/static/Wishlist.html
rfda671c r07fe0be 120 120 <body> 121 121 <div class="header"> 122 <img src="/images/home.png" alt="Home Icon" >122 <img src="/images/home.png" alt="Home Icon" @click="home"> 123 123 <h1>SkyChase</h1> 124 124 <button @click="home">Log Out</button> … … 133 133 <br> 134 134 <br> 135 <button @click="bookFlights " class="book">135 <button @click="bookFlights(item.wishlistID)" class="book"> 136 136 Book 137 137 </button> … … 145 145 data: { 146 146 userId: '', 147 wishlist: [] // Array to hold the wishlist items 147 wishlist: [], 148 wishlistId:'' 148 149 }, 149 150 mounted() { … … 156 157 try { 157 158 const response = await fetch(`/api/wishlists/${this.userId}`); 158 this.wishlist = await response.json(); // Directly assign the result to the wishlist data159 this.wishlist = await response.json(); 159 160 } catch (error) { 160 161 console.error('Error fetching wishlist:', error); 161 162 } 162 163 }, 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)}`) 181 168 .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 ) 185 197 }) 186 198 .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); 189 200 }); 201 202 }, 203 home() { 204 window.location.href = '/'; 190 205 } 191 206 }
Note:
See TracChangeset
for help on using the changeset viewer.