from typing import List

from fastapi.encoders import jsonable_encoder
from sqlalchemy.orm import Session

from app.crud.base import CRUDBase
from app.models.article import Article
from app.schemas.article import ArticleCreate, ArticleUpdate

from app.utils import random_name

class CRUDArticle(CRUDBase[Article, ArticleCreate, ArticleUpdate]):
    def create_with_owner(
        self, db: Session, *, obj_in: ArticleCreate, owner_id: int, posted_datetime: str
    ) -> Article:
        obj_in_data = jsonable_encoder(obj_in)
        db_obj = self.model(**obj_in_data, owner_id=owner_id, posted_datetime=posted_datetime)
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj
    
article = CRUDArticle(Article)