From 66853af8e794f26f7ff4888fab309a40732ef50c Mon Sep 17 00:00:00 2001 From: Youssef Amazzal <79524624+youssef-amazzal@users.noreply.github.com> Date: Tue, 30 Aug 2022 18:50:22 +0100 Subject: [PATCH] issue resolved: favorite flights appear to all issue resolved: favorite flights doesn't belong to user appears in his account Co-Authored-By: imane_quabbou <103893606+imane12j@users.noreply.github.com> --- .../src/main/java/data/FavoriteDao.java | 25 +++++++++++++++++++ .../src/main/java/models/Account.java | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/FlightBookingApplication/src/main/java/data/FavoriteDao.java b/FlightBookingApplication/src/main/java/data/FavoriteDao.java index 88dd456..7bf3f0e 100644 --- a/FlightBookingApplication/src/main/java/data/FavoriteDao.java +++ b/FlightBookingApplication/src/main/java/data/FavoriteDao.java @@ -119,6 +119,31 @@ public List readAll() { } } + public List readAll(Account account) { + Connection conn = DataSource.getConnection(); + List list = new ArrayList<>(); + + try { + PreparedStatement query = conn.prepareStatement("SELECT * FROM favorites WHERE id_account = ?;"); + query.setInt(1, account.getId()); + ResultSet res = query.executeQuery(); + + while (res.next()) { + Favorite favorite = new Favorite(); + favorite.setId(res.getInt("id")); + favorite.setFlight(flightDao.read(res.getInt("id_flight"))); + favorite.setAccount((res.getInt("id_account"))); + + list.add(favorite); + favoritesMap.put(res.getInt("id_flight"), favorite); + } + return list; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + @Override public void update(int id, Favorite favorite) { Connection conn = DataSource.getConnection(); diff --git a/FlightBookingApplication/src/main/java/models/Account.java b/FlightBookingApplication/src/main/java/models/Account.java index fc75516..aa364db 100644 --- a/FlightBookingApplication/src/main/java/models/Account.java +++ b/FlightBookingApplication/src/main/java/models/Account.java @@ -85,7 +85,7 @@ public Reservation getReservation(Flight flight) { public ObservableList getFavoriteFlights() { FavoriteDao favoriteDao = new FavoriteDao(); - List favoriteList = favoriteDao.readAll(); + List favoriteList = favoriteDao.readAll(this); List favoriteFlights = new ArrayList<>(); favoriteList.forEach(favorite -> favoriteFlights.add(favorite.getFlight())); return FXCollections.observableList(favoriteFlights);