From 43745c278e6b5398ca4416ed7494c42e0bc9110f Mon Sep 17 00:00:00 2001 From: kongxy Date: Thu, 18 Jul 2024 23:14:03 +0800 Subject: [PATCH] =?UTF-8?q?device=20=E6=9F=A5=E8=AF=A2=20=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crud.py | 16 ++++++++++++++++ main.py | 18 ++++++++++++++++++ schemas.py | 24 ++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/crud.py b/crud.py index a4bcc79..3b3ab93 100644 --- a/crud.py +++ b/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 diff --git a/main.py b/main.py index b41b585..f7ac807 100644 --- a/main.py +++ b/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 diff --git a/schemas.py b/schemas.py index bccea25..f49b196 100644 --- a/schemas.py +++ b/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