-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
154 lines (143 loc) · 5.05 KB
/
Copy pathmain.py
File metadata and controls
154 lines (143 loc) · 5.05 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security.oauth2 import OAuth2PasswordBearer, OAuth2PasswordRequestForm
import httpx
from mysql.connector import cursor
from db import get_db
import dotenv
import os
dotenv.load_dotenv()
import json
import uvicorn
from fastapi import FastAPI, Depends, HTTPException, Request
from authlib.integrations.starlette_client import OAuth
from starlette.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"], # 检查这里的域名是否正确
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
#取得所有使用者
oauth = OAuth()
oauth.register(
name="nycu",
client_id=os.getenv("OAUTH_ID"),
client_secret=os.getenv("OAUTH_KEY"),
authorize_url="https://nycu.edu.tw/oauth/authorize",
authorize_params=None,
authorize_prompt=None,
authorize_response=None,
authorize_token=None,
authorize_token_url="https://nycu.edu.tw/oauth/token",
client_kwargs=None,
)
@app.get("/")
async def hello():
return {"message": "welcome to NYCU 14 UNION greeting api"}
@app.get("/login")
async def login(request: Request):
return RedirectResponse(f'https://id.nycu.edu.tw/o/authorize/?client_id={os.getenv("OAUTH_ID")}&response_type=code&scope=profile&redirect_uri=http://127.0.0.1:8000/callback')
@app.get("/callback")
async def callback(code):
print(code)
# token = await oauth.nycu.authorize_access_token(request)
# user = await oauth.nycu.parse_id_token(request, token)
return code
# return {"success": True, "user": user}
@app.get("/users/")
async def get_users(db: cursor.MySQLCursor = Depends(get_db)):
query = "SELECT * FROM users"
db.execute(query)
result = db.fetchall()
if result:
return {"users": result}
else:
return {"error": "User not found"}
# 取得使用者
@app.get("/users/{user_id}")
async def get_user(user_id: str,
db: cursor.MySQLCursor = Depends(get_db)):
query = f"SELECT * FROM users WHERE id = {user_id};"
print(query)
db.execute(query)
result = db.fetchall()
if result:
print({"id": result[0][0], "name": result[0][1], "money":result[0][2], "at_1":result[0][3],"at_2":result[0][4]})
return {"id": result[0][0], "name": result[0][1], "money":result[0][2], "at_1":result[0][3],"at_2":result[0][4]}
else:
#create user
print("user not found, creating...")
query = f"INSERT INTO users (id) VALUES ({user_id});"
db.execute(query)
db.execute("COMMIT")
#find user
query = f"SELECT * FROM users WHERE id = {user_id};"
db.execute(query)
result = db.fetchall()
print({"id": result[0][0], "name": result[0][1], "money":result[0][2], "at_1":result[0][3],"at_2":result[0][4]})
return {"id": result[0][0], "name": result[0][1], "money":result[0][2], "at_1":result[0][3],"at_2":result[0][4]}
# 創建使用者
@app.get("/create/{user_id}")
async def insert_user(user_id: str,
db: cursor.MySQLCursor = Depends(get_db)):
query = "INSERT INTO users (id) VALUES (%s)"
db.execute(query, (user_id,))
result = db.fetchone()
db.execute("COMMIT")
return {"user_name": user_id}
# 下注
@app.get("/bet/{user_id}/{at}/{money}")
async def bet(user_id: str,
at: str,
money: int,
db: cursor.MySQLCursor = Depends(get_db)):
query = f"UPDATE users SET money = money-{money}, {at} = {at}+{money} WHERE id = {user_id} ;"
db.execute(query)
db.execute("COMMIT")
return {"bet": "success"}
# return await get_user(user_id)
# 取得下注金額
@app.get("/total_bet_money")
async def get_total_bet_money(db: cursor.MySQLCursor = Depends(get_db)):
query = "SELECT SUM(at_1), SUM(at_2) FROM users"
db.execute(query)
result = db.fetchone()
db.execute("COMMIT")
# print(result)
return result
# 截標
@app.get("/stop/{at_1_rate}/{at_2_rate}")
async def get_new_money(at_1_rate:float,
at_2_rate:float,
db: cursor.MySQLCursor = Depends(get_db)):
query = f"UPDATE users SET money = money+at_1*{at_1_rate}+ at_2*{at_2_rate}, at_1 = 0, at_2 = 0;"
db.execute(query)
db.execute("COMMIT")
return {"stop": "success"}
if __name__ == "__main__":
uvicorn.run("main:app", reload=True)
"""
mysql> CREATE TABLE
-> `users` (
-> `id` varchar(255) NOT NULL,
-> `name` varchar(255) DEFAULT NULL,
-> `money` int(11) DEFAULT 1000,
-> `at_1` int(11) DEFAULT 0,
-> `at_2` int(11) DEFAULT 0,
-> PRIMARY KEY (`id`)
-> ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb3;
"""
"""
SELECT * FROM users
-> ;
+------+------+-------+------+------+
| id | name | money | at_1 | at_2 |
+------+------+-------+------+------+
| 1111 | Atom | 1000 | 0 | 0 |
| 2222 | NULL | 1000 | 0 | 0 |
+------+------+-------+------+------+
2 rows in set (0.00 sec)
"""