'dev'
This commit is contained in:
BIN
app/__pycache__/crud.cpython-310.pyc
Normal file
BIN
app/__pycache__/crud.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/__pycache__/database.cpython-310.pyc
Normal file
BIN
app/__pycache__/database.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/__pycache__/main.cpython-310.pyc
Normal file
BIN
app/__pycache__/main.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/__pycache__/models.cpython-310.pyc
Normal file
BIN
app/__pycache__/models.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/__pycache__/schemas.cpython-310.pyc
Normal file
BIN
app/__pycache__/schemas.cpython-310.pyc
Normal file
Binary file not shown.
37
app/crud.py
Normal file
37
app/crud.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app import models, schemas
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
def get_user(db: Session, user_id: int):
|
||||||
|
return db.query(models.User).filter(models.User.id == user_id).first()
|
||||||
|
|
||||||
|
def get_user_by_openid_or_wxid(db: Session, identifier: str):
|
||||||
|
return db.query(models.User).filter((models.User.openid == identifier) | (models.User.wxid == identifier)).first()
|
||||||
|
|
||||||
|
def get_users(db: Session, skip: int = 0, limit: int = 10):
|
||||||
|
return db.query(models.User).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def create_user(db: Session, user: schemas.UserCreate):
|
||||||
|
db_user = models.User(**user.dict())
|
||||||
|
try:
|
||||||
|
db.add(db_user)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_user)
|
||||||
|
except IntegrityError:
|
||||||
|
db.rollback()
|
||||||
|
# wxid的唯一性
|
||||||
|
raise HTTPException(status_code=400, detail="wxid must be unique")
|
||||||
|
return db_user
|
||||||
|
|
||||||
|
def delete_user(db: Session, user: models.User):
|
||||||
|
db.delete(user)
|
||||||
|
db.commit()
|
||||||
|
return user
|
||||||
|
|
||||||
|
def update_user(db: Session, user: models.User, user_update: schemas.UserUpdate):
|
||||||
|
for key, value in user_update.dict(exclude_unset=True).items():
|
||||||
|
setattr(user, key, value)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(user)
|
||||||
|
return user
|
||||||
11
app/database.py
Normal file
11
app/database.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
|
||||||
|
|
||||||
|
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
61
app/main.py
Normal file
61
app/main.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
from fastapi import FastAPI, HTTPException, Depends
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from app import models, schemas, crud
|
||||||
|
from app.database import SessionLocal, engine
|
||||||
|
|
||||||
|
# 初始化数据库
|
||||||
|
models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
# 依赖注入
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
@app.get("/add", response_model=schemas.User)
|
||||||
|
def add_user(
|
||||||
|
nickname: Optional[str] = None,
|
||||||
|
remark: Optional[str] = None,
|
||||||
|
wechat_number: Optional[str] = None,
|
||||||
|
wxid: Optional[str] = None,
|
||||||
|
gender: Optional[str] = None,
|
||||||
|
friend_add_time: Optional[str] = None,
|
||||||
|
openid: Optional[str] = None,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
return crud.create_user(db=db, user=schemas.UserCreate(
|
||||||
|
nickname=nickname, remark=remark, wechat_number=wechat_number,
|
||||||
|
wxid=wxid, gender=gender, friend_add_time=friend_add_time,
|
||||||
|
openid=openid))
|
||||||
|
|
||||||
|
@app.get("/users/", response_model=List[schemas.User])
|
||||||
|
def read_users(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
|
||||||
|
return crud.get_users(db=db, skip=skip, limit=limit)
|
||||||
|
|
||||||
|
@app.get("/user/{identifier}", response_model=schemas.User)
|
||||||
|
def read_user(identifier: str, db: Session = Depends(get_db)):
|
||||||
|
user = crud.get_user_by_openid_or_wxid(db, identifier=identifier)
|
||||||
|
if user is None:
|
||||||
|
raise HTTPException(status_code=404, detail="User not found")
|
||||||
|
return user
|
||||||
|
|
||||||
|
@app.delete("/user/{user_id}", response_model=schemas.User)
|
||||||
|
def delete_user(user_id: int, db: Session = Depends(get_db)):
|
||||||
|
user = crud.get_user(db=db, user_id=user_id)
|
||||||
|
if user is None:
|
||||||
|
raise HTTPException(status_code=404, detail="User not found")
|
||||||
|
return crud.delete_user(db=db, user=user)
|
||||||
|
|
||||||
|
@app.put("/user/{identifier}", response_model=schemas.User)
|
||||||
|
def update_user(identifier: str, user_update: schemas.UserUpdate, db: Session = Depends(get_db)):
|
||||||
|
user = crud.get_user_by_openid_or_wxid(db, identifier=identifier)
|
||||||
|
if user is None:
|
||||||
|
raise HTTPException(status_code=404, detail="User not found")
|
||||||
|
|
||||||
|
return crud.update_user(db=db, user=user, user_update=user_update)
|
||||||
14
app/models.py
Normal file
14
app/models.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String
|
||||||
|
from app.database import Base
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
nickname = Column(String, index=True)
|
||||||
|
remark = Column(String)
|
||||||
|
wechat_number = Column(String)
|
||||||
|
wxid = Column(String, index=True, unique=True)
|
||||||
|
gender = Column(String)
|
||||||
|
friend_add_time = Column(String)
|
||||||
|
openid = Column(String, index=True)
|
||||||
23
app/schemas.py
Normal file
23
app/schemas.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class UserBase(BaseModel):
|
||||||
|
nickname: Optional[str] = None
|
||||||
|
remark: Optional[str] = None
|
||||||
|
wechat_number: Optional[str] = None
|
||||||
|
wxid: Optional[str] = None
|
||||||
|
gender: Optional[str] = None
|
||||||
|
friend_add_time: Optional[str] = None
|
||||||
|
openid: Optional[str] = None
|
||||||
|
|
||||||
|
class UserCreate(UserBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class UserUpdate(UserBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class User(UserBase):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
fastapi
|
||||||
|
uvicorn
|
||||||
|
sqlalchemy
|
||||||
|
pydantic
|
||||||
Reference in New Issue
Block a user