-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
206 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,43 @@ | ||
from ..models import foods_collection | ||
|
||
|
||
def add_food(name: str) -> None: | ||
foods_collection.insert_one({"name": name, "ratings": []}) | ||
""" | ||
food_name: str, | ||
ratings: { | ||
username: int | ||
} | ||
""" | ||
|
||
## Basic CRUD | ||
|
||
def remove_food(food_name: str) -> None: | ||
foods_collection.delete_one({"name": food_name}) | ||
|
||
|
||
# NOTE: you might want to add more to seach for the type of food | ||
def overwrite_food_rating(food_name: str, user_name: str, rating: int) -> None: | ||
# make sure rating is between 0 and 5 | ||
if rating < 0: | ||
rating = 0 | ||
elif rating > 5: | ||
rating = 5 | ||
|
||
# get the food | ||
food: dict[str, str | int | list[dict[str, str | int]]] | None = ( | ||
foods_collection.find_one({"name": food_name}) | ||
) | ||
|
||
# if the food does not exist, add it | ||
if food is None: | ||
add_food(food_name) | ||
food: dict[str, str | int | list[dict[str, str | int]]] | None = ( | ||
foods_collection.find_one({"name": food_name}) | ||
) | ||
|
||
# check if the food is none | ||
if food is None: | ||
def set_food(name: str) -> None: | ||
# check if food already exists | ||
food = foods_collection.find_one({"food_name": name}) | ||
if food: | ||
return | ||
# add food | ||
foods_collection.insert_one({"food_name": name, "ratings": {}}) | ||
|
||
# check if the user has already rated the food | ||
ratings: list[dict[str, str | int]] = food["ratings"] | ||
for rate in ratings: | ||
if rate["user"] == user_name: | ||
rate["rating"] = rating | ||
return | ||
|
||
# add new rating to the food for the user | ||
ratings.append({"user": user_name, "rating": rating}) | ||
|
||
|
||
# NOTE: you might want to add more to seach for the type of food | ||
def get_food_ratings(food_name: str) -> list[dict[str, str | int]]: | ||
# get the food | ||
food: dict[str, str | int | list[dict[str, str | int]]] | None = ( | ||
foods_collection.find_one({"name": food_name}) | ||
) | ||
|
||
# if the food does not exist, return an empty list | ||
if food is None: | ||
return [] | ||
def get_food(name: str) -> dict | None: | ||
return foods_collection.find_one({"food_name": name}) | ||
|
||
return food["ratings"] | ||
|
||
|
||
def get_average_rating(food_name: str) -> float: | ||
# get the food | ||
ratings = get_food_ratings(food_name) | ||
if len(ratings) == 0: | ||
return 0 | ||
|
||
# calculate the average rating | ||
total = 0 | ||
for rate in ratings: | ||
num: int = int(rate["rating"]) | ||
total += num | ||
|
||
return total / len(ratings) | ||
|
||
|
||
def remove_all_ratings(food_name: str) -> None: | ||
# get the food | ||
food: dict[str, str | int | list[dict[str, str | int]]] | None = ( | ||
foods_collection.find_one({"name": food_name}) | ||
) | ||
|
||
# if the food does not exist, return | ||
if food is None: | ||
def update_food(food_name: str, username: str, rating: int | None = None) -> None: | ||
# check if food exists | ||
food = foods_collection.find_one({"food_name": food_name}) | ||
if not food: | ||
return | ||
|
||
# remove all ratings | ||
food["ratings"] = [] | ||
|
||
# update the food | ||
foods_collection.update_one({"name": food_name}, {"$set": {"ratings": []}}) | ||
if rating is not None: | ||
# add/change rating | ||
food["ratings"][username] = rating | ||
# update food | ||
foods_collection.update_one( | ||
{"food_name": food_name}, {"$set": {"ratings": food["ratings"]}} | ||
) | ||
|
||
|
||
# NOTE: there can be more functions to search for the type of food | ||
def delete_food(name: str) -> None: | ||
foods_collection.delete_one({"food_name": name}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,59 @@ | ||
from ..models import locations_collection | ||
|
||
""" | ||
name: str | ||
""" | ||
|
||
def remove_locations_from_db(names: list[str]) -> None: | ||
for name in names: | ||
locations_collection.delete_many({"name": name}) | ||
|
||
## Basic CRUD | ||
def set_location(location: dict) -> None: | ||
locations_collection.insert_one(location) | ||
|
||
def add_locations_to_db(locations: list[dict]) -> None: | ||
for dh in locations: | ||
locations_collection.insert_one(dh) | ||
|
||
def get_location(name: str) -> dict | None: | ||
return locations_collection.find_one({"name": name}) | ||
|
||
def get_names_of_locations(locations: list[dict]) -> list[str]: | ||
names = [] | ||
for dh in locations: | ||
names.append(dh["name"]) | ||
return names | ||
|
||
def update_location(name: str, location: dict) -> None: | ||
""" | ||
check if the location exists then overwrite the location | ||
""" | ||
# check if the location exists | ||
if get_location(name) is None: | ||
set_location(location) # if not, create a new location | ||
|
||
def remove_add_locations_to_db(locations: list[dict]) -> None: | ||
# get names of locations | ||
names = get_names_of_locations(locations) | ||
# remove locations with the names | ||
remove_locations_from_db(names) | ||
# add locations to db | ||
add_locations_to_db(locations) | ||
# overwrite the location | ||
locations_collection.update_one({"name": name}, {"$set": location}) | ||
|
||
|
||
def get_all_locations_from_db() -> list[dict]: | ||
return list(locations_collection.find({})) | ||
def delete_location(name: str) -> None: | ||
locations_collection.delete_one({"name": name}) | ||
|
||
|
||
## Bulk CRUD | ||
|
||
|
||
def set_locations(locations: list[dict]) -> None: | ||
locations_collection.insert_many(locations) | ||
|
||
|
||
def get_locations(names: list[str] = []) -> list[dict]: | ||
""" | ||
if no names are given, return all locations | ||
else, return the locations with the given names | ||
""" | ||
if len(names) == 0: | ||
return list(locations_collection.find({})) # get all locations | ||
|
||
return list( | ||
locations_collection.find({"name": {"$in": names}}) | ||
) # get specific locations | ||
|
||
|
||
def update_locations(locations: list[dict]) -> None: | ||
for location in locations: | ||
update_location(location["name"], location) | ||
|
||
|
||
def delete_locations(names: list[str]) -> None: | ||
locations_collection.delete_many({"name": {"$in": names}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.