diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml deleted file mode 100644 index 3fb6487..0000000 --- a/.idea/dataSources.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - mariadb - true - org.mariadb.jdbc.Driver - jdbc:mariadb://localhost:3306/BOOK_DEMO - $ProjectFileDir$ - - - \ No newline at end of file diff --git a/main.py b/main.py index 0b8a1be..3317488 100644 --- a/main.py +++ b/main.py @@ -1,36 +1,51 @@ from math import floor from random import uniform -from fastapi import FastAPI +from fastapi import FastAPI, UploadFile from fastapi.middleware.cors import CORSMiddleware import sqlalchemy from sqlalchemy import text +import pandas as pd app = FastAPI() origins = ["http://localhost:5173"] + # noinspection PyTypeChecker app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], - allow_headers=["*"], + allow_headers=["*"] ) -engine = sqlalchemy.create_engine("mariadb+mariadbconnector://cflsxjw:1212@127.0.0.1:3306/BOOK_DEMO") +engine = sqlalchemy.create_engine("mariadb+mariadbconnector://cflsxjw:1212@127.0.0.1:3306/ROLLER_DB") + + @app.get("/") async def root(): return {"message": "Hello World"} -@app.get("/hello/{name}") -async def say_hello(name: str): - return {"message": f"Hello {name}"} - -@app.get("/get_random_stu") +@app.get("/api/get/get_random_stu") async def get_random_stu(): with engine.connect() as c: stu = c.execute(text("SELECT sno, sname FROM stu_info")).fetchall() size = stu.__len__() return {"sno": stu[floor(uniform(0, size - 1))][0], "sname": stu[floor(uniform(0, size - 1))][1]} + +@app.get("/api/get/get_random_event") +async def get_event(): + return {"eventId": 10, "eventDescription": "疯狂星期四!/v我50:P"} + +@app.post("/api/post/upload_stu_info") +async def upload_stu_info(file: UploadFile): + upload_file = file.file + data = pd.read_csv(upload_file) + with engine.connect() as c: + c.execute(text("TRUNCATE TABLE stu_info")) + for i in data.index.values: + with engine.begin() as c: + c.execute(text(f"INSERT INTO stu_info VALUES ('{data.values[i, 0]}', '{data.values[i,1]}', 0);")) + return file.filename \ No newline at end of file