@Override public JSONObject getRestaurantsById(String businessId, boolean isVisited) { try { String sql = "SELECT * from restaurants where business_id = ?"; PreparedStatement statement = conn.prepareStatement(sql); statement.setString(1, businessId); ResultSet rs = statement.executeQuery(); if (rs.next()) { Restaurant restaurant = new Restaurant( rs.getString("business_id"), rs.getString("name"), rs.getString("categories"), rs.getString("city"), rs.getString("state"), rs.getString("full_address"), rs.getFloat("stars"), rs.getFloat("latitude"), rs.getFloat("longitude"), rs.getString("image_url"), rs.getString("url")); JSONObject obj = restaurant.toJSONObject(); obj.put("is_visited", isVisited); return obj; } } catch (Exception e) { /* report an error */ System.out.println(e.getMessage()); } return null; }
@Override public JSONArray searchRestaurants(String userId, double lat, double lon) { try { YelpAPI api = new YelpAPI(); JSONObject response = new JSONObject(api.searchForBusinessesByLocation(lat, lon)); JSONArray array = (JSONArray) response.get("businesses"); List<JSONObject> list = new ArrayList<JSONObject>(); Set<String> visited = getVisitedRestaurants(userId); for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); Restaurant restaurant = new Restaurant(object); String businessId = restaurant.getBusinessId(); String name = restaurant.getName(); String categories = restaurant.getCategories(); String city = restaurant.getCity(); String state = restaurant.getState(); String fullAddress = restaurant.getFullAddress(); double stars = restaurant.getStars(); double latitude = restaurant.getLatitude(); double longitude = restaurant.getLongitude(); String imageUrl = restaurant.getImageUrl(); String url = restaurant.getUrl(); JSONObject obj = restaurant.toJSONObject(); if (visited.contains(businessId)) { object.put("is_visited", true); } else { object.put("is_visited", false); } String sql = "INSERT IGNORE INTO restaurants VALUES (?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement statement = conn.prepareStatement(sql); statement.setString(1, businessId); statement.setString(2, name); statement.setString(3, categories); statement.setString(4, city); statement.setString(5, state); statement.setDouble(6, stars); statement.setString(7, fullAddress); statement.setDouble(8, latitude); statement.setDouble(9, longitude); statement.setString(10, imageUrl); statement.setString(11, url); statement.execute(); list.add(obj); } return new JSONArray(list); } catch (Exception e) { System.out.println(e.getMessage()); } return null; }