fix part 2
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
from collections.abc import Sequence
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database.models.board_image import BoardImage
|
||||
from app.database.models.image import Image
|
||||
@@ -12,11 +13,11 @@ from app.database.models.image import Image
|
||||
class ImageRepository:
|
||||
"""Repository for image database operations."""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
def __init__(self, db: AsyncSession):
|
||||
"""Initialize repository with database session."""
|
||||
self.db = db
|
||||
|
||||
def create_image(
|
||||
async def create_image(
|
||||
self,
|
||||
user_id: UUID,
|
||||
filename: str,
|
||||
@@ -25,7 +26,7 @@ class ImageRepository:
|
||||
mime_type: str,
|
||||
width: int,
|
||||
height: int,
|
||||
metadata: dict,
|
||||
image_metadata: dict,
|
||||
) -> Image:
|
||||
"""Create new image record."""
|
||||
image = Image(
|
||||
@@ -36,59 +37,68 @@ class ImageRepository:
|
||||
mime_type=mime_type,
|
||||
width=width,
|
||||
height=height,
|
||||
image_metadata=metadata,
|
||||
image_metadata=image_metadata,
|
||||
)
|
||||
self.db.add(image)
|
||||
self.db.commit()
|
||||
self.db.refresh(image)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(image)
|
||||
return image
|
||||
|
||||
def get_image_by_id(self, image_id: UUID) -> Image | None:
|
||||
async def get_image_by_id(self, image_id: UUID) -> Image | None:
|
||||
"""Get image by ID."""
|
||||
return self.db.query(Image).filter(Image.id == image_id).first()
|
||||
result = await self.db.execute(select(Image).where(Image.id == image_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
def get_user_images(
|
||||
async def get_user_images(
|
||||
self, user_id: UUID, limit: int = 50, offset: int = 0
|
||||
) -> tuple[Sequence[Image], int]:
|
||||
"""Get all images for a user with pagination."""
|
||||
total = self.db.query(Image).filter(Image.user_id == user_id).count()
|
||||
images = (
|
||||
self.db.query(Image)
|
||||
.filter(Image.user_id == user_id)
|
||||
from sqlalchemy import func
|
||||
|
||||
# Get total count efficiently
|
||||
count_result = await self.db.execute(
|
||||
select(func.count(Image.id)).where(Image.user_id == user_id)
|
||||
)
|
||||
total = count_result.scalar_one()
|
||||
|
||||
# Get paginated images
|
||||
result = await self.db.execute(
|
||||
select(Image)
|
||||
.where(Image.user_id == user_id)
|
||||
.order_by(Image.created_at.desc())
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
.all()
|
||||
)
|
||||
images = result.scalars().all()
|
||||
return images, total
|
||||
|
||||
def delete_image(self, image_id: UUID) -> bool:
|
||||
async def delete_image(self, image_id: UUID) -> bool:
|
||||
"""Delete image record."""
|
||||
image = self.get_image_by_id(image_id)
|
||||
image = await self.get_image_by_id(image_id)
|
||||
if not image:
|
||||
return False
|
||||
|
||||
self.db.delete(image)
|
||||
self.db.commit()
|
||||
await self.db.delete(image)
|
||||
await self.db.commit()
|
||||
return True
|
||||
|
||||
def increment_reference_count(self, image_id: UUID) -> None:
|
||||
async def increment_reference_count(self, image_id: UUID) -> None:
|
||||
"""Increment reference count for image."""
|
||||
image = self.get_image_by_id(image_id)
|
||||
image = await self.get_image_by_id(image_id)
|
||||
if image:
|
||||
image.reference_count += 1
|
||||
self.db.commit()
|
||||
await self.db.commit()
|
||||
|
||||
def decrement_reference_count(self, image_id: UUID) -> int:
|
||||
async def decrement_reference_count(self, image_id: UUID) -> int:
|
||||
"""Decrement reference count for image."""
|
||||
image = self.get_image_by_id(image_id)
|
||||
image = await self.get_image_by_id(image_id)
|
||||
if image and image.reference_count > 0:
|
||||
image.reference_count -= 1
|
||||
self.db.commit()
|
||||
await self.db.commit()
|
||||
return image.reference_count
|
||||
return 0
|
||||
|
||||
def add_image_to_board(
|
||||
async def add_image_to_board(
|
||||
self,
|
||||
board_id: UUID,
|
||||
image_id: UUID,
|
||||
@@ -107,36 +117,68 @@ class ImageRepository:
|
||||
self.db.add(board_image)
|
||||
|
||||
# Increment reference count
|
||||
self.increment_reference_count(image_id)
|
||||
await self.increment_reference_count(image_id)
|
||||
|
||||
self.db.commit()
|
||||
self.db.refresh(board_image)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(board_image)
|
||||
return board_image
|
||||
|
||||
def get_board_images(self, board_id: UUID) -> Sequence[BoardImage]:
|
||||
async def get_board_images(self, board_id: UUID) -> Sequence[BoardImage]:
|
||||
"""Get all images for a board, ordered by z-order."""
|
||||
return (
|
||||
self.db.query(BoardImage)
|
||||
.filter(BoardImage.board_id == board_id)
|
||||
result = await self.db.execute(
|
||||
select(BoardImage)
|
||||
.where(BoardImage.board_id == board_id)
|
||||
.order_by(BoardImage.z_order.asc())
|
||||
.all()
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
def remove_image_from_board(self, board_id: UUID, image_id: UUID) -> bool:
|
||||
"""Remove image from board."""
|
||||
board_image = (
|
||||
self.db.query(BoardImage)
|
||||
.filter(BoardImage.board_id == board_id, BoardImage.image_id == image_id)
|
||||
.first()
|
||||
async def get_board_image(self, board_id: UUID, image_id: UUID) -> BoardImage | None:
|
||||
"""Get a specific board image."""
|
||||
result = await self.db.execute(
|
||||
select(BoardImage)
|
||||
.where(BoardImage.board_id == board_id, BoardImage.image_id == image_id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def update_board_image(
|
||||
self,
|
||||
board_id: UUID,
|
||||
image_id: UUID,
|
||||
position: dict | None = None,
|
||||
transformations: dict | None = None,
|
||||
z_order: int | None = None,
|
||||
group_id: UUID | None = None,
|
||||
) -> BoardImage | None:
|
||||
"""Update board image position, transformations, z-order, or group."""
|
||||
board_image = await self.get_board_image(board_id, image_id)
|
||||
|
||||
if not board_image:
|
||||
return None
|
||||
|
||||
if position is not None:
|
||||
board_image.position = position
|
||||
if transformations is not None:
|
||||
board_image.transformations = transformations
|
||||
if z_order is not None:
|
||||
board_image.z_order = z_order
|
||||
if group_id is not None:
|
||||
board_image.group_id = group_id
|
||||
|
||||
await self.db.commit()
|
||||
await self.db.refresh(board_image)
|
||||
return board_image
|
||||
|
||||
async def remove_image_from_board(self, board_id: UUID, image_id: UUID) -> bool:
|
||||
"""Remove image from board."""
|
||||
board_image = await self.get_board_image(board_id, image_id)
|
||||
|
||||
if not board_image:
|
||||
return False
|
||||
|
||||
self.db.delete(board_image)
|
||||
await self.db.delete(board_image)
|
||||
|
||||
# Decrement reference count
|
||||
self.decrement_reference_count(image_id)
|
||||
await self.decrement_reference_count(image_id)
|
||||
|
||||
self.db.commit()
|
||||
await self.db.commit()
|
||||
return True
|
||||
|
||||
@@ -26,13 +26,14 @@ class ImageUploadResponse(BaseModel):
|
||||
mime_type: str
|
||||
width: int
|
||||
height: int
|
||||
metadata: dict[str, Any]
|
||||
metadata: dict[str, Any] = Field(..., alias="image_metadata")
|
||||
created_at: datetime
|
||||
|
||||
class Config:
|
||||
"""Pydantic config."""
|
||||
|
||||
from_attributes = True
|
||||
populate_by_name = True
|
||||
|
||||
|
||||
class ImageResponse(BaseModel):
|
||||
@@ -46,7 +47,7 @@ class ImageResponse(BaseModel):
|
||||
mime_type: str
|
||||
width: int
|
||||
height: int
|
||||
metadata: dict[str, Any]
|
||||
metadata: dict[str, Any] = Field(..., alias="image_metadata")
|
||||
created_at: datetime
|
||||
reference_count: int
|
||||
|
||||
@@ -54,6 +55,7 @@ class ImageResponse(BaseModel):
|
||||
"""Pydantic config."""
|
||||
|
||||
from_attributes = True
|
||||
populate_by_name = True
|
||||
|
||||
|
||||
class BoardImageCreate(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user