Skip to main content

Performing Bookmark Operations

To manage bookmarks, tags, and collections within the application, you interact with the BookmarkRepository. This repository provides an in-memory data store for these entities, offering a straightforward interface for common data operations.

Creating a Bookmark

To create a new bookmark, instantiate a Bookmark object and pass it to the create_bookmark method.

from app.db.models import Bookmark
from app.db.repository import BookmarkRepository

# Initialize the repository
bookmark_repo = BookmarkRepository()

# Create a new bookmark
new_bookmark = Bookmark(
url="https://example.com/new-article",
title="A New Article",
description="This is a description of the new article.",
status="unread",
tags=["python", "programming"]
)

created_bookmark = bookmark_repo.create_bookmark(new_bookmark)
print(f"Created bookmark: {created_bookmark.title} (ID: {created_bookmark.id})")

Retrieving a Bookmark

You can retrieve a specific bookmark by its unique identifier using the get_bookmark method.

# Assuming 'created_bookmark' from the previous example
retrieved_bookmark = bookmark_repo.get_bookmark(created_bookmark.id)

if retrieved_bookmark:
print(f"Retrieved bookmark: {retrieved_bookmark.title}")
else:
print(f"Bookmark with ID {created_bookmark.id} not found.")

Listing Bookmarks

The list_bookmarks method allows you to retrieve a collection of bookmarks. It supports pagination and filtering by status, tags, or collections.

# List all bookmarks (with default pagination)
all_bookmarks = bookmark_repo.list_bookmarks()
print(f"Total bookmarks: {len(all_bookmarks)}")
for bookmark in all_bookmarks:
print(f"- {bookmark.title} ({bookmark.status})")

# List 'read' bookmarks, paginated
read_bookmarks_page_1 = bookmark_repo.list_bookmarks(status="read", offset=0, limit=5)
print(f"
Read bookmarks (page 1):")
for bookmark in read_bookmarks_page_1:
print(f"- {bookmark.title}")

# List bookmarks associated with a specific tag (illustrative tag_id)
# In a real scenario, you'd get the tag_id from a Tag object
tag_id_example = "some_tag_id_here"
bookmarks_by_tag = bookmark_repo.list_bookmarks(tag_id=tag_id_example)
print(f"
Bookmarks with tag ID '{tag_id_example}':")
for bookmark in bookmarks_by_tag:
print(f"- {bookmark.title}")

Updating a Bookmark

To modify an existing bookmark, provide the bookmark's ID and an updated Bookmark object (or a partial update) to the update_bookmark method.

# Update the status and add a new tag to the created_bookmark
updated_data = Bookmark(
id=created_bookmark.id, # ID is crucial for identifying the bookmark
url=created_bookmark.url,
title="Updated Article Title",
description="This article has been updated and is now read.",
status="read",
tags=["python", "programming", "webdev"]
)

updated_bookmark = bookmark_repo.update_bookmark(created_bookmark.id, updated_data)

if updated_bookmark:
print(f"
Updated bookmark: {updated_bookmark.title}")
print(f"New status: {updated_bookmark.status}")
print(f"New tags: {updated_bookmark.tags}")
else:
print(f"Bookmark with ID {created_bookmark.id} not found for update.")

Deleting a Bookmark

The delete_bookmark method permanently removes a bookmark from the repository.

# Delete the updated bookmark
deleted_success = bookmark_repo.delete_bookmark(created_bookmark.id)

if deleted_success:
print(f"
Bookmark with ID {created_bookmark.id} deleted successfully.")
else:
print(f"Bookmark with ID {created_bookmark.id} not found for deletion.")

# Verify deletion
verified_deletion = bookmark_repo.get_bookmark(created_bookmark.id)
print(f"Bookmark exists after deletion attempt: {verified_deletion is not None}")

Managing Tags and Collections

The BookmarkRepository also provides methods for managing Tag and Collection entities, mirroring the CRUD operations available for bookmarks. You can create, retrieve, list, update, and delete tags and collections using similar patterns to the bookmark operations.

Important Considerations

When using the BookmarkRepository, be aware of the following critical points:

  • In-Memory Storage: The current implementation of BookmarkRepository stores all data in memory. This means that any data created or modified will be lost when the application restarts. It is suitable for development, testing, or scenarios where data persistence is handled externally, but it is not suitable for production environments requiring persistent storage.
  • No Transaction Support: The repository does not implement transactional operations. In a real-world database scenario, transactions are vital for ensuring data integrity, especially when multiple operations need to succeed or fail together. Without transactions, partial updates or inconsistencies can occur if an operation fails midway.
  • Hard-Delete Behavior: The delete_bookmark method performs a permanent deletion of the bookmark. Many applications prefer a "soft-delete" mechanism, where records are marked as deleted but remain in the database, allowing for recovery or historical tracking. If soft-delete is required, the BookmarkRepository would need to be extended to support this.

For production applications, it is recommended to replace or extend BookmarkRepository with an implementation that uses a persistent database (e.g., PostgreSQL, MySQL) and incorporates robust transaction management and appropriate deletion strategies.