From 87f980dd8550a7d4250bcc48c91cc7ee7552afd2 Mon Sep 17 00:00:00 2001 From: geeker Date: Mon, 21 Sep 2026 07:53:26 +0800 Subject: [PATCH 1/2] add .gitignore --- .gitignore | 16 ++++++++ .tool-versions | 1 + dao/order_dao.py | 17 +++++++++ main.py | 4 +- model/order.py | 46 ++++++++++++++--------- router/dbopt/{order.py => order_route.py} | 36 +++++++++++------- 6 files changed, 86 insertions(+), 34 deletions(-) create mode 100644 .tool-versions create mode 100644 dao/order_dao.py rename router/dbopt/{order.py => order_route.py} (56%) diff --git a/.gitignore b/.gitignore index ed8ebf5..234415f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,17 @@ +.venv/ +.idea +__pycache__/ +*.pyc +.pytest_cache/ +.mypy_cache/ +.ruff_cache/ +tests/ +.git/ +.github/ +*.md +!README.md +.env +.env.* +Dockerfile + __pycache__ \ No newline at end of file diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..bd92a07 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +python 3.13.4 diff --git a/dao/order_dao.py b/dao/order_dao.py new file mode 100644 index 0000000..f5abec9 --- /dev/null +++ b/dao/order_dao.py @@ -0,0 +1,17 @@ +''' +todo: 只定义dao method ? +''' + +from fastapi import Depends +from model.order import Order, get_session, OrderResponse, OrderSchema + +def get_order(id:int=None, user_id:int=None, title:str=None,session=None) -> OrderResponse: + q = session.query(Order) + if id: q = q.filter( Order.id == id ) + if user_id: q = q.filter( Order.userid == user_id ) + if title: q = q.where(Order.title.like('%'+title+'%')) + res = q.all() + # return {"data":OrderSchema(many=True).dump(q,many=True)} + # return {"data":[OrderSchema.model_validate(o) for o in r]} + return {"data":[{"order_id":r.id,'order_title':r.title,"user_id":r.userid,"create_date":r.create_date,"update_date":r.update_date} for r in res]} + diff --git a/main.py b/main.py index 36f8be3..47b8892 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ from fastapi.middleware.cors import CORSMiddleware from starlette.staticfiles import StaticFiles from router.features import form, method, file, validation, mimetype, path_query_variables, json -from router.dbopt import mysql,order +from router.dbopt import mysql,order_route from router.workhome import wk_0915,wk_0916,wk_0917 @@ -27,7 +27,7 @@ app.include_router(json.route) app.include_router(mysql.route) -app.include_router(order.route) +app.include_router(order_route.route) # homeworks... app.include_router(wk_0915.route) diff --git a/model/order.py b/model/order.py index 3861b18..50fbc01 100644 --- a/model/order.py +++ b/model/order.py @@ -1,8 +1,6 @@ from pydantic import BaseModel - from sqlalchemy.orm import declarative_base,sessionmaker from sqlalchemy import Column, Integer,String,DATETIME,create_engine - from datetime import datetime db_url = "mysql+pymysql://geeker:geeker@127.0.0.1:3306/fastapi_tutor?charset=utf8mb4" @@ -16,22 +14,6 @@ def get_session(): ) yield Session() -# 定义创建订单的请求体模型 -class OrderRequest(BaseModel): - id:int|None = None - title:str|None = None - userid:int|None =None - -class OrderResponse(BaseModel): - id:int|None = None - title:str|None = None - userid:int|None =None - create_date:datetime|None = datetime.now() - update_date:datetime|None = datetime.now() - status_code:str|None = 200 - detail:str|None = 'succeed!' - - Base = declarative_base() # 执行函数,返回一个基类 class Order(Base): # python里的表名字 __tablename__ = 'order_info_detail' # 实际数据库的表名字 @@ -53,4 +35,32 @@ class Order(Base): # python里的表名字 , default=datetime.now # 首次创建的时间跟更新时间一致 , onupdate=datetime.now ) + +# 定义创建订单的请求体模型 +class OrderRequest(BaseModel): + id:int|None = None + title:str|None = None + userid:int|None =None + +# todo 序列化问题: Order - Base 无法直接序列 +class OrderSchema(BaseModel): + id:int + title:str + userid:int + create_date:datetime + update_date:datetime + +class OrderResponse(BaseModel): + # id:int|None = None + # title:str|None = None + # userid:int|None =None + # create_date:datetime|None = datetime.now() + # update_date:datetime|None = datetime.now() + status_code:str = 200 + detail:str = 'succeed!' + total:int = 1 + # data:str|dict|list[OrderSchema] # 响应数据 + data:str|dict|list[object] # 响应数据 + + Order.__table__.create(engine,checkfirst = True) diff --git a/router/dbopt/order.py b/router/dbopt/order_route.py similarity index 56% rename from router/dbopt/order.py rename to router/dbopt/order_route.py index e821677..ebb1205 100644 --- a/router/dbopt/order.py +++ b/router/dbopt/order_route.py @@ -7,25 +7,34 @@ from pydantic import BaseModel from model.order import OrderRequest,Order,get_session,OrderResponse +from dao.order_dao import get_order + route = APIRouter(prefix="/order",tags=["orders"]) # 模糊查询 -@route.get('/',response_model=list[OrderResponse]) +@route.get('/',response_model=OrderResponse) +def orders(id:int=None, user_id:int=None, title:str=None,session=Depends(get_session)): + return get_order(id,user_id,title,session) + +# todo 请求响应模型序列化Order类问题 +@route.get('/no_response_model') def orders(id:int=None, user_id:int=None, title:str=None,session=Depends(get_session)): q = session.query(Order) - if id: q = q.filter( Order.id == id ) - if user_id: q = q.filter( Order.userid == userid ) - if title: q = q.where(Order.title.like('%'+title+'%')) - r = q.all() - session.close() - return r + if id: q = q.filter(Order.id == id) + if user_id: q = q.filter(Order.userid == user_id) + if title: q = q.where(Order.title.like('%' + title + '%')) + res = q.all() + return res + + @route.get('/{id}',response_model=OrderResponse) -def orders(id:int): - session = Session() - r = session.query(Order).filter( Order.id == id ).all() +def orders(id:int,session=Depends(get_session)): + r = session.query(Order).filter( Order.id == id ).first() session.close() - 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] + return {"data": + {"order_id":r.id,'order_title':r.title,"user_id":r.userid,"create_date":r.create_date,"update_date":r.update_date} + } @route.delete('/',response_model=OrderResponse) def orders(session=Depends(get_session)): @@ -44,8 +53,7 @@ def orders(order:OrderRequest,session=Depends(get_session)): @route.put('/{id}',response_model=OrderResponse) def orders(order:OrderRequest,id:int,session=Depends(get_session)): - session.query( Order ).filter( Order.id == id ).update( order.model_dump(exclude_unset=True) ) + r = session.query( Order ).filter( Order.id == id ).update( order.model_dump(exclude_unset=True) ) session.commit() - res = session.query(Order).filter(Order.id == id).first() session.close() - return res + return OrderResponse(status_code='200',detail='ok',total=r) From 678449752929d6f34fc5456d1e9723a2d3554843 Mon Sep 17 00:00:00 2001 From: geeker Date: Mon, 21 Sep 2026 07:55:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4.idea=20,=20log=E6=97=A0?= =?UTF-8?q?=E5=85=B3=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 5 - .idea/fastapi-tutor.iml | 14 --- .../inspectionProfiles/profiles_settings.xml | 6 -- .idea/misc.xml | 4 - .idea/modules.xml | 8 -- logplus.log | 96 +++++++++++++++++++ 6 files changed, 96 insertions(+), 37 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/fastapi-tutor.iml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index b58b603..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/fastapi-tutor.iml b/.idea/fastapi-tutor.iml deleted file mode 100644 index 8bca85d..0000000 --- a/.idea/fastapi-tutor.iml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index cea07b2..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 06a348b..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/logplus.log b/logplus.log index 4ad19d1..ec5942a 100644 --- a/logplus.log +++ b/logplus.log @@ -386,3 +386,99 @@ 2026-09-19 10:13:19 - ai-model - INFO - ai-model - 1 2026-09-19 10:13:20 - ai-model - INFO - ai-model - 0 2026-09-19 10:13:20 - ai-model - INFO - ai-model - 1 +2026-09-19 10:58:52 - ai-model - INFO - ai-model - 0 +2026-09-19 10:58:52 - ai-model - INFO - ai-model - 1 +2026-09-19 10:58:55 - ai-model - INFO - ai-model - 0 +2026-09-19 10:58:55 - ai-model - INFO - ai-model - 1 +2026-09-19 10:59:02 - ai-model - INFO - ai-model - 0 +2026-09-19 10:59:02 - ai-model - INFO - ai-model - 1 +2026-09-19 11:29:54 - ai-model - INFO - ai-model - 0 +2026-09-19 11:29:54 - ai-model - INFO - ai-model - 1 +2026-09-19 11:36:12 - ai-model - INFO - ai-model - 0 +2026-09-19 11:36:12 - ai-model - INFO - ai-model - 1 +2026-09-19 11:36:28 - ai-model - INFO - ai-model - 0 +2026-09-19 11:36:28 - ai-model - INFO - ai-model - 1 +2026-09-19 11:37:23 - ai-model - INFO - ai-model - 0 +2026-09-19 11:37:23 - ai-model - INFO - ai-model - 1 +2026-09-19 11:38:07 - ai-model - INFO - ai-model - 0 +2026-09-19 11:38:07 - ai-model - INFO - ai-model - 1 +2026-09-19 11:38:08 - ai-model - INFO - ai-model - 0 +2026-09-19 11:38:08 - ai-model - INFO - ai-model - 1 +2026-09-19 11:38:54 - ai-model - INFO - ai-model - 0 +2026-09-19 11:38:54 - ai-model - INFO - ai-model - 1 +2026-09-19 11:40:14 - ai-model - INFO - ai-model - 0 +2026-09-19 11:40:14 - ai-model - INFO - ai-model - 1 +2026-09-19 11:40:15 - ai-model - INFO - ai-model - 0 +2026-09-19 11:40:15 - ai-model - INFO - ai-model - 1 +2026-09-19 11:40:38 - ai-model - INFO - ai-model - 0 +2026-09-19 11:40:38 - ai-model - INFO - ai-model - 1 +2026-09-19 11:40:46 - ai-model - INFO - ai-model - 0 +2026-09-19 11:40:46 - ai-model - INFO - ai-model - 1 +2026-09-19 11:40:47 - ai-model - INFO - ai-model - 0 +2026-09-19 11:40:47 - ai-model - INFO - ai-model - 1 +2026-09-19 11:41:20 - ai-model - INFO - ai-model - 0 +2026-09-19 11:41:20 - ai-model - INFO - ai-model - 1 +2026-09-19 11:41:21 - ai-model - INFO - ai-model - 0 +2026-09-19 11:41:21 - ai-model - INFO - ai-model - 1 +2026-09-19 11:41:53 - ai-model - INFO - ai-model - 0 +2026-09-19 11:41:53 - ai-model - INFO - ai-model - 1 +2026-09-19 11:41:54 - ai-model - INFO - ai-model - 0 +2026-09-19 11:41:54 - ai-model - INFO - ai-model - 1 +2026-09-19 11:42:03 - ai-model - INFO - ai-model - 0 +2026-09-19 11:42:03 - ai-model - INFO - ai-model - 1 +2026-09-19 11:42:04 - ai-model - INFO - ai-model - 0 +2026-09-19 11:42:04 - ai-model - INFO - ai-model - 1 +2026-09-19 11:42:36 - ai-model - INFO - ai-model - 0 +2026-09-19 11:42:36 - ai-model - INFO - ai-model - 1 +2026-09-19 11:42:37 - ai-model - INFO - ai-model - 0 +2026-09-19 11:42:37 - ai-model - INFO - ai-model - 1 +2026-09-19 11:44:13 - ai-model - INFO - ai-model - 0 +2026-09-19 11:44:13 - ai-model - INFO - ai-model - 1 +2026-09-19 11:44:14 - ai-model - INFO - ai-model - 0 +2026-09-19 11:44:14 - ai-model - INFO - ai-model - 1 +2026-09-19 11:44:18 - ai-model - INFO - ai-model - 0 +2026-09-19 11:44:18 - ai-model - INFO - ai-model - 1 +2026-09-19 11:44:19 - ai-model - INFO - ai-model - 0 +2026-09-19 11:44:19 - ai-model - INFO - ai-model - 1 +2026-09-19 11:44:29 - ai-model - INFO - ai-model - 0 +2026-09-19 11:44:29 - ai-model - INFO - ai-model - 1 +2026-09-19 11:44:29 - ai-model - INFO - ai-model - 0 +2026-09-19 11:44:29 - ai-model - INFO - ai-model - 1 +2026-09-19 11:54:02 - ai-model - INFO - ai-model - 0 +2026-09-19 11:54:02 - ai-model - INFO - ai-model - 1 +2026-09-19 11:54:03 - ai-model - INFO - ai-model - 0 +2026-09-19 11:54:03 - ai-model - INFO - ai-model - 1 +2026-09-19 11:55:11 - ai-model - INFO - ai-model - 0 +2026-09-19 11:55:11 - ai-model - INFO - ai-model - 1 +2026-09-19 11:56:00 - ai-model - INFO - ai-model - 0 +2026-09-19 11:56:00 - ai-model - INFO - ai-model - 1 +2026-09-19 12:00:41 - ai-model - INFO - ai-model - 0 +2026-09-19 12:00:41 - ai-model - INFO - ai-model - 1 +2026-09-19 12:01:02 - ai-model - INFO - ai-model - 0 +2026-09-19 12:01:02 - ai-model - INFO - ai-model - 1 +2026-09-19 12:01:03 - ai-model - INFO - ai-model - 0 +2026-09-19 12:01:03 - ai-model - INFO - ai-model - 1 +2026-09-19 12:01:16 - ai-model - INFO - ai-model - 0 +2026-09-19 12:01:16 - ai-model - INFO - ai-model - 1 +2026-09-19 12:01:17 - ai-model - INFO - ai-model - 0 +2026-09-19 12:01:17 - ai-model - INFO - ai-model - 1 +2026-09-19 12:13:12 - ai-model - INFO - ai-model - 0 +2026-09-19 12:13:12 - ai-model - INFO - ai-model - 1 +2026-09-19 12:13:13 - ai-model - INFO - ai-model - 0 +2026-09-19 12:13:13 - ai-model - INFO - ai-model - 1 +2026-09-19 12:14:05 - ai-model - INFO - ai-model - 0 +2026-09-19 12:14:05 - ai-model - INFO - ai-model - 1 +2026-09-19 12:14:06 - ai-model - INFO - ai-model - 0 +2026-09-19 12:14:06 - ai-model - INFO - ai-model - 1 +2026-09-19 12:15:51 - ai-model - INFO - ai-model - 0 +2026-09-19 12:15:51 - ai-model - INFO - ai-model - 1 +2026-09-19 12:25:50 - ai-model - INFO - ai-model - 0 +2026-09-19 12:25:50 - ai-model - INFO - ai-model - 1 +2026-09-19 12:26:05 - ai-model - INFO - ai-model - 0 +2026-09-19 12:26:05 - ai-model - INFO - ai-model - 1 +2026-09-19 12:26:06 - ai-model - INFO - ai-model - 0 +2026-09-19 12:26:06 - ai-model - INFO - ai-model - 1 +2026-09-21 07:47:51 - ai-model - INFO - ai-model - 0 +2026-09-21 07:47:51 - ai-model - INFO - ai-model - 1 +2026-09-21 07:48:51 - ai-model - INFO - ai-model - 0 +2026-09-21 07:48:51 - ai-model - INFO - ai-model - 1