上传文件至「/」
This commit is contained in:
BIN
Binary file not shown.
+20
@@ -0,0 +1,20 @@
|
||||
from sqlalchemy import *
|
||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||
|
||||
db_url = "mysql+pymysql://root:123456@127.0.0.1:3306/ai0824?charset=utf8mb4"
|
||||
engine = create_engine(db_url)
|
||||
|
||||
Base = declarative_base()
|
||||
Session = sessionmaker(bind=engine
|
||||
, autoflush=False
|
||||
, autocommit=False
|
||||
)
|
||||
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
from fastapi import FastAPI
|
||||
from day22.api.order_api import order_api
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(order_api,tags=['订单接口'],prefix='/orders')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run("main:app",host='127.0.0.1',port=12345)
|
||||
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
from fastapi import APIRouter,Depends,HTTPException
|
||||
from day22.schema.order_request import OrderRequest,OrderResponse
|
||||
from day22.database import get_db
|
||||
from day22.model.order_model import Order
|
||||
from day22.dao.order_dao import add_student_dao,update_orders_dao
|
||||
|
||||
order_api = APIRouter()
|
||||
|
||||
|
||||
@order_api.post('',response_model=OrderResponse)
|
||||
def add_orders(order:OrderRequest,db=Depends(get_db)):
|
||||
d = order.model_dump()
|
||||
r = add_student_dao( o=d,db=db )
|
||||
if not r:
|
||||
raise HTTPException(status_code=500,detail='服务器繁忙,请稍后添加!')
|
||||
return OrderResponse(totals=1,data=d)
|
||||
|
||||
@order_api.put('/{id}')
|
||||
def get_orders(order:OrderRequest,id:int,db=Depends(get_db)):
|
||||
d = order.model_dump(exclude_unset=True)
|
||||
r = update_orders_dao( id=id,update_data=d,db=db )
|
||||
if not r:
|
||||
raise HTTPException(status_code=500, detail='没有更新!')
|
||||
return {'code':200,'totals':r,'detail':'更新成功'}
|
||||
|
||||
@order_api.get('')
|
||||
def get_orders(order_title:str,db=Depends(get_db)):
|
||||
r = db.query(Order).filter( Order.title == order_title ).all()
|
||||
if r:
|
||||
return [{"order_id": i.id, 'order_title': i.title, "user_id": i.userid, "create_date": i.create_date,
|
||||
"update_date": i.update_date} for i in r]
|
||||
|
||||
raise HTTPException(status_code=404,detail='订单不存在!')
|
||||
|
||||
@order_api.delete('/{id}')
|
||||
def get_orders(id:int,db=Depends(get_db)):
|
||||
try: # 删除有返回值,删除的行数
|
||||
rows = db.query(Order).filter(Order.id == id).delete()
|
||||
except:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500,detail='删除异常,稍后操作!')
|
||||
else:
|
||||
db.commit()
|
||||
return {'code':200,'totals':rows,'detail':'删除成功'}
|
||||
|
||||
# 要求:
|
||||
'''
|
||||
①把剩下的delete、get查询接口完成dao层的分层
|
||||
②新增一个user_info的表,有外键联系,oder订单表外键依赖user_info,改造一下订单post的接口,新增订单前,判断下用户的id是否存在user_info表里
|
||||
'''
|
||||
Reference in New Issue
Block a user