Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,40 @@ class Book(Base):
title = Column(String(255), nullable=False)
author = Column(String(255), nullable=False)
pages_count = Column(Integer, nullable=True)


# Список книжок (мокові дані)
books = [
Book(id=1, title="Book 1", author="Author 1", pages_count = 'pages_count1'),
Book(id=2, title="Book 2", author="Author 2", pages_count = 'pages_count2'),
Book(id=3, title="Book 3", author="Author 3", pages_count = 'pages_count3'),
]


# Оновлення книжки за id
@app.put("/books/{id}")
async def update_book(id: int, book: Book):
for i in range(len(books)):
if books[i].id == id:
books[i] = book
return {"message": "Book updated"}
raise HTTPException(status_code=404, detail="Book not found")


# Видалення книжки за id
@app.delete("/books/{id}")
async def delete_book(id: int):
for i in range(len(books)):
if books[i].id == id:
del books[i]
return {"message": "Book deleted"}
raise HTTPException(status_code=404, detail="Book not found")


# Пошук книжки за id
@app.get("/books/{id}")
async def get_book(id: int):
for book in books:
if book.id == id:
return book
raise HTTPException(status_code=404, detail="Book not found")