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