forked from vikram-dagger/dagger-python-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepositories.py
More file actions
43 lines (33 loc) · 1.1 KB
/
repositories.py
File metadata and controls
43 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from sqlalchemy.orm import Session
from . import models
# Create a new book
def create_book(db: Session, book: models.BookIn):
db_book = models.Book(title=book.title, author=book.author)
db.add(db_book)
db.commit()
db.refresh(db_book)
return db_book
# Get all books
def get_books(db: Session, skip: int = 0, limit: int = 10):
return db.query(models.Book).offset(skip).limit(limit).all()
# Get a book by ID
def get_book(db: Session, book_id: int):
return db.query(models.Book).filter(models.Book.id == book_id).first()
# Update a book
def update_book(db: Session, book_id: int, book: models.BookIn):
db_book = db.query(models.Book).filter(models.Book.id == book_id).first()
if db_book:
db_book.title = book.title
db_book.author = book.author
db.commit()
db.refresh(db_book)
return db_book
return None
# Delete a book
def delete_book(db: Session, book_id: int):
db_book = db.query(models.Book).filter(models.Book.id == book_id).first()
if db_book:
db.delete(db_book)
db.commit()
return db_book
return None