Browse Source

device 查询 创建接口

master
kongxy 6 months ago
parent
commit
43745c278e
  1. 16
      crud.py
  2. 18
      main.py
  3. 24
      schemas.py

16
crud.py

@ -17,3 +17,19 @@ def create_equipment(db: Session, equipment: schemas.EquipmentCreate):
db.commit()
db.refresh(db_equipment)
return db_equipment
def get_device(db: Session, device_id: int):
return db.query(models.Device).filter(models.Device.id == device_id).first()
def get_devices(db: Session, skip: int = 0, limit: int = 10):
return db.query(models.Device).offset(skip).limit(limit).all()
def create_device(db: Session, device: schemas.DeviceCreate):
db_device = models.Device(**device.dict())
db.add(db_device)
db.commit()
db.refresh(db_device)
return db_device

18
main.py

@ -32,3 +32,21 @@ def get_equipments(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)
def get_equipment(equipment_id: int, db: Session = Depends(get_db)):
equipment = crud.get_equipment(db=db, equipment_id=equipment_id)
return equipment
@app.get("/devices", response_model=list[schemas.Device])
def get_devices(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
devices = crud.get_devices(db, skip=skip, limit=limit)
return devices
@app.get("/devices/{device_id}", response_model=schemas.Device)
def get_device(device_id: int, db: Session = Depends(get_db)):
device = crud.get_device(db=db, device_id=device_id)
return device
@app.post("/devices", response_model=schemas.Device)
def create_device(device: schemas.DeviceCreate, db: Session = Depends(get_db)):
db_device = crud.create_device(db=db, device=device)
return db_device

24
schemas.py

@ -17,5 +17,25 @@ class EquipmentCreate(BaseModel):
class Equipment(EquipmentCreate):
id: int
class Config:
orm_mode = True
class ConfigDict:
from_attributes = True
class DeviceCreate(BaseModel):
year: int
name: str
auth: int
dep: str
num: int
pPrice: float
dPrice: float
proNum: str
process: int
delFlag: int = 0
class Device(DeviceCreate):
id: int
class ConfigDict:
from_attributes = True

Loading…
Cancel
Save