-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud.py
More file actions
112 lines (86 loc) · 2.14 KB
/
Copy pathcrud.py
File metadata and controls
112 lines (86 loc) · 2.14 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from sqlalchemy.orm import Session
import models
from auth import hash_password
import schemas
from auth import hash_password, verify_password
def create_user(db: Session, user: schemas.UserCreate):
hashed_password = hash_password(user.password)
db_user = models.User(name=user.name,hashed_password=hashed_password)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def get_users(db:Session):
return db.query(models.User).all()
def get_user(
db: Session,
user_id: int
):
return (
db.query(models.User)
.filter(models.User.id == user_id)
.first()
)
def delete_user(
db: Session,
user_id: int
):
user = (
db.query(models.User)
.filter(models.User.id == user_id)
.first()
)
if user:
db.delete(user)
db.commit()
return user
def authenticate_user(
db: Session,
name: str,
password: str
):
user = (
db.query(models.User)
.filter(models.User.name == name)
.first()
)
if not user:
return None
if not verify_password(password, user.hashed_password):
return None
return user
def create_book(db: Session, book: schemas.BookCreate):
db_book = models.Book(
title=book.title,
user_id=book.user_id
)
db.add(db_book)
db.commit()
db.refresh(db_book)
return db_book
def get_books(db: Session):
return db.query(models.Book).all()
def get_book(db: Session, book_id: int):
return db.query(models.Book).filter(
models.Book.id == book_id
).first()
def update_book(db: Session, book_id: int, book: schemas.BookCreate):
db_book = db.query(models.Book).filter(
models.Book.id == book_id
).first()
if not db_book:
return None
db_book.title = book.title
db_book.user_id = book.user_id
db.commit()
db.refresh(db_book)
return db_book
def delete_book(db: Session, book_id: int):
db_book = db.query(models.Book).filter(
models.Book.id == book_id
).first()
if not db_book:
return None
db.delete(db_book)
db.commit()
return db_book