-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Bookmark catalog-items (#2303)
* feat: Bookmark catalog-items
- Loading branch information
Showing
13 changed files
with
212 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from __future__ import annotations | ||
from typing import Optional, List | ||
import os | ||
from datetime import datetime, timedelta | ||
from sqlalchemy.orm import ( | ||
Mapped, | ||
mapped_column, | ||
relationship, | ||
selectinload, | ||
) | ||
from sqlalchemy import ( | ||
Boolean, | ||
DDL, | ||
ForeignKey, | ||
Integer, | ||
String, | ||
and_, | ||
desc, | ||
exists, | ||
event, | ||
or_, | ||
select, | ||
text, | ||
) | ||
from . import CustomBaseMinimal | ||
from .database import Database as db | ||
from .catalog_item import CatalogItem | ||
import logging | ||
|
||
logger = logging.getLogger() | ||
|
||
class Bookmark(CustomBaseMinimal): | ||
__tablename__ = 'bookmarks' | ||
__date_field__ = 'created_at' | ||
|
||
user_id: Mapped[int] = mapped_column(Integer, ForeignKey('users.id'), unique=False, nullable=False, comment='User id', primary_key=True) | ||
asset_uuid: Mapped[str] = mapped_column(String, ForeignKey('catalog_items.asset_uuid'), unique=False, nullable=False, primary_key=True) | ||
|
||
user: Mapped["User"] = relationship("User", back_populates="bookmarks") | ||
|
||
async def check_existing(self) -> Optional[Bookmark]: | ||
async with db.get_session() as session: | ||
stmt = select(Bookmark) | ||
stmt = stmt.where(and_(Bookmark.user_id == self.user_id, | ||
Bookmark.asset_uuid == self.asset_uuid | ||
) | ||
) | ||
result = await session.execute(stmt) | ||
return result.scalars().first() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from typing import List, Optional | ||
import logging | ||
from fastapi import APIRouter, HTTPException, Depends | ||
from schemas import ( | ||
BookmarkSchema, | ||
BookmarkListSchema | ||
) | ||
from models import Bookmark, User | ||
logger = logging.getLogger('babylon-ratings') | ||
|
||
|
||
tags = ["user-manager"] | ||
|
||
router = APIRouter(tags=tags) | ||
|
||
|
||
@router.get("/api/user-manager/v1/bookmarks/{email}", | ||
response_model=BookmarkListSchema, | ||
summary="Get favorites catalog item asset") | ||
async def bookmarks_get(email: str) -> BookmarkListSchema: | ||
|
||
logger.info(f"Getting favorites for user {email}") | ||
try: | ||
user = await User.get_by_email(email) | ||
if user: | ||
logger.info(user.bookmarks) | ||
return BookmarkListSchema(bookmarks=user.bookmarks) | ||
else: | ||
raise HTTPException(status_code=404, detail="User email doesn't exists") from e | ||
except Exception as e: | ||
logger.error(f"Error getting favorite: {e}", stack_info=True) | ||
raise HTTPException(status_code=500, detail="Error getting favorites") from e | ||
|
||
@router.post("/api/user-manager/v1/bookmarks", | ||
response_model=BookmarkListSchema, | ||
summary="Add bookmark", | ||
) | ||
async def bookmarks_post(email: str, | ||
asset_uuid: str) -> {}: | ||
|
||
logger.info(f"Add favorite item for user {email}") | ||
try: | ||
user = await User.get_by_email(email) | ||
if user: | ||
logger.info(user) | ||
bookmark = Bookmark.from_dict({"user_id": user.id, "asset_uuid": asset_uuid}) | ||
await bookmark.save() | ||
user = await User.get_by_email(email) | ||
return BookmarkListSchema(bookmarks=user.bookmarks) | ||
else: | ||
raise HTTPException(status_code=404, detail="User email doesn't exists") from e | ||
except Exception as e: | ||
logger.error(f"Error saving favorite: {e}", stack_info=True) | ||
raise HTTPException(status_code=500, detail="Error saving favorites") from e | ||
|
||
@router.delete("/api/user-manager/v1/bookmarks", | ||
response_model={}, | ||
summary="Delete bookmark", | ||
) | ||
async def bookmarks_delete(email: str, | ||
asset_uuid: str) -> BookmarkListSchema: | ||
|
||
logger.info(f"Delete favorite item for user {email}") | ||
try: | ||
user = await User.get_by_email(email) | ||
if user: | ||
bookmark = Bookmark.from_dict({"user_id": user.id, "asset_uuid": asset_uuid}) | ||
await bookmark.delete() | ||
user = await User.get_by_email(email) | ||
return BookmarkListSchema(bookmarks=user.bookmarks) | ||
else: | ||
raise HTTPException(status_code=404, detail="User email doesn't exists") from e | ||
|
||
except Exception as e: | ||
logger.error(f"Error deleting favorite: {e}", stack_info=True) | ||
raise HTTPException(status_code=404, detail="Error deleting favorite") from e | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import Optional, List | ||
from datetime import datetime | ||
from pydantic import BaseModel, Field | ||
|
||
|
||
class BookmarkSchema(BaseModel): | ||
asset_uuid: Optional[str] = Field(None, description="The asset uuid of the catalog item.") | ||
user_id: int = Field(..., description="The unique identifier for the user.") | ||
created_at: Optional[datetime] = Field(None, description="The date the bookmark was created.") | ||
updated_at: Optional[datetime] = Field(None, description="The date the bookmark was updated.") | ||
|
||
class Config: | ||
from_attributes = True | ||
|
||
class BookmarkListSchema(BaseModel): | ||
bookmarks: Optional[List[BookmarkSchema]] = Field(..., description="The list of bookmarks.") | ||
|
||
class Config: | ||
from_attributes = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters