feat: fastapi
This commit is contained in:
0
__init__.py
Normal file
0
__init__.py
Normal file
BIN
__pycache__/crud.cpython-312.pyc
Normal file
BIN
__pycache__/crud.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/database.cpython-312.pyc
Normal file
BIN
__pycache__/database.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/main.cpython-312.pyc
Normal file
BIN
__pycache__/main.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/models.cpython-312.pyc
Normal file
BIN
__pycache__/models.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/schemas.cpython-312.pyc
Normal file
BIN
__pycache__/schemas.cpython-312.pyc
Normal file
Binary file not shown.
41
crud.py
Normal file
41
crud.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# from . import models, schemas
|
||||
import models, schemas
|
||||
|
||||
|
||||
# 通过id获取用户
|
||||
def get_user(db: Session, user_id: int):
|
||||
return db.query(models.User).filter(models.User.id == user_id).first()
|
||||
|
||||
|
||||
# 通过openId岔村用户
|
||||
def get_user_by_openId(db: Session, openId: str):
|
||||
return db.query(models.User).filter(models.User.openId == openId).first()
|
||||
|
||||
|
||||
# 分页查询-用户all
|
||||
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
||||
return db.query(models.User).offset(skip).limit(limit).all()
|
||||
|
||||
|
||||
# 创建用户
|
||||
# user:schemas.UserCreate
|
||||
def create_user(db: Session, user:schemas.UserCreate):
|
||||
db_user = models.User(
|
||||
wxid=user.wxid,
|
||||
openId=user.openId,
|
||||
sex=user.sex,
|
||||
country=user.country,
|
||||
province=user.province,
|
||||
sourceStr=user.sourceStr,
|
||||
usertype=user.usertype,
|
||||
nickname=user.nickname,
|
||||
wxaccount=user.wxaccount,
|
||||
remark=user.remark,
|
||||
smallhead=user.smallhead,
|
||||
)
|
||||
db.add(db_user)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
return db_user
|
||||
13
database.py
Normal file
13
database.py
Normal file
@@ -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:///./kunpeng.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()
|
||||
BIN
kunpeng.db
Normal file
BIN
kunpeng.db
Normal file
Binary file not shown.
53
main.py
Normal file
53
main.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# from . import crud, models, schemas
|
||||
import crud, models, schemas
|
||||
from database import SessionLocal, engine
|
||||
# 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()
|
||||
|
||||
|
||||
# 默认首页
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello World"}
|
||||
|
||||
|
||||
# 创建用户
|
||||
@app.post("/users/")
|
||||
# schemas.UserCreate决定了swagger文档
|
||||
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
|
||||
db_user = crud.create_user(db, user)
|
||||
if db_user:
|
||||
raise HTTPException(status_code=400,
|
||||
detail="openId already registered")
|
||||
return crud.create_user(db=db, user=user)
|
||||
|
||||
|
||||
# 分页查询-用户
|
||||
@app.get("/users/")
|
||||
def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
||||
users = crud.get_users(db, skip=skip, limit=limit)
|
||||
return users
|
||||
|
||||
|
||||
# 根据openId查询用户
|
||||
@app.get("/users/{openId}")
|
||||
def read_user(openId, db: Session = Depends(get_db)):
|
||||
db_user = crud.get_user_by_openId(db, openId=openId)
|
||||
if db_user is None:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
return db_user
|
||||
22
models.py
Normal file
22
models.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base
|
||||
# from .database import Base
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
wxid = Column(String)
|
||||
openId = Column(String)
|
||||
sex = Column(String) #性别
|
||||
country = Column(String) #"CN"
|
||||
province = Column(String) #"Hubei"
|
||||
sourceStr = Column(String) #:"通过朋友验证消息添加"
|
||||
usertype = Column(String)
|
||||
nickname = Column(String) #昵称
|
||||
wxaccount = Column(String) #微信号
|
||||
remark = Column(String) #备注
|
||||
smallhead = Column(String) #头像
|
||||
|
||||
28
schemas.py
Normal file
28
schemas.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
|
||||
|
||||
# 用户信息
|
||||
class UserCreate(BaseModel):
|
||||
password: str
|
||||
wxid: str
|
||||
openId: str
|
||||
sex: str
|
||||
country: str
|
||||
province: str
|
||||
sourceStr: str
|
||||
usertype: str
|
||||
nickname: str
|
||||
wxaccount: str
|
||||
remark: str
|
||||
smallhead: str
|
||||
|
||||
|
||||
|
||||
class User(UserCreate):
|
||||
id: int
|
||||
is_active: bool
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
Reference in New Issue
Block a user