Ali2206 commited on
Commit
bce5673
·
1 Parent(s): 505bbe6

Fix CRUD endpoints with proper MongoDB queries and validation

Browse files
Files changed (1) hide show
  1. main.py +30 -3
main.py CHANGED
@@ -970,11 +970,20 @@ async def update_checklist_item(user_id: str, item_id: str, item_data: dict):
970
  try:
971
  logger.info(f"Updating item {item_id} for user: {user_id}")
972
 
 
 
 
 
 
 
 
 
 
973
  # Prepare update fields
974
  update_fields = {}
975
  for field in ['requirement', 'compliance', 'deviation', 'action']:
976
  if field in item_data:
977
- update_fields[f"sections.$[section].items.$[item].{field}"] = item_data[field]
978
 
979
  if not update_fields:
980
  raise HTTPException(
@@ -984,9 +993,9 @@ async def update_checklist_item(user_id: str, item_id: str, item_data: dict):
984
 
985
  # Update the item
986
  result = await db.checklists.update_one(
987
- {"userId": user_id, "sections.items.id": item_id},
988
  {"$set": update_fields},
989
- array_filters=[{"section.items.id": item_id}]
990
  )
991
 
992
  if result.modified_count > 0:
@@ -1029,6 +1038,15 @@ async def delete_checklist_item(user_id: str, item_id: str):
1029
  try:
1030
  logger.info(f"Deleting item {item_id} for user: {user_id}")
1031
 
 
 
 
 
 
 
 
 
 
1032
  # Remove the item from the checklist
1033
  result = await db.checklists.update_one(
1034
  {"userId": user_id},
@@ -1137,6 +1155,15 @@ async def delete_checklist_section(user_id: str, section_id: str):
1137
  try:
1138
  logger.info(f"Deleting section {section_id} for user: {user_id}")
1139
 
 
 
 
 
 
 
 
 
 
1140
  # Remove the section from the checklist
1141
  result = await db.checklists.update_one(
1142
  {"userId": user_id},
 
970
  try:
971
  logger.info(f"Updating item {item_id} for user: {user_id}")
972
 
973
+ # Check if checklist exists
974
+ checklist = await db.checklists.find_one({"userId": user_id})
975
+ if not checklist:
976
+ logger.warning(f"No checklist found for user {user_id}")
977
+ raise HTTPException(
978
+ status_code=status.HTTP_404_NOT_FOUND,
979
+ detail=f"Checklist not found for user {user_id}. Please create a checklist first."
980
+ )
981
+
982
  # Prepare update fields
983
  update_fields = {}
984
  for field in ['requirement', 'compliance', 'deviation', 'action']:
985
  if field in item_data:
986
+ update_fields[f"sections.$[].items.$[item].{field}"] = item_data[field]
987
 
988
  if not update_fields:
989
  raise HTTPException(
 
993
 
994
  # Update the item
995
  result = await db.checklists.update_one(
996
+ {"userId": user_id},
997
  {"$set": update_fields},
998
+ array_filters=[{"item.id": item_id}]
999
  )
1000
 
1001
  if result.modified_count > 0:
 
1038
  try:
1039
  logger.info(f"Deleting item {item_id} for user: {user_id}")
1040
 
1041
+ # Check if checklist exists
1042
+ checklist = await db.checklists.find_one({"userId": user_id})
1043
+ if not checklist:
1044
+ logger.warning(f"No checklist found for user {user_id}")
1045
+ raise HTTPException(
1046
+ status_code=status.HTTP_404_NOT_FOUND,
1047
+ detail=f"Checklist not found for user {user_id}. Please create a checklist first."
1048
+ )
1049
+
1050
  # Remove the item from the checklist
1051
  result = await db.checklists.update_one(
1052
  {"userId": user_id},
 
1155
  try:
1156
  logger.info(f"Deleting section {section_id} for user: {user_id}")
1157
 
1158
+ # Check if checklist exists
1159
+ checklist = await db.checklists.find_one({"userId": user_id})
1160
+ if not checklist:
1161
+ logger.warning(f"No checklist found for user {user_id}")
1162
+ raise HTTPException(
1163
+ status_code=status.HTTP_404_NOT_FOUND,
1164
+ detail=f"Checklist not found for user {user_id}. Please create a checklist first."
1165
+ )
1166
+
1167
  # Remove the section from the checklist
1168
  result = await db.checklists.update_one(
1169
  {"userId": user_id},