//# Function to list of cars that are eligible for users request public List<Car> GetCars(InputRequestAttributes RequestQuery) { //# Get the list of zipcodes that are located in specified distance from user requested zipcode ConnectedRepository ctx = new ConnectedRepository(DbName); LocationDataLayer lctx = new LocationDataLayer(); List<string> NearByZipCodes = new List<string>(); if (!string.IsNullOrEmpty(RequestQuery.Zipcode)) { List<Location> locationsAround = lctx.GetNearZipcodes(RequestQuery.Zipcode,RequestQuery.distance); if (locationsAround.Count != 0) { foreach (Location location in locationsAround) { NearByZipCodes.Add(location.zip); } NearByZipCodes.Add(RequestQuery.Zipcode); } } //# Build a DB query based on user constraint. Check request if it has value and add it to query. QueryDocument CarQuery = new QueryDocument(); if (RequestQuery.Year > 2000) CarQuery.Add("Year", RequestQuery.Year); if (RequestQuery.Name != null) CarQuery.Add("Name", RequestQuery.Name); if (RequestQuery.Maker != null) CarQuery.Add("Maker", RequestQuery.Maker); if (RequestQuery.MaxPrice >= 1000) CarQuery.Add("Price", new BsonDocument("$lt", RequestQuery.MaxPrice)); if (NearByZipCodes.Count() != 0) CarQuery.Add("Zipcode", new BsonDocument("$in", new BsonArray(NearByZipCodes))); MongoCursor<Car> Cars = ctx.Cars.Find(CarQuery); List<Car> carsList = Cars.ToList(); return carsList; }
public List<Location> GetNearZipcodes(string zip, int distance) { LocationDataLayer lctx = new LocationDataLayer(); QueryDocument ZipQuery = new QueryDocument(); ZipQuery.Add("zip", zip); double distance1 = distance * (1/69.0); Location GeoLocation = lctx.zips.FindOne(ZipQuery); if (GeoLocation != null) { var query = Query.Near("loc", GeoLocation.loc.lat, GeoLocation.loc.lon, distance1); MongoCursor<Location> Zips = zips.Find(query); List<Location> NearbyZips = Zips.ToList(); return NearbyZips; } else { // need to handle Exception in much better way return null; } }