From 4272acb67788cf9f8e58fe8da614135670b6a4ff Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 16 Dec 2022 16:50:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9A=84curd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 0 app/__init__.py | 0 app/__pycache__/crud.cpython-310.pyc | Bin 0 -> 2039 bytes app/__pycache__/database.cpython-310.pyc | Bin 0 -> 503 bytes app/__pycache__/main.cpython-310.pyc | Bin 843 -> 3696 bytes app/__pycache__/models.cpython-310.pyc | Bin 0 -> 1063 bytes app/__pycache__/schemas.cpython-310.pyc | Bin 0 -> 1517 bytes app/crud.py | 56 +++++++++++++++ app/database.py | 13 ++++ app/main.py | 84 ++++++++++++++++++++++- app/models.py | 26 +++++++ app/schemas.py | 35 ++++++++++ app/sql_app.db | Bin 0 -> 32768 bytes 13 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 __init__.py create mode 100644 app/__init__.py create mode 100644 app/__pycache__/crud.cpython-310.pyc create mode 100644 app/__pycache__/database.cpython-310.pyc create mode 100644 app/__pycache__/models.cpython-310.pyc create mode 100644 app/__pycache__/schemas.cpython-310.pyc create mode 100644 app/crud.py create mode 100644 app/database.py create mode 100644 app/models.py create mode 100644 app/schemas.py create mode 100644 app/sql_app.db diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/__pycache__/crud.cpython-310.pyc b/app/__pycache__/crud.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a20b8624d9dc375135e7c11e9e44302fb21974e1 GIT binary patch literal 2039 zcmaJ?&2QT_6emSW@>dea&6>1z(|rQf*2N9BV=)X!QJ@!Jf?aeG6p^0g$d*gW3p_Zt zUc0|whXm*)|IN<4?$qmY+i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RXtL5tXi_@3y+mq$HKADj= literal 0 HcmV?d00001 diff --git a/app/__pycache__/main.cpython-310.pyc b/app/__pycache__/main.cpython-310.pyc index 3f3631e20bdc617d909d57c500a4e08a4b66e5e2..afb3bcfe213d8ac9a293afd8dc435ac3c5bfb7d5 100644 GIT binary patch literal 3696 zcmZ`*&2JRR6|buP^vu{EgE8iF&GI=cb_Xz!U7-yuAInPF5ecv;b;zh^s>W{9Jv~(S z1UxYk#3)LnSlLKXE=wX%q}}DVQVvnh`3G{YYqBJJ=Ei~Ky_y*_#?FlTRn@C{Rqyjx zm5z=U2>j;eo;3aey?^3h_~*mm0({wPN{AqW#w4MfCXBPh;#OjFnSqe5@}&&c}^>nU_sF!6%^g;%&)xzCD@bla$Ev z(vGF6rJZt8_-ho#bd~UFF(L|&3E%ZG5k*mYOhieRRxQ38#-m~k#$$4i^jB@Z7dYdh z44krAu}@Av-7hAdQEACttF%eQHo04Dmt##^Oo|9u_T(eV5n>91A=78As zjEdc2&tn2o@5^hvH+Ad*)9S$92f7I6OI@gPQa>Jxeui0L|~+k znv=f=wlTrP$u;!ADcE68e+MQ`i+QudF~M$-%GvKQGb*Z2h~#9IJRse|3i*ssa+|D>r}P>01i7)^ zzek?aYK19GccR}VgQQwA2;Fj#6OBwa)6jR9CpfQYz1>XhuvMC^va4HCAw+Q5hE$Dfu zAn>rIZB#*f9iX*HR4b15YqeIG$XZPoYPBR4of!M2TJ3HpjQcZImMcA8tA(vrnuqB6 ztX4Bd4Uq{rS8WHf3$?I;6iA60&L(Bi#TRhdG?0O^f^thRZVL-a-3E0XJz@e`1^u)a z#`1nk=Gs*_M^@$v!0L`ypb~+zmWjkzt&DVpAJDtB|`% z)pfsKh1r3s!+zzCbR{T`RRO-X&J*cDYopJ(4|Cr7oYRkktL;y6nP?mRTh&D&vxbUH zPIIRX=^?e1hxaQZZ<~hhQs`ebgTw=(oo1YdxjKN1t!&*76F6zIs6c!=3ARpCh5T+Q zfI^YEtYJoI8>t$Hi3S5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>-aIncLz}59FzSx>z1PcDeEkX83_EeI z3kFHxE^6hQ6d43h{Q&l;uxht(3s7_RA}$^%^YIqS6xLmk37p0Ns%vHsg~nAY74G$4 zcM1PYw8_;wPr>Ct#7#O04&opOf`~dCf`b?ri>b#+dnri|#7Q}4mrgFC zqqBd8qgz%dH)khbi^662lK1iBy+^*6-@`@I=|lw2>Gk8q6D8!cI%7S3h(;3)8OcEb z)Nh(mMVp%`mco=Mp7Q5Fvlq}@2lr$ODr`)MM*JI1k4Y@Q>Jc$`v6r}V=jdSY1M5{1 z^)_8S{0=l|Is#0xffsq|Trx~==JwMpGpi@YX8M)b0F!^HT5My9TVqOz(s2a6mDGy2 z2e}KttB=-NU-81Y((@~4#y-%Kyl}RINgot;G%jOm+jRwYrgMnK3PiWsidN2Jp~NL_ zoD?IAe?#nijV{8oq$oTarezbKY0<1%_wjN&H+qt#eZa~sAlgKNgpf)Wfeh#27cS0J AeELt9jKn~Yi#fo2G#k~%cE*)st`FJt zqkU;%NsHZVF_Jy(_7ws!!4(sM;v$3yVin*OUWQ_TEb%NtmJ-`=k>P!aPvgr#j9^US zRf_dQWhi{;qBj>~5< zW4@qm+65TBTs#AN*4EA9owTiN^aY$OWYa9>3zP?ioi}Tf;hVIUog$DK357pROmI^pHur47CK?;m+tMy6E(K literal 0 HcmV?d00001 diff --git a/app/__pycache__/schemas.cpython-310.pyc b/app/__pycache__/schemas.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6049bae28431fdfdfdcf4d341ce0e71bd5439d8e GIT binary patch literal 1517 zcmZ`(O>fgc5Z$%cj^iY4sLDtAQpEwu64ZYnLRE`|IFJkUl8fbLx5dJ-&8|yTuH|3w z3;J(3@0Anh-Z=5bPD+)emUis7vpYL)=B>-YAY^!6@BW|K_KrzMx<8n76~A;R4zQ3Ou%{eg7uc5_;7)@Da3H(D-DbT99LgSWPkC7H1NY?s zc+lXE-1x|%%|AGmMcj4|QlmbUQsq;cx&62R`xxdWjA9cm*~B?w%2h)05we_k%9YLm z19k}`uCT%TTFoe^_3Et7)!6o=GE<#ZbygOUZ-XQ$(wRyU8z#xDlxI18_mkx7S(?v1 zuBo-%Op>%H$~vWuCP}o1Jg%7T;nkSGaeQQyHnGy#G=8hhm%6Otlho8{m7S~OIIXI9 zFE-Q9YL=S4>OvDGD!lPwLKg6C-ps9VN5@+3eS9pqOAdyP4T<5QR%xxqk*g_`?vM${ z++^2&(SALE>0%fzFfrg4pv_N`D%C~}&vQ8## zP@We`Cz)J{zaVuzycJ*a37Uu)?)M(W*gGtXlkD`<_)08MLu5Upj;VDdt>13EhuGIG z&w%S~pmF3cM{={G)^tCb1?$4;4S1<;hN?=f3iy`O%&zcS)b08Kmaf*VWoHA@N-Ipr zAP(=q=2yO_`hZ>(aNusQpj9 UA^wJv>9e6b^oC*>48^wi2M!npk^lez literal 0 HcmV?d00001 diff --git a/app/crud.py b/app/crud.py new file mode 100644 index 0000000..dd5b280 --- /dev/null +++ b/app/crud.py @@ -0,0 +1,56 @@ +from sqlalchemy.orm import Session +from sqlalchemy import select + + +import models, schemas + + +# 删除 +def delete_user(db: Session, user_id: int): + db.query(models.User).where(models.User.id == user_id).delete() + db.commit() + # db.refresh(db_user) + + +# 更新 +def update_user(db: Session, user_id: int): + db_test=db.query(models.User).filter(models.User.id == user_id).update({"email":'111'},synchronize_session=False) + db.commit() + # db_test=db.query(models.User).filter(models.User.id == user_id).first() + # db_test.email='12345@qq.com' + # db.commit() + + +# 查询 +def get_user(db: Session, user_id: int): + return db.query(models.User).filter(models.User.id == user_id).first() + + +def get_user_by_email(db: Session, email: str): + return db.query(models.User).filter(models.User.email == email).first() + + +def get_users(db: Session, skip: int = 0, limit: int = 100): + return db.query(models.User).offset(skip).limit(limit).all() + +# 新增 +def create_user(db: Session, user: schemas.UserCreate): + + fake_hashed_password = user.password + "notreallyhashed" + db_user = models.User(email=user.email, hashed_password=fake_hashed_password,is_active=False) + db.add(db_user) + db.commit() + db.refresh(db_user) + return db_user + + +def get_items(db: Session, skip: int = 0, limit: int = 100): + return db.query(models.Item).offset(skip).limit(limit).all() + + +def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int): + db_item = models.Item(**item.dict(), owner_id=user_id) + db.add(db_item) + db.commit() + db.refresh(db_item) + return db_item diff --git a/app/database.py b/app/database.py new file mode 100644 index 0000000..45a8b9f --- /dev/null +++ b/app/database.py @@ -0,0 +1,13 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db" +# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db" + +engine = create_engine( + SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} +) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +Base = declarative_base() diff --git a/app/main.py b/app/main.py index 23ecc7b..c0d002c 100644 --- a/app/main.py +++ b/app/main.py @@ -1,17 +1,67 @@ -from fastapi import FastAPI +from fastapi import FastAPI,Header,Depends,HTTPException import requests import json +from enum import Enum +from typing import Union +from pydantic import BaseModel + + +from sqlalchemy.orm import Session +import crud, models, schemas +from database import SessionLocal, engine +models.Base.metadata.create_all(bind=engine) + app = FastAPI() +# Dependency +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + +class User(BaseModel): + user_id: str + +class ModelName(str, Enum): + alexnet = "alexnet" + resnet = "resnet" + lenet = "lenet" + +class Item(BaseModel): + name: str + description: Union[str, None] = None + price: float + tax: Union[float, None] = None + +@app.post("/delete_user") +async def delete(user:User,db: Session = Depends(get_db)): + db_user = crud.delete_user(db,user_id=user.user_id) + # print('db_user'+db_user) + return {"message": "Hello delete_user"} + + +@app.post("/update_user") +async def update(user:User,db: Session = Depends(get_db)): + db_user = crud.update_user(db,user_id=user.user_id) + return {"message": "Hellupdate_usero World"} + + @app.get("/") async def root(): return {"message": "Hello World"} +@app.post("/users/", response_model=schemas.User) +def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): + db_user = crud.get_user_by_email(db, email=user.email) + if db_user: + raise HTTPException(status_code=400, detail="Email already registered") + return crud.create_user(db=db, user=user) + # 飞书通知 - - @app.get("/feishu") async def root(): headers = {'Content-Type': 'application/json'} @@ -22,3 +72,31 @@ async def root(): except(e): print('e') return {"message": "触发飞书webhook"} + +# post请求 +@app.post("/testPost") +async def root(): + return {"message": "test-post"} + +# 路径参数 +@app.get("/path/{pathName}") +async def root(pathName:ModelName): + return {"message": pathName} + +# 查询参数 +@app.get("/items/") +async def read_item(skip: int = 0, limit: Union[int, None] = None): + fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}] + # return fake_items_db[skip : skip + limit] + return f'{skip}+{limit}' + +# 请求体 +@app.post("/postitems/") +async def create_item(item: Item): + return item + + +# header参数 +@app.get("/items/header") +async def read_items(user_agent: Union[str, None] = Header(default=None)): + return {"User-Agent": user_agent} \ No newline at end of file diff --git a/app/models.py b/app/models.py new file mode 100644 index 0000000..8da01c2 --- /dev/null +++ b/app/models.py @@ -0,0 +1,26 @@ +from sqlalchemy import Boolean, Column, ForeignKey, Integer, String +from sqlalchemy.orm import relationship + +from database import Base + + +class User(Base): + __tablename__ = "users" + + id = Column(Integer, primary_key=True, index=True) + email = Column(String, unique=True, index=True) + hashed_password = Column(String) + is_active = Column(Boolean, default=True) + + items = relationship("Item", back_populates="owner") + + +class Item(Base): + __tablename__ = "items" + + id = Column(Integer, primary_key=True, index=True) + title = Column(String, index=True) + description = Column(String, index=True) + owner_id = Column(Integer, ForeignKey("users.id")) + + owner = relationship("User", back_populates="items") diff --git a/app/schemas.py b/app/schemas.py new file mode 100644 index 0000000..aea2e3f --- /dev/null +++ b/app/schemas.py @@ -0,0 +1,35 @@ +from pydantic import BaseModel + + +class ItemBase(BaseModel): + title: str + description: str | None = None + + +class ItemCreate(ItemBase): + pass + + +class Item(ItemBase): + id: int + owner_id: int + + class Config: + orm_mode = True + + +class UserBase(BaseModel): + email: str + + +class UserCreate(UserBase): + password: str + + +class User(UserBase): + id: int + is_active: bool + items: list[Item] = [] + + class Config: + orm_mode = True diff --git a/app/sql_app.db b/app/sql_app.db new file mode 100644 index 0000000000000000000000000000000000000000..a10c99d120d2ebf3e74896804e247c0efe5ce96a GIT binary patch literal 32768 zcmeI(Pfyce90%~{q3uSSj7KG*;^{HQWKD!*jKwP(7M#pZ%1}9&rLqD|_6OD~c#s5N zgm1#cSK-;0z=;zF;NX8AT*`}Egee@~xJcX^&{Tbq?2^7&q;+wvlw zCwB;?WQB7=2os-a@fmDs(NG5`;z$$i+nS6lzIl-mSJRmXgzd3=Ge2jH%(u+GIK>75 z2tWV=5P$##AOHafK;S(h^k@QR}RzrcKkNU%beB-yfr_sP!w>ilvG@7?z=n$16KEd);=qQ>}4lyHa_= z4PB0xzb?9~&x>vxNc52mcaGbBw|-qDPJQK;Ul!dRzG3h1tQc_|TdTTudEJR)*}**) zckMOXwVhRai+>3HZYa|YtE3Dw%WI+&c~nPb23KQ{>niS)eDC3axtHa)yr3zYO25pu zQ{LQ8y#7R9qM*FbIx}g{HY06is@zQt*~$>l-fAn0B@qw2@W5}>54|uv?sOZ;OF>xo zcB9~<&r8*6#V$HG0UK6^eJ3hACG4F2U|-p(Xkvo^1Rwwb2tWV=5P$##AOHafKwzu_ zbDBoj!gL|u?nGVRYc@aiX7qEaj4zMIMdx#DLC?$hLq#Kx7xdiHebJZ|ja+UtpHq#@ zKh?vi8?^U(`;ndub}8on=Y;)Y7h(m-}j3nWbWxK+%=?{-S^(Cj2V9AnXtO%`U|n zz{R*93iTiW0SG_<0uX=z1Rwwb2tWV=5cn4YnnI~2Rskqg)p)NcqSw^al%nW*{NH@> zr+;h^fB*y_009U<00Izz00bZa0SHX6fDw-k^ZyBc!x$O_AOHafKmY;|fB*y_009U< zfDp|8@hyM=1Rwwb2tWV=5P$##AOHafOuhi-|C9fYF+vDH00Izz00bZa0SG_<0uX?} EUpO5y*8l(j literal 0 HcmV?d00001