Skip to content

Commit 728a70d

Browse files
committed
Just use User model
We don't actually need separate models here as the `User` with events isn't used at all. Simplify!
1 parent ea67bae commit 728a70d

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

building-with-fast-api/chapter_05/models/users.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# ------------------------------------------------------------------------------
66
# Our USER model
77
# ==============================================================================
8-
# Here we'll set our user model, which has a `User.Events` column, to list all
8+
# ~~Here we'll set our user model, which has a `User.Events` column, to list all
99
# the events attached to that specific user. We have a couple of types, depending
10-
# on the situation (for example, sign-in form). We could also create a separate
10+
# on the situation (for example, sign-in form).~~ We could also create a separate
1111
# `User` model for the `response_model=` in our routes, to hide the `password`.
1212
#
1313
# Questions
@@ -19,9 +19,3 @@
1919
class User(BaseModel):
2020
email: EmailStr # A custom type for emails
2121
password: str
22-
events: Optional[List[Event]] # Empty by default, currently not used
23-
24-
# For `sign-up` and `sign-in`
25-
class UserSign(BaseModel):
26-
email: EmailStr
27-
password: str

building-with-fast-api/chapter_05/routes/users.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from fastapi import APIRouter, HTTPException, status
2-
from models.users import User, UserSign
2+
from models.users import User
33

44
# ------------------------------------------------------------------------------
55
# Our USERS routes
@@ -27,15 +27,15 @@
2727
# for both, rather than creating separate models for `sign-up` and `sign-in`.
2828

2929
@user_router.post("/signup")
30-
async def sign_new_user(data: UserSign) -> dict:
30+
async def sign_new_user(data: User) -> dict:
3131
if data.email in users:
3232
raise HTTPException(status_code=409, detail="Username already exists")
3333

3434
users[data.email] = data # != Ask copilot to explain this line
3535
return { "message": "User registered!" }
3636

3737
@user_router.post("/signin")
38-
async def sign_in_user(user: UserSign) -> dict:
38+
async def sign_in_user(user: User) -> dict:
3939
# Check if `UserSign().email` is in the `users` dictionary
4040
# You could also use `if _ or _` here!
4141
if user.email not in users:

0 commit comments

Comments
 (0)