Skip to content

Commit

Permalink
redo
Browse files Browse the repository at this point in the history
  • Loading branch information
AkhilS2 committed May 15, 2024
1 parent 09cd99a commit 997ab58
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 230 deletions.
106 changes: 78 additions & 28 deletions backend/myapi/db_functions/food.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,93 @@
from ..models import foods_collection


"""
food_name: str,
ratings: {
username: int
}
"""
def add_food(name: str) -> None:
foods_collection.insert_one({"name": name, "ratings": []})

## Basic CRUD

def remove_food(food_name: str) -> None:
foods_collection.delete_one({"name": food_name})

def set_food(name: str) -> None:
# check if food already exists
food = foods_collection.find_one({"food_name": name})
if food:

# 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:
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})
)

def get_food(name: str) -> dict | None:
return foods_collection.find_one({"food_name": name})
# if the food does not exist, return an empty list
if food is None:
return []

return food["ratings"]

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:

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:
return

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"]}}
)
# remove all ratings
food["ratings"] = []

# update the food
foods_collection.update_one({"name": food_name}, {"$set": {"ratings": []}})


def delete_food(name: str) -> None:
foods_collection.delete_one({"food_name": name})
# NOTE: there can be more functions to search for the type of food
68 changes: 20 additions & 48 deletions backend/myapi/db_functions/locations.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,31 @@
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

# overwrite the location
locations_collection.update_one({"name": name}, {"$set": 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)


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}})
def get_all_locations_from_db() -> list[dict]:
return list(locations_collection.find({}))
74 changes: 19 additions & 55 deletions backend/myapi/db_functions/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from requests import get
from ..models import tasks_collection
from datetime import datetime

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

## Basic CRUD


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

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

# create a new task
task = {"name": task_name, "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}}
)
# get the task from the tasks collection
task: dict | None = tasks_collection.find_one({"name": task_name})


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


## Additional CRUD
# 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}}
)


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

# check if the location exists
if task is None:
Expand All @@ -80,13 +54,3 @@ def get_last_update_time(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")
42 changes: 0 additions & 42 deletions backend/myapi/db_functions/user.py

This file was deleted.

Loading

0 comments on commit 997ab58

Please sign in to comment.