from sqlalchemy.orm import Session
import models
from fastapi import HTTPException ,status
import schemas

def get_all(db:Session):
    blogs = db.query(models.Blog).all()
    return blogs

def show(id,db :Session):
    blog = db.query(models.Blog).filter(models.Blog.id==id).first()
    if not blog:
        raise HTTPException(status_code = status.HTTP_404_NOT_FOUND,
                                detail= f"Blog with the id {id} is not available")
        # response.status_code = status.HTTP_404_NOT_FOUND
        # return {'detail':f"Blog with the id {id} is not available"}

    return blog 

def destroy(id,db:Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f'Blog with id {id} not found')
    
    db.delete(synchronize_session=False)
    db.commit()
    return "done"

def update(id,request:schemas.Blog,db :Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                                detail=f'Blog with id {id} not found')

    blog.update(request)
    db.commit() 
    return 'update'

def create(request:schemas.Blog,db :Session):
    new_blog = models.Blog(title=request.title,body=request.body,user_id=1)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog 
6f1131bdbb22984eb2c010909941740fcfffccd2
<mask token>


def show(id, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id).first()
    if not blog:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with the id {id} is not available')
    return blog


def destroy(id, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with id {id} not found')
    db.delete(synchronize_session=False)
    db.commit()
    return 'done'


def update(id, request: schemas.Blog, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with id {id} not found')
    blog.update(request)
    db.commit()
    return 'update'


<mask token>

<mask token>


def get_all(db: Session):
    blogs = db.query(models.Blog).all()
    return blogs


def show(id, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id).first()
    if not blog:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with the id {id} is not available')
    return blog


def destroy(id, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with id {id} not found')
    db.delete(synchronize_session=False)
    db.commit()
    return 'done'


def update(id, request: schemas.Blog, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with id {id} not found')
    blog.update(request)
    db.commit()
    return 'update'


<mask token>

<mask token>


def get_all(db: Session):
    blogs = db.query(models.Blog).all()
    return blogs


def show(id, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id).first()
    if not blog:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with the id {id} is not available')
    return blog


def destroy(id, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with id {id} not found')
    db.delete(synchronize_session=False)
    db.commit()
    return 'done'


def update(id, request: schemas.Blog, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with id {id} not found')
    blog.update(request)
    db.commit()
    return 'update'


def create(request: schemas.Blog, db: Session):
    new_blog = models.Blog(title=request.title, body=request.body, user_id=1)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog

from sqlalchemy.orm import Session
import models
from fastapi import HTTPException, status
import schemas


def get_all(db: Session):
    blogs = db.query(models.Blog).all()
    return blogs


def show(id, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id).first()
    if not blog:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with the id {id} is not available')
    return blog


def destroy(id, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with id {id} not found')
    db.delete(synchronize_session=False)
    db.commit()
    return 'done'


def update(id, request: schemas.Blog, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=
            f'Blog with id {id} not found')
    blog.update(request)
    db.commit()
    return 'update'


def create(request: schemas.Blog, db: Session):
    new_blog = models.Blog(title=request.title, body=request.body, user_id=1)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog

from sqlalchemy.orm import Session
import models
from fastapi import HTTPException ,status
import schemas

def get_all(db:Session):
    blogs = db.query(models.Blog).all()
    return blogs

def show(id,db :Session):
    blog = db.query(models.Blog).filter(models.Blog.id==id).first()
    if not blog:
        raise HTTPException(status_code = status.HTTP_404_NOT_FOUND,
                                detail= f"Blog with the id {id} is not available")
        # response.status_code = status.HTTP_404_NOT_FOUND
        # return {'detail':f"Blog with the id {id} is not available"}

    return blog 

def destroy(id,db:Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f'Blog with id {id} not found')
    
    db.delete(synchronize_session=False)
    db.commit()
    return "done"

def update(id,request:schemas.Blog,db :Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                                detail=f'Blog with id {id} not found')

    blog.update(request)
    db.commit() 
    return 'update'

def create(request:schemas.Blog,db :Session):
    new_blog = models.Blog(title=request.title,body=request.body,user_id=1)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog 