fixed error on empty poems table

This commit is contained in:
Dmitriy Shishkov 2023-08-15 23:42:04 +03:00
parent 11abdb9147
commit 1a51c580d4
Signed by: dm1sh
GPG Key ID: 027994B0AA357688

@ -191,6 +191,8 @@ def add_points(user_id: int, db: Annotated[Session, Depends(utils.get_db)]):
@app.get("/api/user/poem", response_model=schemas.Poem) # пока не работает
def poems_to_front(db: Annotated[Session, Depends(utils.get_db)]): # db: Annotated[Session, Depends(utils.get_db)]
num_of_poems = db.query(models.Poems).count() # определяем кол-во стихов в бд
if num_of_poems < 1:
raise HTTPException(status_code=404, detail="Poems not found")
rand_id = random.randint(1, num_of_poems) # генерируем номер стихотворения
poem = db.query(models.Poems).filter(models.Poems.id == rand_id).first() # находим стих в бд
if not poem: