Spaces:
Runtime error
Runtime error
Ved Gupta
commited on
Commit
·
a854ef9
1
Parent(s):
8713eac
updated
Browse files- app/api/endpoints/users.py +6 -7
- app/core/errors.py +1 -1
- app/core/models/User.py +26 -0
- app/main.py +1 -1
app/api/endpoints/users.py
CHANGED
@@ -5,6 +5,7 @@ from app.api.models.user import User, UpdateUser, UserResponse
|
|
5 |
from app.core.database import SessionLocal
|
6 |
from app.core.security import get_password_hash, verify_password
|
7 |
from app.core.models import UserInDB
|
|
|
8 |
|
9 |
database = SessionLocal()
|
10 |
users_router = router = APIRouter()
|
@@ -12,16 +13,14 @@ users_router = router = APIRouter()
|
|
12 |
|
13 |
@router.post("/", status_code=201)
|
14 |
async def create_user(user: User):
|
15 |
-
|
|
|
16 |
username=user.username,
|
17 |
email=user.email,
|
18 |
-
|
19 |
)
|
20 |
-
|
21 |
-
|
22 |
-
database.refresh(db_user)
|
23 |
-
print(db_user)
|
24 |
-
return {**db_user.data()}
|
25 |
|
26 |
|
27 |
@router.get("/{user_id}/", response_model=UserResponse)
|
|
|
5 |
from app.core.database import SessionLocal
|
6 |
from app.core.security import get_password_hash, verify_password
|
7 |
from app.core.models import UserInDB
|
8 |
+
from app.core.models.User import UserController
|
9 |
|
10 |
database = SessionLocal()
|
11 |
users_router = router = APIRouter()
|
|
|
13 |
|
14 |
@router.post("/", status_code=201)
|
15 |
async def create_user(user: User):
|
16 |
+
USER = UserController(database)
|
17 |
+
USER.create(
|
18 |
username=user.username,
|
19 |
email=user.email,
|
20 |
+
password=user.password,
|
21 |
)
|
22 |
+
|
23 |
+
return USER.details()
|
|
|
|
|
|
|
24 |
|
25 |
|
26 |
@router.get("/{user_id}/", response_model=UserResponse)
|
app/core/errors.py
CHANGED
@@ -17,5 +17,5 @@ async def error_handler(request: Request, exc: Exception) -> JSONResponse:
|
|
17 |
|
18 |
return JSONResponse(
|
19 |
status_code=500,
|
20 |
-
content={"detail":
|
21 |
)
|
|
|
17 |
|
18 |
return JSONResponse(
|
19 |
status_code=500,
|
20 |
+
content={"detail": str(exc)},
|
21 |
)
|
app/core/models/User.py
CHANGED
@@ -10,6 +10,8 @@ from sqlalchemy.sql import func
|
|
10 |
|
11 |
from app.core.database import Base
|
12 |
|
|
|
|
|
13 |
|
14 |
class UserInDB(Base):
|
15 |
__tablename__ = "users"
|
@@ -34,3 +36,27 @@ class UserInDB(Base):
|
|
34 |
"email": self.email,
|
35 |
"created_at": self.created_at,
|
36 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
from app.core.database import Base
|
12 |
|
13 |
+
from app.core.security import get_password_hash, verify_password
|
14 |
+
|
15 |
|
16 |
class UserInDB(Base):
|
17 |
__tablename__ = "users"
|
|
|
36 |
"email": self.email,
|
37 |
"created_at": self.created_at,
|
38 |
}
|
39 |
+
|
40 |
+
|
41 |
+
class UserController:
|
42 |
+
UserInDB = UserInDB
|
43 |
+
|
44 |
+
def __init__(self, database):
|
45 |
+
self.db = database
|
46 |
+
|
47 |
+
def create(self, username: str, email: str, password: str):
|
48 |
+
self.username = username
|
49 |
+
self.email = email
|
50 |
+
self.hashed_password = get_password_hash(password)
|
51 |
+
db_user = UserInDB(
|
52 |
+
username=self.username,
|
53 |
+
email=self.email,
|
54 |
+
hashed_password=self.hashed_password,
|
55 |
+
)
|
56 |
+
self.db.add(db_user)
|
57 |
+
self.db.commit()
|
58 |
+
self.db.refresh(db_user)
|
59 |
+
self.user = db_user.data()
|
60 |
+
|
61 |
+
def details(self):
|
62 |
+
return self.user
|
app/main.py
CHANGED
@@ -24,7 +24,7 @@ if settings.BACKEND_CORS_ORIGINS:
|
|
24 |
app.include_router(api_router, prefix=settings.API_V1_STR)
|
25 |
|
26 |
# # Error handlers
|
27 |
-
app.add_exception_handler(422, error_handler)
|
28 |
app.add_exception_handler(500, error_handler)
|
29 |
|
30 |
# Print all routes
|
|
|
24 |
app.include_router(api_router, prefix=settings.API_V1_STR)
|
25 |
|
26 |
# # Error handlers
|
27 |
+
# app.add_exception_handler(422, error_handler)
|
28 |
app.add_exception_handler(500, error_handler)
|
29 |
|
30 |
# Print all routes
|