Archived
66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
LOCATIONS = [
|
|
{
|
|
"id": 1,
|
|
"name": "Nashville North"
|
|
},
|
|
{
|
|
"id": 2,
|
|
"name": "Nashville South"
|
|
}
|
|
,
|
|
]
|
|
|
|
def get_all_locations():
|
|
return LOCATIONS
|
|
|
|
def get_single_location():
|
|
requested_location = None
|
|
|
|
|
|
for location in LOCATIONS:
|
|
if location["id"] == id:
|
|
requested_location = location
|
|
|
|
return requested_location
|
|
|
|
def create_location(location):
|
|
# Get the id value of the last location in the list
|
|
max_id = LOCATIONS[-1]["id"]
|
|
|
|
# Add 1 to whatever that number is
|
|
new_id = max_id + 1
|
|
|
|
# Add an `id` property to the location dictionary
|
|
location["id"] = new_id
|
|
|
|
# Add the location dictionary to the list
|
|
LOCATIONS.append(location)
|
|
|
|
# Return the dictionary with `id` property added
|
|
return location
|
|
|
|
def delete_location(id):
|
|
# Initial -1 value for location index, in case one isn't found
|
|
location_index = -1
|
|
|
|
# Iterate the LOCATIONS list, but use enumerate() so that you
|
|
# can access the index value of each item
|
|
for index, location in enumerate(LOCATIONS):
|
|
if location["id"] == id:
|
|
# Found the location. Store the current index.
|
|
location_index = index
|
|
|
|
# If the location was found, use pop(int) to remove it from list
|
|
if location_index >= 0:
|
|
LOCATIONS.pop(location_index)
|
|
|
|
def update_location(id, new_location):
|
|
# Iterate the LOCATIONS list, but use enumerate() so that
|
|
# you can access the index value of each item.
|
|
for index, location in enumerate(LOCATIONS):
|
|
if location["id"] == id:
|
|
# Found the location. Update the value.
|
|
LOCATIONS[index] = new_location
|
|
break
|
|
|