Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AkhilS2 committed May 15, 2024
1 parent 997ab58 commit 81bfd93
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 185 deletions.
106 changes: 28 additions & 78 deletions backend/myapi/db_functions/food.py
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})
68 changes: 48 additions & 20 deletions backend/myapi/db_functions/locations.py
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}})
74 changes: 55 additions & 19 deletions backend/myapi/db_functions/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from requests import get
from ..models import tasks_collection
from datetime import datetime

Expand All @@ -9,39 +10,64 @@
last_update: str
"""

## Basic CRUD

def set_task_last_update(task_name: str) -> None:

def set_task(task_name: str) -> dict[str, str]:
"""
Set the time that the task were last update the time should be based upon the database time
Create a new task with the given name
"""
# get the time from django
time_now: datetime = timezone.now()
# get the time from django and make it naive
time_now: datetime = timezone.now().replace(tzinfo=None)

# convert the time to a string
time_str = time_now.strftime("%Y-%m-%d %H:%M:%S")

# get the task from the tasks collection
task: dict | None = tasks_collection.find_one({"name": task_name})
# create a new task
task = {"name": task_name, "last_update": time_str}

# check if the task exists
if task is None:
# create a new task
task = {"name": task_name, "last_update": time_str}
# insert the task into the tasks collection
tasks_collection.insert_one(task)
else:
# update the task
tasks_collection.update_one(
{"name": task_name}, {"$set": {"last_update": time_str}}
)
# insert the task into the tasks collection
tasks_collection.insert_one(task)

return task


def get_task(task_name: str) -> dict | None:
"""
Get the task from the database
"""
return tasks_collection.find_one({"name": task_name})


def update_task(task_name: str, last_update: datetime) -> None:
"""
Update the task in the database
"""
# convert the time to a string
time_str = last_update.strftime("%Y-%m-%d %H:%M:%S")

# update the task
tasks_collection.update_one(
{"name": task_name}, {"$set": {"last_update": time_str}}
)


def get_task_last_update(task_name: str) -> datetime | None:
def delete_task(task_name: str) -> None:
"""
Delete the task from the database
"""
tasks_collection.delete_one({"name": task_name})


## Additional CRUD


def get_last_update_time(task_name: str) -> datetime | None:
"""
Get the last update time of the task from the database
"""
# find the task in the tasks collection
task: dict | None = tasks_collection.find_one({"name": task_name})
task = get_task(task_name)

# check if the location exists
if task is None:
Expand All @@ -54,3 +80,13 @@ def get_task_last_update(task_name: str) -> datetime | None:
last_update_time = datetime.strptime(last_update, "%Y-%m-%d %H:%M:%S")

return last_update_time


## helper functions


def str_to_datetime(time_str: str) -> datetime:
"""
Convert a string to a datetime object
"""
return datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
Loading

0 comments on commit 81bfd93

Please sign in to comment.