-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (61 loc) · 1.86 KB
/
main.py
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
import mysql.connector
from fastapi import FastAPI, Query, HTTPException
mydb = mysql.connector.connect(
host = "localhost",
user = "admin",
password = "admin",
database = "my_api"
)
mycursor = mydb.cursor()
table = []
mycursor.execute("SHOW TABLES LIKE 'user'")
for x in mycursor:
table.append(x)
# print(table)
if table == [] :
mycursor.execute("CREATE TABLE user (name VARCHAR(255), phone VARCHAR(255))")
app = FastAPI()
@app.post("/adduser/")
def Add_User(name: str, phone: str = Query(None, min_length=11, max_length=11, regex="^09")):
if name == "":
raise HTTPException(status_code = 400, detail = "Please Enter Your Name")
if phone == None:
raise HTTPException(status_code = 400, detail = "Please Enter Your PhoneNumber")
mycursor.execute("SELECT * FROM user")
myresult = mycursor.fetchall()
listt = []
list_names = []
list_phones = []
for x in myresult:
listt.append(list(x))
# print(listt)
for z in listt:
names = z[0]
list_names.append(names)
# print(list_names)
for y in listt:
phones = z[1]
list_phones.append(phones)
# print(list_phones)
if name in list_names:
raise HTTPException(status_code = 400, detail = "Name is existed")
if phone in list_phones:
raise HTTPException(status_code = 400, detail = "PhoneNumber is existed")
else:
sql = 'INSERT INTO user (name, phone) VALUES (%s, %s)'
val = (name, phone)
mycursor.execute(sql, val)
mydb.commit()
return {"Username": name, "PhoneNumber": phone}
@app.get("/read/")
def show_users():
mycursor.execute("SELECT * FROM user")
myresult = mycursor.fetchall()
listt = []
for x in myresult:
listt.append(x)
return listt
@app.get("/div/")
async def divition(a: int, b: int):
div = a/b
return int(div)