Skip to content

Commit

Permalink
Added search engine for friends; fixed routing and rendered template …
Browse files Browse the repository at this point in the history
…for search engine for restaurants
  • Loading branch information
Ashley Hsiao authored and Ashley Hsiao committed Jun 2, 2016
1 parent dab6edb commit 164f0bc
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 23 deletions.
56 changes: 37 additions & 19 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def signup():
db.session.commit()

session["current_user"] = {
"email": new_user.email,
"first_name": new_user.first_name,
"user_id": new_user.user_id
}

Expand Down Expand Up @@ -260,6 +260,24 @@ def show_friends_and_requests():
friends=friends)


@app.route("/friends/search", methods=["GET"])
def search_users():
"""Search for a user by email and return results."""

# Get value from searchbox form for user's query
user_search = request.args.get("q")

# # Search user's query in restaurant table of db and return all search results
# query = db.session.query(Restaurant)
# query = search(query, user_search)
# search_results = query.all()

# Refactored above code to one line
search_results = search(db.session.query(User), user_search).all()

return render_template("friends_search_results.html", search_results=search_results)


@app.route("/restaurants")
def restaurant_list():
"""Show list of restaurants."""
Expand All @@ -271,6 +289,24 @@ def restaurant_list():
restaurants=restaurants)


@app.route("/restaurants/search", methods=["GET"])
def search_restaurants():
"""Search for a restaurant by name or address and return results."""

# Get value from searchbox form for user's query
user_search = request.args.get("q")

# # Search user's query in restaurant table of db and return all search results
# query = db.session.query(Restaurant)
# query = search(query, user_search)
# search_results = query.all()

# Refactored above code to one line
search_results = search(db.session.query(Restaurant), user_search).all()

return render_template("restaurants_search_results.html", search_results=search_results)


@app.route("/restaurants/<int:restaurant_id>")
def restaurant_profile(restaurant_id):
"""Show restaurant information."""
Expand Down Expand Up @@ -321,24 +357,6 @@ def add_visit():
return redirect("/restaurants/%s" % restaurant_id)


@app.route("/search", methods=["GET"])
def search_restaurants():
"""Search for a restaurant and return results."""

# Get value from searchbox form for user's query
user_search = request.args.get("q")

# # Search user's query in restaurant table of db and return all search results
# query = db.session.query(Restaurant)
# query = search(query, user_search)
# search_results = query.all()

# Refactored above code to one line
search_results = search(db.session.query(Restaurant), user_search).all()

return render_template("search_results.html", search_results=search_results)


if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the point
# that we invoke the DebugToolbarExtension
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</ul>

<!-- Searchbox feature for restaurants -->
<form class="navbar-form navbar-left" role="search" action="/search">
<form class="navbar-form navbar-left" role="search" action="/restaurants/search">
<div class="input-group">
<input class="form-control" id="searchbox" type="search" name="q" aria-label="Find Restaurant" placeholder="Find restaurant by name or address">
<span class="input-group-btn">
Expand Down
17 changes: 14 additions & 3 deletions templates/friends.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</ul>
</li>
<li role="presentation">
<a href="#find-friends" id="find-friends-tab" role="tab" data-toggle="tab" aria-controls="find-friends" aria-expanded="true">Find Friends</a>
<a href="#find-friends" id="find-friends-tab" role="tab" data-toggle="tab" aria-controls="find-friends" aria-expanded="false">Find Friends</a>
</li>
</ul>

Expand All @@ -61,7 +61,7 @@ <h1>My Friends</h1>
</div><!-- /.col -->
{% endfor %}
{% else %}
<p>You haven't added any friends. Please look them up and connect with them!</p>
<p>You haven't added any friends. Find friends and connect with them!</p>
{% endif %}
</div><!-- /.row -->
</div><!-- /#friends -->
Expand Down Expand Up @@ -116,7 +116,18 @@ <h1>Sent Friend Requests</h1>
<h1>Find Friends</h1>
<div class="row">
<div class="col-xs-12">
Feature coming soon!
<p>
Enter the email address of your friend and see if they are a user of Breadcrumbs! Connect with them to see what restaurants they visited and what they ate.
</p>
<!-- Search engine for users -->
<form class="navbar-form navbar-left" role="search" action="/friends/search">
<div class="input-group">
<input class="form-control" id="searchbox" type="search" name="q" aria-label="Find Friend" placeholder="Find friend by email address">
<span class="input-group-btn">
<button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>
</span>
</div>
</form>
</div>
</div><!-- /.row -->
</div><!-- /#find-friends -->
Expand Down
47 changes: 47 additions & 0 deletions templates/restaurants_search_results.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% extends 'base.html' %}

{% block title %}Restaurants: Search Results{% endblock %}

{% block head %}{% endblock %}

{% block content %}

<div class="container">
<h1>Restaurants: Search Results</h1>
{% if search_results %}
<div class="row">
{% for restaurant in search_results %}
<div class="col-xs-12 col-md-6">
<div class="list-group" id="restaurant-list">
<a href="/restaurants/{{ restaurant.restaurant_id }}" class="list-group-item">
<div class="media">
<div class="media-left">
{% if restaurant.image_url %}
<img class="media-object" src="{{ restaurant.image_url }}" alt="Image for {{ restaurant.name }}">
{% else %}
<img class="media-object" src="http://placehold.it/100x100?text=No+Image+Available" alt="Image for {{ restaurant.name }}">
{% endif %}
</div><!-- /.media-left -->
<div class="media-body">
<h3 class="media-heading">{{ restaurant.name }}</h3>
<p>
<span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span> {{ restaurant.address }}<br>
<span class="glyphicon glyphicon-earphone" aria-hidden="true"></span> {{ restaurant.phone }}
</p>
</div><!-- /.media-body -->
</div><!-- /.media -->
</a>
</div><!-- /.list-group -->
</div><!-- /.col -->
{% endfor %}
</div><!-- /.row -->
{% else %}
<div class="row">
<div class="col-xs-12 col-md-6">
<p>No results found. Please try again.</p>
</div><!-- /.col -->
</div><!-- /.row -->
{% endif %}
</div><!-- /.container -->

{% endblock %}

0 comments on commit 164f0bc

Please sign in to comment.