- Timestamp:
- 01/12/25 17:15:36 (3 days ago)
- Branches:
- master
- Children:
- df7f390
- Parents:
- cd64b06
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
backend/GlobeGuru-backend/src/main/java/DatabaseUtil.java
rcd64b06 r0a7426e 72 72 } 73 73 } 74 75 74 public static boolean authenticateUser(String email, String password) throws SQLException { 76 75 String sql = "SELECT password FROM users WHERE email = ?"; … … 91 90 return false; 92 91 } 93 94 92 public static boolean deleteUser(int userId) throws SQLException { 95 93 String selectSql = "SELECT userId FROM users WHERE userId = ?"; … … 118 116 } 119 117 } 120 121 118 public static boolean userExists(String email) throws SQLException { 122 119 String query = "SELECT COUNT(*) FROM users WHERE email = ?"; … … 131 128 return false; 132 129 } 133 134 130 public static boolean isAdmin(String email) throws SQLException { 135 131 String selectSql = "SELECT isAdmin FROM users WHERE email = ?"; … … 145 141 return false; 146 142 } 147 143 public static boolean saveFavoriteOption(int userId, int detailId) throws SQLException { 144 String sql = "INSERT INTO savedOptions (userId, detailId) VALUES (?, ?) ON CONFLICT DO NOTHING"; 145 try (Connection conn = getConnection(); 146 PreparedStatement stmt = conn.prepareStatement(sql)) { 147 stmt.setInt(1, userId); 148 stmt.setInt(2, detailId); 149 return stmt.executeUpdate() > 0; 150 } 151 } 152 public static boolean removeFavoriteOption(int userId, int detailId) throws SQLException { 153 String sql = "DELETE FROM savedOptions WHERE userId = ? AND detailId = ?"; 154 try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { 155 stmt.setInt(1, userId); 156 stmt.setInt(2, detailId); 157 return stmt.executeUpdate() > 0; 158 } 159 } 160 public static List<Option> getSavedTripsByUser(int userId) throws SQLException { 161 List<Option> savedTrips = new ArrayList<>(); 162 String sql = "SELECT od.id AS detail_id, o.*, od.* FROM savedOptions so " + 163 "JOIN optionDetails od ON so.detailId = od.id " + 164 "JOIN options o ON od.optionId = o.id " + 165 "WHERE so.userId = ?"; 166 try (Connection conn = getConnection(); 167 PreparedStatement stmt = conn.prepareStatement(sql)) { 168 stmt.setInt(1, userId); 169 try (ResultSet rs = stmt.executeQuery()) { 170 while (rs.next()) { 171 Option option = new Option(); 172 option.setId(rs.getInt("detail_id")); 173 option.setLink(rs.getString("link")); 174 option.setImgSrc(rs.getString("imgSrc")); 175 option.setHotelName(rs.getString("hotelName")); 176 option.setCountry(rs.getString("country")); 177 option.setDateRange(rs.getString("dateRange")); 178 option.setNumPeople(rs.getInt("numberOfPeople")); 179 option.setType(rs.getString("type")); 180 option.setBoard(rs.getString("board")); 181 option.setAmenities(rs.getString("amenities")); 182 option.setPrice(rs.getFloat("price")); 183 savedTrips.add(option); 184 } 185 } 186 } 187 return savedTrips; 188 } 189 public static int getUserIdByEmail(String email) throws SQLException { 190 String sql = "SELECT id FROM users WHERE email = ?"; 191 try (Connection conn = getConnection(); 192 PreparedStatement stmt = conn.prepareStatement(sql)) { 193 stmt.setString(1, email); 194 try (ResultSet rs = stmt.executeQuery()) { 195 if (rs.next()) { 196 return rs.getInt("id"); 197 } else { 198 throw new SQLException("User not found"); 199 } 200 } 201 } 202 } 203 204 205 206 public static List<Option> poolOptionDetails(int id) throws SQLException{ 207 String sql = "SELECT * FROM optionDetails WHERE optionId = ?"; 208 List<Option> options = new ArrayList<>(); 209 try(Connection conn = getConnection(); 210 PreparedStatement stmt = conn.prepareStatement(sql)) { 211 stmt.setInt(1,id); 212 try(ResultSet rs = stmt.executeQuery()){ 213 while (rs.next()){ 214 Option o = new Option(); 215 o.setAmenities(rs.getString("amenities")); 216 o.setType(rs.getString("type")); 217 o.setBoard(rs.getString("board")); 218 o.setPrice(rs.getFloat("price")); 219 o.setId(id); 220 o.setDetail_id(rs.getInt("id")); 221 } 222 } 223 224 } 225 return options; 226 } 148 227 public static List<Option> queryOptions(String destination, String dateQuery, int numPeople, boolean dateFlag) throws SQLException { 149 228 List<Option> options = new ArrayList<>(); … … 203 282 return options; 204 283 } 205 206 207 208 public static boolean saveFavoriteOption(int userId, int detailId) throws SQLException {209 String sql = "INSERT INTO savedOptions (userId, detailId) VALUES (?, ?) ON CONFLICT DO NOTHING";210 try (Connection conn = getConnection();211 PreparedStatement stmt = conn.prepareStatement(sql)) {212 stmt.setInt(1, userId);213 stmt.setInt(2, detailId);214 return stmt.executeUpdate() > 0;215 }216 }217 218 219 220 public static boolean removeFavoriteOption(int userId, int detailId) throws SQLException {221 String sql = "DELETE FROM savedOptions WHERE userId = ? AND detailId = ?";222 try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {223 stmt.setInt(1, userId);224 stmt.setInt(2, detailId);225 return stmt.executeUpdate() > 0;226 }227 }228 229 230 public static List<Option> getSavedTripsByUser(int userId) throws SQLException {231 List<Option> savedTrips = new ArrayList<>();232 String sql = "SELECT od.id AS detail_id, o.*, od.* FROM savedOptions so " +233 "JOIN optionDetails od ON so.detailId = od.id " +234 "JOIN options o ON od.optionId = o.id " +235 "WHERE so.userId = ?";236 try (Connection conn = getConnection();237 PreparedStatement stmt = conn.prepareStatement(sql)) {238 stmt.setInt(1, userId);239 try (ResultSet rs = stmt.executeQuery()) {240 while (rs.next()) {241 Option option = new Option();242 option.setId(rs.getInt("detail_id"));243 option.setLink(rs.getString("link"));244 option.setImgSrc(rs.getString("imgSrc"));245 option.setHotelName(rs.getString("hotelName"));246 option.setCountry(rs.getString("country"));247 option.setDateRange(rs.getString("dateRange"));248 option.setNumPeople(rs.getInt("numberOfPeople"));249 option.setType(rs.getString("type"));250 option.setBoard(rs.getString("board"));251 option.setAmenities(rs.getString("amenities"));252 option.setPrice(rs.getFloat("price"));253 savedTrips.add(option);254 }255 }256 }257 return savedTrips;258 }259 260 261 262 public static int getUserIdByEmail(String email) throws SQLException {263 String sql = "SELECT id FROM users WHERE email = ?";264 try (Connection conn = getConnection();265 PreparedStatement stmt = conn.prepareStatement(sql)) {266 stmt.setString(1, email);267 try (ResultSet rs = stmt.executeQuery()) {268 if (rs.next()) {269 return rs.getInt("id");270 } else {271 throw new SQLException("User not found");272 }273 }274 }275 }276 277 public static int getCurrentOptionsCount() throws SQLException {278 String sql = "SELECT COUNT(*) AS optionsCount FROM options";279 try (Connection conn = getConnection();280 PreparedStatement stmt = conn.prepareStatement(sql);281 ResultSet rs = stmt.executeQuery()) {282 if (rs.next()) {283 return rs.getInt("optionsCount");284 } else {285 return 0;286 }287 }288 }289 290 public static int getChangedOptionsCountSinceLastUpdate() throws SQLException, IOException {291 LocalDateTime lastUpdateTime = Server.getLastUpdateTime();292 if (lastUpdateTime == null) {293 return 0;294 }295 String sql = "SELECT COUNT(*) AS changedOptionsCount FROM options WHERE lastModified > ?";296 try (Connection conn = getConnection();297 PreparedStatement stmt = conn.prepareStatement(sql)) {298 stmt.setTimestamp(1, Timestamp.valueOf(lastUpdateTime));299 try (ResultSet rs = stmt.executeQuery()) {300 if (rs.next()) {301 return rs.getInt("changedOptionsCount");302 } else {303 return 0;304 }305 }306 }307 }308 309 284 public static int saveOptionToDatabase(Option option) { 310 285 String sql = "INSERT INTO options (link, imgSrc, hotelName, country, dateRange,numberOfPeople, isPriceChanged, newPrice) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; … … 331 306 return id; 332 307 } 333 334 308 public static void saveOptionDetails(int id, String type, String board, String amenity, float price) { 335 309 String sql = "INSERT INTO optionDetails (optionId, type, board, amenities, price) VALUES (?, ?, ?, ?, ?)"; … … 347 321 } 348 322 } 349 350 323 public static void dropOptions() throws SQLException { 351 324 String sql = "DROP TABLE options"; … … 358 331 } 359 332 } 333 334 public static void updateOptionDetails(int detail_id, String type, String board, String amenities, float price) { 335 String sql = "UPDATE optionDetails SET "; 336 337 List<String> updates = new ArrayList<>(); 338 if (type != null && !type.isEmpty()) updates.add("type = '" + type + "'"); 339 if (board != null && !board.isEmpty()) updates.add("board = '" + board + "'"); 340 if (amenities != null && !amenities.isEmpty()) updates.add("amenities = '" + amenities + "'"); 341 updates.add("price = " + price); 342 343 sql += String.join(", ", updates); 344 sql += " WHERE id = " + detail_id; 345 346 try (Connection conn = getConnection(); 347 PreparedStatement stmt = conn.prepareStatement(sql)) { 348 stmt.executeUpdate(); 349 } catch (SQLException e) { 350 e.printStackTrace(); 351 } 352 } 353 360 354 public static void updateOptionInDatabase(Option option) { 361 355 String sql = "UPDATE options SET link = ?, imgSrc = ?, hotelName = ?, country = ?, price = ?, dateRange = ?, isPriceChanged = ?, newPrice = ? WHERE id = ?"; … … 376 370 } 377 371 } 378 379 372 public static Option findOption(Option option) { 380 373 String sql = "SELECT * FROM options WHERE id = ?"; … … 404 397 } 405 398 406 public static List<Option> fetchAllOptions() { 407 List<Option> options = new ArrayList<>(); 408 String sql = "SELECT * FROM options"; 409 410 try (Connection conn = DriverManager.getConnection("jdbc:sqlite:globe_guru.db"); 399 400 public static int getCurrentOptionsCount() throws SQLException { 401 String sql = "SELECT COUNT(*) AS optionsCount FROM options"; 402 try (Connection conn = getConnection(); 411 403 PreparedStatement stmt = conn.prepareStatement(sql); 412 404 ResultSet rs = stmt.executeQuery()) { 413 while (rs.next()) { 414 Option option = new Option(); 415 option.setId(rs.getInt("id")); 416 option.setLink(rs.getString("link")); 417 option.setImgSrc(rs.getString("imgSrc")); 418 option.setHotelName(rs.getString("hotelName")); 419 option.setCountry(rs.getString("country")); 420 option.setPrice(rs.getFloat("price")); 421 option.setDateRange(rs.getString("dateRange")); 422 option.setPriceChanged(rs.getBoolean("isPriceChanged")); 423 option.setNewPrice(rs.getInt("newPrice")); 424 option.setNumPeople(rs.getInt("numberOfPeople")); 425 options.add(option); 426 } 427 428 } catch (SQLException e) { 429 e.printStackTrace(); 430 } 431 return options; 405 if (rs.next()) { 406 return rs.getInt("optionsCount"); 407 } else { 408 return 0; 409 } 410 } 411 } 412 public static int getChangedOptionsCountSinceLastUpdate() throws SQLException, IOException { 413 LocalDateTime lastUpdateTime = Server.getLastUpdateTime(); 414 if (lastUpdateTime == null) { 415 return 0; 416 } 417 String sql = "SELECT COUNT(*) AS changedOptionsCount FROM options WHERE lastModified > ?"; 418 try (Connection conn = getConnection(); 419 PreparedStatement stmt = conn.prepareStatement(sql)) { 420 stmt.setTimestamp(1, Timestamp.valueOf(lastUpdateTime)); 421 try (ResultSet rs = stmt.executeQuery()) { 422 if (rs.next()) { 423 return rs.getInt("changedOptionsCount"); 424 } else { 425 return 0; 426 } 427 } 428 } 432 429 } 433 430
Note:
See TracChangeset
for help on using the changeset viewer.