fenya123 / forum123

Flask application written for educational purposes.
http://193.124.115.171/
1 stars 1 forks source link

Encapsulate models querying #66

Closed birthdaysgift closed 1 year ago

birthdaysgift commented 1 year ago

In order to separate application layers we need to move all "low-level" database related code into models.

In the scope of this task we need to encapsulate all models quirying functionality into models methods. We need to get rid of using session.query() in our routes, because this is a database layer responsibility.

Below are changes we expect to be in routes.py. "Before" designates what code we have now, "After" designates what code we want to see after this refactoring.


Before:

@bp.route("/")
@bp.route("/index")
def index() -> str:
    ...
    users = session.query(User).all()
    ...
    user_session = session.query(UserSession).filter_by(session_id=session_id).first()
    ...
    current_user = session.query(User).filter_by(id=user_session.user_id).one()

After:

@bp.route("/")
@bp.route("/index")
def index() -> str:
    ...
    users = User.get_users()
    ...
    user_session = UserSession.get_user_session_by_session_id(session_id)
    ...
    current_user = user_session.user

Before:

@bp.route("/login", methods=["POST", "GET"])
def login() -> str | Response:
    ...
    user = session.query(User).filter_by(username=form.username.data).first()
    ...

After:

@bp.route("/login", methods=["POST", "GET"])
def login() -> str | Response:
    ...
    user = User.get_user_by_credentials(form.username.data, form.password.data)
    ...

Before:

@bp.route("/logout")
def logout() -> Response:
    ...
    if session_to_delete := session.query(UserSession).filter_by(session_id=session_id).first():
    ...

After:

@bp.route("/logout")
def logout() -> Response:
    ...
    if session_to_delete := UserSession.get_user_session_by_session_id(session_id):
    ...

Before:

@bp.route("/topics")
def topics() -> str | Response:
    ...
    user_session = session.query(UserSession).filter_by(session_id=session_id).first()
    ...
    current_user = session.query(User).filter_by(id=user_session.user_id).one()
    topic_list = session.query(Topic).order_by(Topic.created_at.desc()).all()
    ...

After:

@bp.route("/topics")
def topics() -> str | Response:
    ...
    user_session = UserSession.get_user_session_by_session_id(session_id)
    ...
    current_user = user_session.user
    topic_list = Topic.get_topics()
    ...

Before:

@bp.route("/topics/create", methods=["POST", "GET"])
def create_topic() -> str | Response:
    ...
    user_session = session.query(UserSession).filter_by(session_id=session_id).first()
    ...
    current_user = session.query(User).filter_by(id=user_session.user_id).one()
    ...

After:

@bp.route("/topics/create", methods=["POST", "GET"])
def create_topic() -> str | Response:
    ...
    user_session = UserSession.get_user_session_by_session_id(session_id)
    ...
    current_user = user_session.user
    ...

Before:

@bp.route("/topics/<int:topic_id>")
def topic_page(topic_id: int) -> str | Response:
    ...
    user_session = session.query(UserSession).filter_by(session_id=session_id).first()
    ...
    current_user = session.query(User).filter_by(id=user_session.user_id).one()
        if not (topic := session.query(Topic).filter_by(id=topic_id).first()):
    ...

After:

@bp.route("/topics/<int:topic_id>")
def topic_page(topic_id: int) -> str | Response:
    ...
    user_session = UserSession.get_user_session_by_session_id(session_id)
    ...
    current_user = user_session.user
        if not (topic := Topic.get(topic_id)):
    ...

Before:

@bp.route("/topics/<int:topic_id>/posts/create", methods=["POST", "GET"])
def create_post(topic_id: int) -> str | Response:  # noqa: CFQ004
    ...
    user_session = session.query(UserSession).filter_by(session_id=session_id).first()
    ...
    current_user = session.query(User).filter_by(id=user_session.user_id).one()
    ...
    if not (topic := session.query(Topic).filter_by(id=topic_id).first()):
    ...

After:

@bp.route("/topics/<int:topic_id>/posts/create", methods=["POST", "GET"])
def create_post(topic_id: int) -> str | Response:  # noqa: CFQ004
    ...
    user_session = UserSession.get_user_session_by_session_id(session_id)
    ...
    current_user = user_session.user
    ...
    if not (topic := Topic.get(topic_id)):
    ...
birthdaysgift commented 1 year ago

I've pushed expected changes for routes.py to example-66-encapsulate-models-querying, but please use a separate branch to work on this task.