-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
130 lines (91 loc) · 3.34 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
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
import sys
import asyncio
if sys.platform != "win32":
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
from src.utils.asyncMySQL import AsyncMySQL
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
@app.on_event("startup")
async def startup():
global db
db = AsyncMySQL()
await db.init()
@app.on_event("shutdown")
async def shutdown():
await db.close()
@app.get("/history/user")
async def get_history(uid: str, page: int = 1, isReverse: bool = False):
res = await db.search("AID,CID,update_time", "history", f"UID={uid}",
f"{(page-1)*20},{page*20}",
f"update_time {'ASC' if isReverse else 'DESC'}")
ret_list = [{
"aid": item[0],
"cid": item[1],
"update_time":item[2].strftime("%Y-%m-%d %H:%M:%S")
} for item in res]
return ret_list
@app.get("/history/album")
async def get_history(uid: int, aid: int):
res = await db.search("UID,CID,update_time", "history", f"UID={uid} and AID={aid}",
sort=f"update_time ASC")
if res == ():
return None
ret_dict = {
"uid": res[0][0],
"cid": res[0][1],
"update_time": res[0][2].strftime("%Y-%m-%d %H:%M:%S")
}
return ret_dict
@app.delete("/history")
async def delete_history(uid: str):
await db.delete("history", f"UID={uid}")
return Response(status_code=200)
@app.post("/record")
async def update_history(uid: str, aid: int, cid: int):
res = await db.search("*", "history", f"UID={uid} and AID={aid}", "1", "update_time DESC")
if(res == ()):
await db.insert("history", "UID,AID,CID,update_time", f"{uid},{aid},{cid},NOW()")
else:
await db.update("history", f"CID={cid}, update_time=NOW()", f"UID={uid} and AID={aid}")
return Response(status_code=201)
@app.get("/tags")
async def get_tags(uid: str):
res = await db.search("tag", "tags", f"UID={uid}")
return res
@app.post("/tag")
async def update_tag(uid: str, tag: str):
if(await db.search("tag", "tags", f"UID={uid} AND tag='{tag}'")):
return Response(status_code=200)
else:
await db.insert("tags", "UID,tag", f"{uid},'{tag}'")
return Response(status_code=201)
@app.get("/comments")
async def get_comments(aid: str = None, uid: str = None, page: int = 1, isReverse: bool = False):
if(aid and uid):
return Response(status_code=400)
elif(aid):
res = await db.search("UID,AID,comment,comment_time", "comments", f"AID={aid}",
f"{(page-1)*20},{page*20}",
f"comment_time {'ASC' if isReverse else 'DESC'}")
elif(uid):
res = await db.search("UID,AID,comment,comment_time", "comments", f"UID={uid}",
f"{(page-1)*20},{page*20}",
f"comment_time {'ASC' if isReverse else 'DESC'}")
else:
return Response(status_code=400)
ret_list = [{
"uid": item[0],
"aid": item[1],
"comment": item[2],
"comment_time":item[3].strftime("%Y-%m-%d %H:%M:%S")
} for item in res]
return ret_list