-
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
5 changed files
with
67 additions
and
29 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,31 +1,48 @@ | ||
from ..models import locations_collection | ||
|
||
""" | ||
name: str | ||
""" | ||
|
||
def remove_dining_halls_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 get_location(name: str) -> dict | None: | ||
return locations_collection.find_one({"name": name}) | ||
|
||
def add_dining_halls_to_db(dining_halls: list[dict]) -> None: | ||
for dh in dining_halls: | ||
locations_collection.insert_one(dh) | ||
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 get_names_of_dining_halls(dining_halls: list[dict]) -> list[str]: | ||
names = [] | ||
for dh in dining_halls: | ||
names.append(dh["name"]) | ||
return names | ||
def delete_location(name: str) -> None: | ||
locations_collection.delete_one({"name": name}) | ||
|
||
## Bulk CRUD | ||
|
||
def remove_add_dining_halls_to_db(dining_halls: list[dict]) -> None: | ||
# get names of dining halls | ||
names = get_names_of_dining_halls(dining_halls) | ||
# remove dining halls with the names | ||
remove_dining_halls_from_db(names) | ||
# add dining halls to db | ||
add_dining_halls_to_db(dining_halls) | ||
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 | ||
|
||
def get_all_dining_halls_from_db() -> list[dict]: | ||
return list(locations_collection.find({})) | ||
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
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
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
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