Implement date uniqueness validation in DataManager and update MedTrackerApp for duplicate checks

This commit is contained in:
William Valentin
2025-07-28 17:28:00 -07:00
parent f0dd47d433
commit 9aa1188c98
3 changed files with 144 additions and 8 deletions
+21 -4
View File
@@ -66,6 +66,14 @@ class DataManager:
def add_entry(self, entry_data: list[str | int]) -> bool:
"""Add a new entry to the CSV file."""
try:
# Check if date already exists
df: pd.DataFrame = self.load_data()
date_to_add: str = str(entry_data[0])
if not df.empty and date_to_add in df["date"].values:
self.logger.warning(f"Entry with date {date_to_add} already exists.")
return False
with open(self.filename, mode="a", newline="") as file:
writer = csv.writer(file)
writer.writerow(entry_data)
@@ -74,13 +82,22 @@ class DataManager:
self.logger.error(f"Error adding entry: {str(e)}")
return False
def update_entry(self, date: str, values: list[str | int]) -> bool:
"""Update an existing entry identified by date."""
def update_entry(self, original_date: str, values: list[str | int]) -> bool:
"""Update an existing entry identified by original_date."""
try:
df: pd.DataFrame = self.load_data()
# Find the row to update using date as a unique identifier
new_date: str = str(values[0])
# If the date is being changed, check if the new date already exists
if original_date != new_date and new_date in df["date"].values:
self.logger.warning(
f"Cannot update: entry with date {new_date} already exists."
)
return False
# Find the row to update using original_date as a unique identifier
df.loc[
df["date"] == date,
df["date"] == original_date,
[
"date",
"depression",