diff --git a/htmx_contact/main.py b/htmx_contact/main.py index 172c55a..1fc6253 100644 --- a/htmx_contact/main.py +++ b/htmx_contact/main.py @@ -1,7 +1,15 @@ from flask import Blueprint from flask import redirect from flask import render_template +from flask import request +from flask_login import current_user from flask_login import login_required +from sqlalchemy import and_ +from sqlalchemy import or_ +from sqlalchemy import select + +from htmx_contact import Session +from htmx_contact.models import Contact bp = Blueprint("main", __name__, url_prefix="/") @@ -14,4 +22,26 @@ def index(): @bp.route("/contacts", methods=["GET"]) @login_required def contacts(): - return render_template("contacts.html", message="Hello HTMX") + search = request.args.get("q") + if search is not None: + stmt = ( + select(Contact) + .where( + and_( + Contact.user_id == current_user.id, + or_( + Contact.first_name.like(f"%{search}%"), + Contact.last_name.like(f"%{search}%"), + Contact.email.like(f"%{search}%"), + ), + ) + ) + .order_by(Contact.last_name, Contact.first_name) + ) + else: + stmt = select(Contact).where(Contact.user_id == current_user.id).order_by(Contact.last_name, Contact.first_name) + + with Session() as session: + results = session.scalars(stmt).all() + + return render_template("contacts.html", contacts=results) diff --git a/htmx_contact/templates/base.html b/htmx_contact/templates/base.html index 30392d9..abfe43a 100644 --- a/htmx_contact/templates/base.html +++ b/htmx_contact/templates/base.html @@ -3,7 +3,7 @@
-First | +Last | +Phone | +|
---|---|---|---|
{{ contact.first_name }} | +{{ contact.last_name }} | +{{ contact.phone }} | +{{ contact.email }} | +