-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (61 loc) · 1.8 KB
/
Copy pathmain.py
File metadata and controls
87 lines (61 loc) · 1.8 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
from fastapi import FastAPI
from pydantic import BaseModel
from sqlalchemy import Column, TEXT, VARCHAR, LargeBinary, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import uuid
app = FastAPI()
DATABASE = "postgresql://postgres:jiyad123@localhost:5432/Flutter test"
engine = create_engine(DATABASE)
sessionLocal = sessionmaker(autoflush=False, bind=engine)
db = sessionLocal()
## Signup Model
class CreateUser(BaseModel):
email: str
name: str
password: str
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(TEXT, primary_key=True)
name = Column(VARCHAR(50))
email = Column(VARCHAR(50))
password = Column(TEXT)
## Create a API for Signup User:
@app.post("/signup")
def singupUser(user: CreateUser):
# Extract the data from Request
print(user.email + "this is email")
print(user.name + "this is name")
print(user.password + "this is password")
# Check if the user already registser or not
userDb = db.query(User).filter(User.email == user.email).first()
if userDb:
return "User Already Exist"
# Add New User to DB
userDb = User(
id=str(uuid.uuid4()),
name=user.name,
email=user.email,
password=user.password,
)
db.add(userDb)
db.commit()
return userDb
pass
Base.metadata.create_all(engine)
## one way of sending request
# @app.post('/')
# async def testApi(request: Request):
# print((await request.body()).decode())
# return 'somthing printing'
# --------------------------------------------------
## another way
# class Test(BaseModel):
# name:str
# age:int
# ==============================
# @app.post('/')
# def testApi(t: Test):
# print(t)
# return 'somthing printing'