Skip to content

Commit

Permalink
Backend: refactored and unified fetching offers from db based on diff…
Browse files Browse the repository at this point in the history
…erent filters (#30)
  • Loading branch information
lwitkowski authored Aug 6, 2024
1 parent 7dfbddc commit a8cb0f3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
4 changes: 2 additions & 2 deletions backend/api/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def aircraft_models():

@app.route('/api/offers')
def offers():
return jsonify(db.get_offers_dict(aircraft_type=request.args.get('aircraft_type'),
return jsonify(db.get_offers(aircraft_type=request.args.get('aircraft_type'),
offset=int(request.args.get('offset') or '0'),
limit=int(request.args.get('limit') or '30')))

Expand All @@ -28,7 +28,7 @@ def model_information(manufacturer, model):
abort(404)
manufacturer_info = manufacturers[manufacturer]
del (manufacturer_info["models"]) # remove models info
manufacturer_info["offers"] = db.get_offers_for_model(manufacturer, model)
manufacturer_info["offers"] = db.get_offers(manufacturer=manufacturer, model=model)
return jsonify(manufacturer_info)

if __name__ == '__main__':
Expand Down
31 changes: 11 additions & 20 deletions backend/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,26 @@ def convert_prices(offers, session):
return offers


def get_offers_for_model(manufacturer, model):
def get_offers(offset: int = 0, limit: int = 30, aircraft_type=None, manufacturer=None, model=None):
session = Session()
try:
offers = session.query(AircraftOffer)
offers = offers.filter(AircraftOffer.manufacturer == manufacturer)
offers = offers.filter(AircraftOffer.model == model)
offers = offers.order_by(AircraftOffer.date.asc())
offers = offers.all()

ret = [offer.as_dict() for offer in offers]
ret = convert_prices(ret, session)
return ret
finally:
session.close()

offers = (session.query(AircraftOffer)
.order_by(AircraftOffer.date.desc()))

def get_offers_dict(limit: int = 30, offset: int = 0, aircraft_type=None):
session = Session()
try:
offers = session.query(AircraftOffer).order_by(AircraftOffer.date.desc())
if aircraft_type is not None:
offers = offers.filter(AircraftOffer.aircraft_type == aircraft_type)
else:
offers = offers.filter(AircraftOffer.aircraft_type is not None)

offers = offers.limit(min(limit, 100))
if manufacturer is not None:
offers = offers.filter(AircraftOffer.manufacturer == manufacturer)

if model is not None:
offers = offers.filter(AircraftOffer.model == model)

offers = offers.offset(offset or 0)
offers = offers.limit(min(limit, 300))

if offset is not None:
offers = offers.offset(offset)
offers = offers.all()

ret = [offer.as_dict() for offer in offers]
Expand Down
32 changes: 30 additions & 2 deletions backend/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,36 @@ def test_should_store_and_fetch_offer(self):
db.store_offer(sample_offer)

# then
all_gliders_in_db = db.get_offers_dict(aircraft_type="glider")
all_gliders_in_db = db.get_offers()
self.assertEqual(len(all_gliders_in_db), 1)
self.assertEqual(all_gliders_in_db[0]["title"], "Glider A")

def test_should_filter_offers_by_aircraft_type(self):
# given
sample_offer = buildOfferWithUrl("https://offers.com/1")
db.store_offer(sample_offer)

# when
gliders_only = db.get_offers(aircraft_type="glider")
airplanes_only = db.get_offers(aircraft_type="airplane")

# then
self.assertEqual(len(gliders_only), 1)
self.assertEqual(len(airplanes_only), 0)

def test_should_filter_offers_by_manufacturer_and_model(self):
# given
sample_offer = buildOfferWithUrl("https://offers.com/1")
db.store_offer(sample_offer)

# when
mini_nimbuses = db.get_offers(manufacturer="Schempp-Hirth", model="Mini-Nimbus")
asg29s = db.get_offers(manufacturer="Alexander Schleicher", model="ASG 29 E")

# then
self.assertEqual(len(mini_nimbuses), 1)
self.assertEqual(len(asg29s), 0)

def test_should_check_url_exists(self):
# given offer exists in db
db.store_offer(buildOfferWithUrl("https://offers.com/1"))
Expand All @@ -45,7 +71,9 @@ def buildOfferWithUrl(url):
offer_url=url,
spider="segelflug_de_kleinanzeigen",
detail_text="does not matter to much",
aircraft_type="glider"
aircraft_type="glider",
manufacturer="Schempp-Hirth",
model="Mini-Nimbus"
)


Expand Down
2 changes: 1 addition & 1 deletion backend/tests/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_should_store_offer(self):
self.storage.process_item(sample_raw_offer, None)

# then
all_gliders_in_db = db.get_offers_dict(aircraft_type="glider")
all_gliders_in_db = db.get_offers(aircraft_type="glider")
self.assertEqual(len(all_gliders_in_db), 1)
self.assertEqual(all_gliders_in_db[0]["title"], "Glider A")
self.assertTrue(db.offer_url_exists("https://offers.com/1"))
Expand Down

0 comments on commit a8cb0f3

Please sign in to comment.