@@ -419,3 +419,97 @@ def db_toggle_image_favourite_status(image_id: str) -> bool:
419419 return False
420420 finally :
421421 conn .close ()
422+
423+
424+
425+ def db_delete_image (image_id : ImageId ) -> bool :
426+ """
427+ Delete a single image from the database by its ID.
428+ This will also delete associated records in image_classes due to CASCADE.
429+
430+ Args:
431+ image_id: ID of the image to delete
432+
433+ Returns:
434+ True if deletion was successful, False otherwise
435+ """
436+ conn = _connect ()
437+ cursor = conn .cursor ()
438+
439+ try :
440+ cursor .execute ("DELETE FROM images WHERE id = ?" , (image_id ,))
441+ conn .commit ()
442+ return cursor .rowcount > 0
443+ except Exception as e :
444+ logger .error (f"Error deleting image { image_id } : { e } " )
445+ conn .rollback ()
446+ return False
447+ finally :
448+ conn .close ()
449+
450+
451+ def db_add_tags_to_image (image_id : ImageId , tags : List [str ]) -> bool :
452+ """
453+ Add multiple tags to an image.
454+
455+ Args:
456+ image_id: ID of the image to tag
457+ tags: List of tag names to add
458+
459+ Returns:
460+ True if tagging was successful, False otherwise
461+ """
462+ if not tags :
463+ return True
464+
465+ conn = _connect ()
466+ cursor = conn .cursor ()
467+
468+ try :
469+ # First, verify the image exists
470+ cursor .execute ("SELECT id FROM images WHERE id = ?" , (image_id ,))
471+ if not cursor .fetchone ():
472+ logger .warning (f"Image { image_id } not found" )
473+ return False
474+
475+ # Get or create class IDs for each tag
476+ class_ids = []
477+ for tag in tags :
478+ # Check if tag exists in mappings
479+ cursor .execute ("SELECT class_id FROM mappings WHERE name = ?" , (tag ,))
480+ result = cursor .fetchone ()
481+
482+ if result :
483+ class_ids .append (result [0 ])
484+ else :
485+ # Create new mapping for this tag
486+ cursor .execute (
487+ "INSERT INTO mappings (name) VALUES (?)" ,
488+ (tag ,)
489+ )
490+ class_ids .append (cursor .lastrowid )
491+
492+ # Insert image-class pairs
493+ for class_id in class_ids :
494+ cursor .execute (
495+ """
496+ INSERT OR IGNORE INTO image_classes (image_id, class_id)
497+ VALUES (?, ?)
498+ """ ,
499+ (image_id , class_id ),
500+ )
501+
502+ # Mark image as tagged
503+ cursor .execute (
504+ "UPDATE images SET isTagged = 1 WHERE id = ?" ,
505+ (image_id ,)
506+ )
507+
508+ conn .commit ()
509+ return True
510+ except Exception as e :
511+ logger .error (f"Error adding tags to image { image_id } : { e } " )
512+ conn .rollback ()
513+ return False
514+ finally :
515+ conn .close ()
0 commit comments