Skip to content

Commit

Permalink
fix(database): fix a db problem
Browse files Browse the repository at this point in the history
  • Loading branch information
lmangall committed May 21, 2024
1 parent c082547 commit feeb9e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
5 changes: 2 additions & 3 deletions var/www.saladbook.xyz/database/database.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"Stefano": "Carrot",
"Leo": "nicoise",
"Daniil": "with_beets",

}
"Daniil": "with_beets"
}
30 changes: 24 additions & 6 deletions var/www.saladbook.xyz/database/salad_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,39 @@ def get_script_directory():

def load_database(filename):
"""Load the JSON database from a file."""
print(f"DEBUG: Attempting to load database from {filename}<br>")
if os.path.exists(filename):
try:
with open(filename, "r") as file:
return json.load(file)
except json.JSONDecodeError:
print("Error: Database file contains invalid JSON. Initializing an empty database.")
return {}
print("Error: Database file contains invalid JSON. Unable to load data.")
return None
except Exception as e:
print(f"Error: Failed to read the database file. {e}")
return {}
return None
else:
print(f"DEBUG: Database file {filename} does not exist.<br>")
return {}

def save_database(data, filename):
"""Save the JSON database to a file."""
try:
with open(filename, "w") as file:
json.dump(data, file, indent=4)
print(f"DEBUG: Database saved to {filename}<br>")
except Exception as e:
print(f"Error: Failed to save the database file. {e}")

def initialize_database(filename):
"""Initialize the database file if it does not exist."""
if not os.path.exists(filename):
data = {}
save_database(data, filename)
return data
else:
return load_database(filename)

def main():
print("Content-type: text/html\n")
print()
Expand All @@ -45,9 +57,12 @@ def main():
# Get the directory where the script is located
script_directory = get_script_directory()
filename = os.path.join(script_directory, "database.json")
print(f"DEBUG: Database filename is {filename}<br>")

# Load existing data
data = load_database(filename)
# Initialize and load existing data
data = initialize_database(filename)
if data is None:
data = {}
print(f"DEBUG: Loaded data: {data}<br>")

# Determine the request method
Expand All @@ -58,14 +73,17 @@ def main():
# Read the input data from stdin
content_length = int(os.getenv('CONTENT_LENGTH', 0))
post_data = sys.stdin.read(content_length)
print(f"DEBUG: Received POST data: {post_data}<br>")
form_data = parse_qs(post_data)
else: # DELETE method
form_data = cgi.FieldStorage()
form_data = sys.stdin.read()

# Debugging: Print the form keys and values
for key in form_data.keys():
print(f"DEBUG: form[{key}]={form_data[key]}<br>")

print(f"DEBUG: form_data={form_data}<br>")

name = form_data.get('name', [None])[0]
salad = form_data.get('salad', [None])[0] if method == 'POST' else None

Expand Down

0 comments on commit feeb9e0

Please sign in to comment.