- Timestamp:
- 01/10/25 00:33:50 (3 weeks ago)
- Branches:
- master
- Children:
- 1c51912
- Parents:
- c164f8f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
backend/GlobeGuru-backend/src/main/java/DatabaseUtil.java
rc164f8f r53bad7e 31 31 "price REAL, " + 32 32 "dateRange TEXT, " + 33 "numberOfPeople INTEGER, " + 33 34 "isPriceChanged BOOLEAN DEFAULT 0, " + 34 35 "newPrice REAL DEFAULT 0)" … … 55 56 stmt.setString(1, username); 56 57 stmt.setString(2, email); 57 stmt.setString(3, password); // Store hashed58 stmt.setString(3, password); 58 59 return stmt.executeUpdate() > 0; 59 60 } … … 69 70 String storedPassword = rs.getString("password"); 70 71 if (password == null) { 71 // Assume this is aGoogle login72 // Google login 72 73 return storedPassword == null; 73 74 } 74 return password.equals(storedPassword); // Check hashed password75 return password.equals(storedPassword); 75 76 } 76 77 } … … 92 93 try (ResultSet rs = selectStmt.executeQuery()) { 93 94 if (rs.next()) { 94 // User exists, delete the user and their favourite options95 95 deleteStmt.setInt(1, userId); 96 96 int rowsAffected = deleteStmt.executeUpdate(); … … 101 101 return rowsAffected > 0; 102 102 } else { 103 // User does not exist104 103 return false; 105 104 } … … 135 134 } 136 135 137 public static List<Option> queryOptions(String destination, String dateQuery, boolean dateFlag) throws SQLException {136 public static List<Option> queryOptions(String destination, String dateQuery, int numPeople, boolean dateFlag) throws SQLException { 138 137 List<Option> options = new ArrayList<>(); 139 138 String sql = "SELECT * FROM options WHERE (country LIKE ? OR hotelName LIKE ?)"; … … 143 142 } //append date 144 143 if (dateFlag) { //search only from dates 145 sql += "AND dateRange LIKE ?"; 146 } 144 sql += " AND dateRange LIKE ?"; 145 } 146 if(numPeople != 0) { //with number of people 147 sql += " AND numberOfPeople = ?"; 148 } 149 147 150 System.out.println("Searching for dest:" + destination + "\n" + sql); 148 151 try (Connection conn = getConnection(); … … 156 159 stmt.setString(3, dateQuery + "%"); 157 160 } 158 // Execute query 161 if(numPeople != 0) { 162 stmt.setInt(4, numPeople); 163 } 159 164 try (ResultSet rs = stmt.executeQuery()) { 160 165 while (rs.next()) { … … 167 172 option.setPrice(rs.getFloat("price")); 168 173 option.setDateRange(rs.getString("dateRange")); 174 option.setNumPeople(rs.getInt("numberOfPeople")); 169 175 options.add(option); 170 176 } … … 185 191 } 186 192 187 //TODO add frontend 193 188 194 public static boolean removeFavoriteOption(int userId, int optionId) throws SQLException { 189 195 String sql = "DELETE FROM savedOptions WHERE userId = ? AND optionId = ?"; … … 269 275 270 276 public static void saveOptionToDatabase(Option option) { 271 String sql = "INSERT INTO options (link, imgSrc, hotelName, country, price, dateRange, isPriceChanged, newPrice) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";277 String sql = "INSERT INTO options (link, imgSrc, hotelName, country, price, dateRange,numberOfPeople, isPriceChanged, newPrice) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; 272 278 try (Connection conn = DriverManager.getConnection("jdbc:sqlite:globe_guru.db"); 273 279 PreparedStatement stmt = conn.prepareStatement(sql)) { … … 278 284 stmt.setFloat(5, option.getPrice()); 279 285 stmt.setString(6, option.getDateRange()); 280 stmt.setBoolean(7, option.isPriceChanged()); 281 stmt.setFloat(8, option.getNewPrice()); 286 stmt.setInt(7,option.getNumPeople()); 287 stmt.setBoolean(8, option.isPriceChanged()); 288 stmt.setFloat(9, option.getNewPrice()); 282 289 stmt.executeUpdate(); 283 290 } catch (SQLException e) { … … 293 300 PreparedStatement stmt = conn.prepareStatement(sql)){ 294 301 stmt.executeUpdate(); 295 //Remake the options DB296 302 initializeDatabase(); 297 303 … … 334 340 existingOption.setPriceChanged(rs.getBoolean("isPriceChanged")); 335 341 existingOption.setNewPrice(rs.getInt("newPrice")); 342 existingOption.setNumPeople(rs.getInt("numberOfPeople")); 336 343 return existingOption; 337 344 }
Note:
See TracChangeset
for help on using the changeset viewer.