Skip to content

Commit

Permalink
add search route
Browse files Browse the repository at this point in the history
  • Loading branch information
vivitruong committed Mar 6, 2024
1 parent 8b952b7 commit 590d1b2
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/api/search_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from flask import Blueprint, jsonify
from app.models import db, Product, Review, Image, User


search = Blueprint('search', __name__)


@search.route("/<keyword>")
def default_search(keyword):
products = db.session.query(Product).filter(Product.name.ilike(f"%{keyword}%")).all()

product_details = []

if products is not None:
for product in products:
product_id = product.to_dict_product_id()['id']
product = product.to_dict()

main_image = db.session.query(Image).filter(Image.product_id == product_id).first()

reviews_for_product = db.session.query(Review).filter(Review.product_id == product_id).all()

seller = db.session.query(User).filter(User.id == product['seller_id']).first()

sum_stars = 0
if len(reviews_for_product) > 0 :
for review in reviews_for_product:
sum_stars += review.to_dict_stars()['stars']

avg = sum_stars // len(reviews_for_product)
product['avg_stars'] = avg

product['images'] = [main_image.to_url()]
product['num_reviews'] = len(reviews_for_product)
product['shop_name'] = seller.shop_name

product_details.append(product)

return jsonify(product_details), 200

0 comments on commit 590d1b2

Please sign in to comment.