Skip to content

Commit

Permalink
Split code to be more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhPhan8803 committed Oct 28, 2022
1 parent 7e39e10 commit 41d95b5
Showing 1 changed file with 60 additions and 31 deletions.
91 changes: 60 additions & 31 deletions src/backend/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
""" This is a module docstring """
import json
import dataclasses
from collections import namedtuple
from werkzeug.datastructures import MultiDict
from flask import Flask, request
from login import Login
from mainpage import MainPage

# from logging import FileHandler, WARNING

app = Flask(__name__)
Expand Down Expand Up @@ -33,51 +36,77 @@ def register():
return result.message, 400
return result.message, 200


@app.route("/", methods=["POST", "GET"])
@app.route("/main", methods=["POST", "GET"])
def mainpage():
"""Handle mainpage requests"""
mainpage = MainPage()
mainpage_obj = MainPage()
if request.method == "POST":
apt_id = request.json["apt_id"]
username = request.json["username"]
comment = request.json["comment"]
vote = request.json["vote"]
query_result = ""
if None not in (apt_id, username, comment, vote):
reviews = mainpage.write_apartment_review(apt_id, username, comment, vote)
reviews_dict = [dataclasses.asdict(review) for review in reviews]
query_result = json.dumps(reviews_dict)
return query_result, 201 if len(query_result) != 0 else query_result, 400

return mainpage_post(mainpage_obj)

args = request.args
is_search = args.get("search")
is_populate = args.get("populate")
is_review = args.get("review")
is_pictures = args.get("pictures")
num_apts = args.get("numApts", type=int)
apt_id = args.get("aptId", default=0, type=int)
search_query = args.get("searchQuery", type=str)
price_sort = args.get("priceSort", type=int)
rating_sort = args.get("ratingSort", type=int)
return mainpage_get(mainpage_obj, args)


def mainpage_get(mainpage_obj: MainPage, args: MultiDict):
"""Helper for mainpage get requests"""
action_type = namedtuple(
"action_type", ["is_search", "is_populate", "is_review", "is_pictures"]
)
action = action_type(
args.get("search"),
args.get("populate"),
args.get("review"),
args.get("pictures"),
)

params = namedtuple(
"params", ["num_apts", "apt_id", "search_query", "price_sort", "rating_sort"]
)
param = params(
args.get("numApts", type=int),
args.get("aptId", default=0, type=int),
args.get("searchQuery", type=str),
args.get("priceSort", type=int),
args.get("ratingSort", type=int),
)

query_result = ""
if is_search is not None and search_query is not None:
query_result = json.dumps(mainpage.search_apartments(search_query))
elif is_populate is not None and num_apts is not None:
if action.is_search is not None and param.search_query is not None:
query_result = json.dumps(mainpage_obj.search_apartments(param.search_query))
elif action.is_populate is not None and param.num_apts is not None:
apts = []
if price_sort is not None and rating_sort is not None:
apts = mainpage.apartments_sorted(num_apts, price_sort, rating_sort)
if param.price_sort is not None and param.rating_sort is not None:
apts = mainpage_obj.apartments_sorted(
param.num_apts, param.price_sort, param.rating_sort
)
else:
apts = mainpage.apartments_default(num_apts)
apts = mainpage_obj.apartments_default(param.num_apts)
apts_dict = [dataclasses.asdict(apt) for apt in apts]
query_result = json.dumps(apts_dict)
elif is_review is not None and apt_id is not None:
reviews = mainpage.get_apartments_reviews(apt_id)
elif action.is_review is not None and param.apt_id is not None:
reviews = mainpage_obj.get_apartments_reviews(param.apt_id)
reviews_dict = [dataclasses.asdict(review) for review in reviews]
query_result = json.dumps(reviews_dict)
elif is_pictures is not None and apt_id is not None:
query_result = json.dumps(mainpage.get_apartments_pictures(apt_id))
elif action.is_pictures is not None and param.apt_id is not None:
query_result = json.dumps(mainpage_obj.get_apartments_pictures(param.apt_id))
return query_result, 200 if len(query_result) != 0 else query_result, 400


def mainpage_post(mainpage_obj: MainPage):
"""Helper for mainpage post requests"""
apt_id = request.json["apt_id"]
username = request.json["username"]
comment = request.json["comment"]
vote = request.json["vote"]
query_result = ""
if None not in (apt_id, username, comment, vote):
reviews = mainpage_obj.write_apartment_review(apt_id, username, comment, vote)
reviews_dict = [dataclasses.asdict(review) for review in reviews]
query_result = json.dumps(reviews_dict)
return query_result, 201 if len(query_result) != 0 else query_result, 400


if __name__ == "__main__":
app.run(debug=True) # pragma: no cover

3 comments on commit 41d95b5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
app.py693648%44–49, 54–94, 99–108
TOTAL2173683% 

Tests Skipped Failures Errors Time
35 0 💤 0 ❌ 0 🔥 0.553s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
app.py693648%44–49, 54–94, 99–108
TOTAL2173683% 

Tests Skipped Failures Errors Time
35 0 💤 0 ❌ 0 🔥 0.634s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
app.py693648%44–49, 54–94, 99–108
TOTAL2173683% 

Tests Skipped Failures Errors Time
35 0 💤 0 ❌ 0 🔥 0.812s ⏱️

Please sign in to comment.