fixes
This commit is contained in:
@@ -26,7 +26,8 @@ class GenerationRepo:
|
||||
return Generation(**res)
|
||||
|
||||
async def get_generations(self, character_id: Optional[str] = None, status: Optional[GenerationStatus] = None,
|
||||
limit: int = 10, offset: int = 0, created_by: Optional[str] = None, project_id: Optional[str] = None, idea_id: Optional[str] = None) -> List[Generation]:
|
||||
limit: int = 10, offset: int = 0, created_by: Optional[str] = None, project_id: Optional[str] = None,
|
||||
idea_id: Optional[str] = None, only_liked_by: Optional[str] = None) -> List[Generation]:
|
||||
|
||||
filter: dict[str, Any] = {"is_deleted": False}
|
||||
if character_id is not None:
|
||||
@@ -43,6 +44,8 @@ class GenerationRepo:
|
||||
filter["project_id"] = project_id
|
||||
if idea_id is not None:
|
||||
filter["idea_id"] = idea_id
|
||||
if only_liked_by is not None:
|
||||
filter["liked_by"] = only_liked_by
|
||||
|
||||
# If fetching for an idea, sort by created_at ascending (cronological)
|
||||
# Otherwise typically descending (newest first)
|
||||
@@ -57,7 +60,8 @@ class GenerationRepo:
|
||||
return generations
|
||||
|
||||
async def count_generations(self, character_id: Optional[str] = None, status: Optional[GenerationStatus] = None,
|
||||
album_id: Optional[str] = None, created_by: Optional[str] = None, project_id: Optional[str] = None, idea_id: Optional[str] = None) -> int:
|
||||
album_id: Optional[str] = None, created_by: Optional[str] = None, project_id: Optional[str] = None,
|
||||
idea_id: Optional[str] = None, only_liked_by: Optional[str] = None) -> int:
|
||||
args = {}
|
||||
if character_id is not None:
|
||||
args["linked_character_id"] = character_id
|
||||
@@ -73,6 +77,8 @@ class GenerationRepo:
|
||||
args["idea_id"] = idea_id
|
||||
if album_id is not None:
|
||||
args["album_id"] = album_id
|
||||
if only_liked_by is not None:
|
||||
args["liked_by"] = only_liked_by
|
||||
return await self.collection.count_documents(args)
|
||||
|
||||
async def get_generations_by_ids(self, generation_ids: List[str]) -> List[Generation]:
|
||||
@@ -94,6 +100,37 @@ class GenerationRepo:
|
||||
async def update_generation(self, generation: Generation, ):
|
||||
res = await self.collection.update_one({"_id": ObjectId(generation.id)}, {"$set": generation.model_dump()})
|
||||
|
||||
async def toggle_like(self, generation_id: str, user_id: str) -> bool | None:
|
||||
"""
|
||||
Toggles like for a user on a generation.
|
||||
Returns True if liked, False if unliked, None if generation not found.
|
||||
"""
|
||||
if not ObjectId.is_valid(generation_id):
|
||||
return None
|
||||
|
||||
oid = ObjectId(generation_id)
|
||||
|
||||
# Check if generation exists
|
||||
gen = await self.collection.find_one({"_id": oid}, {"liked_by": 1})
|
||||
|
||||
if not gen:
|
||||
return None
|
||||
|
||||
if user_id in gen.get("liked_by", []):
|
||||
# Unlike
|
||||
await self.collection.update_one(
|
||||
{"_id": oid},
|
||||
{"$pull": {"liked_by": user_id}}
|
||||
)
|
||||
return False
|
||||
else:
|
||||
# Like
|
||||
await self.collection.update_one(
|
||||
{"_id": oid},
|
||||
{"$addToSet": {"liked_by": user_id}}
|
||||
)
|
||||
return True
|
||||
|
||||
async def get_usage_stats(self, created_by: Optional[str] = None, project_id: Optional[str] = None) -> dict:
|
||||
"""
|
||||
Calculates usage statistics (runs, tokens, cost) using MongoDB aggregation.
|
||||
|
||||
Reference in New Issue
Block a user