1
0
mirror of https://github.com/aquatix/digimarks.git synced 2025-12-06 22:05:09 +01:00

Refactoring tags endpoints to use tags_service

This commit is contained in:
2025-09-14 22:10:40 +02:00
parent 425b9441ed
commit ac4ae2edd0
2 changed files with 34 additions and 13 deletions

View File

@@ -288,25 +288,18 @@ async def list_tags_for_user(
user_key: str, user_key: str,
) -> list[str]: ) -> list[str]:
"""List all tags in use by the user.""" """List all tags in use by the user."""
result = await session.exec( return await tags_service.list_tags_for_user(session, user_key)
select(Bookmark).where(Bookmark.user_key == user_key, Bookmark.status != Visibility.DELETED)
)
bookmarks = result.all()
tags = []
for bookmark in bookmarks:
tags += bookmark.tag_list
return tags_service.clean_tags(tags)
@app.get('/api/v1/{user_key}/tags/{tag_key}') @app.get('/api/v1/{user_key}/tags/{tag_key}')
async def list_bookmarks_for_tag_for_user( async def list_bookmarks_for_tag_for_user(
session: SessionDep, session: SessionDep,
user_key: str, user_key: str,
tag_key: str,
) -> list[str]: ) -> list[str]:
"""List all tags in use by the user.""" """List all tags in use by the user."""
result = await session.exec(select(Bookmark).where(Bookmark.user_key == user_key)) logger.info('List bookmarks for tag %s user %s', user_key)
bookmarks = result.all() return await tags_service.list_bookmarks_for_tag_for_user(session, user_key, tag_key)
return tags_service.list_tags_for_bookmarks(bookmarks)
@app.get('/{user_key}', response_class=HTMLResponse) @app.get('/{user_key}', response_class=HTMLResponse)

View File

@@ -1,8 +1,9 @@
"""Helper functions for tags used with Bookmark models.""" """Helper functions for tags used with Bookmark models."""
from sqlalchemy import Sequence from sqlalchemy import Sequence
from sqlmodel import select
from src.digimarks.models import Bookmark from src.digimarks.models import Bookmark, Visibility
def i_filter_false(predicate, iterable): def i_filter_false(predicate, iterable):
@@ -56,7 +57,7 @@ def list_tags_for_bookmarks(bookmarks: Sequence[Bookmark]) -> list[str]:
"""Generate a unique list of the tags from the list of bookmarks.""" """Generate a unique list of the tags from the list of bookmarks."""
tags = [] tags = []
for bookmark in bookmarks: for bookmark in bookmarks:
tags += bookmark.tags_list tags += bookmark.tag_list
return clean_tags(tags) return clean_tags(tags)
@@ -69,3 +70,30 @@ def set_tags(bookmark: Bookmark, new_tags: str) -> None:
tags_split = new_tags.split(',') tags_split = new_tags.split(',')
tags_clean = clean_tags(tags_split) tags_clean = clean_tags(tags_split)
bookmark.tags = ','.join(tags_clean) bookmark.tags = ','.join(tags_clean)
async def list_tags_for_user(
session,
user_key: str,
) -> list[str]:
"""List all tags in use by the user."""
result = await session.exec(
select(Bookmark).where(Bookmark.user_key == user_key, Bookmark.status != Visibility.DELETED)
)
bookmarks = result.all()
tags = []
for bookmark in bookmarks:
tags += bookmark.tag_list
return clean_tags(tags)
async def list_bookmarks_for_tag_for_user(
session,
user_key: str,
tag_key: str,
) -> list[str]:
"""List all tags in use by the user."""
result = await session.exec(select(Bookmark).where(Bookmark.user_key == user_key))
# TODO: filter on tag_key
bookmarks = result.all()
return list_tags_for_bookmarks(bookmarks)