68 lines
2.0 KiB
Python
68 lines
2.0 KiB
Python
"""
|
|
Video comparison model
|
|
"""
|
|
from datetime import datetime
|
|
from bson import ObjectId
|
|
from typing import List, Optional, Dict, Any
|
|
from app.models.database import get_database
|
|
|
|
class Comparison:
|
|
"""Video comparison model"""
|
|
|
|
COLLECTION_NAME = 'comparisons'
|
|
|
|
def __init__(self,
|
|
video_ids: List[ObjectId],
|
|
comparison_result: str,
|
|
created_at: Optional[datetime] = None,
|
|
_id: Optional[ObjectId] = None):
|
|
"""
|
|
Initialize Comparison model
|
|
|
|
Args:
|
|
video_ids: List of video document IDs
|
|
comparison_result: Comparison result text
|
|
created_at: Creation timestamp
|
|
_id: Document ID
|
|
"""
|
|
self._id = _id or ObjectId()
|
|
self.video_ids = video_ids
|
|
self.comparison_result = comparison_result
|
|
self.created_at = created_at or datetime.utcnow()
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
"""Convert to dictionary"""
|
|
return {
|
|
'_id': self._id,
|
|
'video_ids': self.video_ids,
|
|
'comparison_result': self.comparison_result,
|
|
'created_at': self.created_at
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: Dict[str, Any]) -> 'Comparison':
|
|
"""Create Comparison from dictionary"""
|
|
return cls(
|
|
_id=data.get('_id'),
|
|
video_ids=data['video_ids'],
|
|
comparison_result=data['comparison_result'],
|
|
created_at=data.get('created_at')
|
|
)
|
|
|
|
def save(self) -> ObjectId:
|
|
"""Save comparison to database"""
|
|
db = get_database()
|
|
result = db[self.COLLECTION_NAME].insert_one(self.to_dict())
|
|
self._id = result.inserted_id
|
|
return self._id
|
|
|
|
@classmethod
|
|
def find_by_id(cls, comparison_id: str) -> Optional['Comparison']:
|
|
"""Find comparison by ID"""
|
|
db = get_database()
|
|
doc = db[cls.COLLECTION_NAME].find_one({'_id': ObjectId(comparison_id)})
|
|
if doc:
|
|
return cls.from_dict(doc)
|
|
return None
|
|
|