From 9a0fdbb7c17e255d466819be0b954f89d168c552 Mon Sep 17 00:00:00 2001 From: cflsxjw Date: Mon, 7 Oct 2024 21:57:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E5=90=8E=E7=AB=AF=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=AD=A6=E7=94=9F=E4=BF=A1=E6=81=AF=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dataSources.xml | 12 ------------ main.py | 31 +++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 20 deletions(-) delete mode 100644 .idea/dataSources.xml 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