Changeset 0a7426e
- Timestamp:
- 01/12/25 17:15:36 (3 days ago)
- Branches:
- master
- Children:
- df7f390
- Parents:
- cd64b06
- Location:
- backend/GlobeGuru-backend
- Files:
-
- 4 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 -
backend/GlobeGuru-backend/src/main/java/Option.java
rcd64b06 r0a7426e 3 3 public class Option { 4 4 private int id; 5 private int detail_id; 5 6 private String hotelName; 6 7 private String country; … … 24 25 public void setDateRange(String dateRange) { 25 26 this.dateRange = dateRange; 27 } 28 29 public int getDetail_id() { 30 return detail_id; 31 } 32 33 public void setDetail_id(int detail_id) { 34 this.detail_id = detail_id; 26 35 } 27 36 -
backend/GlobeGuru-backend/src/main/java/ScraperThread.java
rcd64b06 r0a7426e 14 14 import org.openqa.selenium.support.ui.WebDriverWait; 15 15 16 import javax.xml.crypto.Data; 16 17 import java.io.File; 17 18 import java.io.IOException; … … 164 165 }else continue; 165 166 166 DatabaseUtil.saveOptionDetails(option.getId(), type,board,amenity, price); 167 //Check for changes 168 int odId = checkForChanges(option.getId(), type, board,amenity,price); 169 if(odId != 0) { //true = changes found - update details 170 DatabaseUtil.updateOptionDetails(odId,type,board,amenity,price); 171 }else{ //false = not found / no changes - save regular 172 DatabaseUtil.saveOptionDetails(option.getId(), type, board, amenity, price); 173 } 167 174 } 168 175 } … … 194 201 } 195 202 System.out.println(type + board + price + amenities); 196 DatabaseUtil.saveOptionDetails(option.getId(), type, board, amenities.toString(), price); 197 } 198 199 } 203 int odId = checkForChanges(option.getId(), type, board,amenities.toString(),price); 204 if(odId != 0) { //true = changes found - update details 205 DatabaseUtil.updateOptionDetails(odId,type,board,amenities.toString(),price); 206 }else{ //false = not found / no changes - save regular 207 DatabaseUtil.saveOptionDetails(option.getId(), type, board, amenities.toString(), price); 208 } 209 } 210 211 } 212 } 213 private int checkForChanges(int id, String type, String board, String amenities, float price){ //return true for changes, false for no changes 214 try { 215 List<Option> pooled = DatabaseUtil.poolOptionDetails(id); 216 if (pooled.isEmpty()) { //not saved = no changes - save regular 217 return 0; 218 }else{ //got the options saved details 219 for(Option o : pooled){ 220 if(o.getType().equals(type) && o.getBoard().equals(board)){//for the room and board check amenity and price changes (Assumption type of room and board do not change) 221 if((!o.getAmenities().equals(amenities)) || o.getPrice() != price){ 222 return o.getDetail_id(); //Change 223 } 224 } 225 } 226 } 227 }catch(SQLException e){ 228 e.printStackTrace(); 229 } 230 return 0; //no changes detected 200 231 } 201 232 private Option optionParser(String data, int numPeople){ … … 218 249 return null; 219 250 } 220 //scrapeOptionInfo(created);221 251 return created; 222 252 } … … 235 265 Element countryElement = doc.selectFirst("l.ponuda-lokacija"); 236 266 created.setCountry(countryElement != null ? countryElement.text() : null); 237 //Element priceElement = doc.selectFirst("div.ponuda-cena");238 267 Element dateElement = doc.selectFirst("l.ponuda-opis.termin"); 239 268 created.setDateRange(dateElement != null ? dateElement.text() : null); 240 /*float price = Float.parseFloat(priceElement != null ? priceElement.text().replaceAll("[^\\d.]", "") : "0");241 created.setPrice(price);*/242 269 return created; 243 270 } … … 252 279 String country = countryP.text().replaceAll("leto hoteli", ""); 253 280 created.setCountry(country); 254 /*Element priceElem = doc.selectFirst("span.hotel-price");255 String priceText = priceElem.text();256 float price = 0;257 if(!priceText.isEmpty()) {258 price = Float.parseFloat(priceText.replace("€", ""));259 }260 created.setPrice(price);*/261 281 String[] queryParams = link.split("[?&]"); 262 282 String startDateStr = null; -
backend/GlobeGuru-backend/target/classes/URLsJSON.json
rcd64b06 r0a7426e 2 2 "agencyurls": 3 3 [ 4 "https://magelantravel.mk/", 4 5 "https://booking.escapetravel.mk/" 5 6 ]
Note:
See TracChangeset
for help on using the changeset viewer.