Skip to content
Snippets Groups Projects
Commit 814aec23 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

added route to update device name

parent 8d53fca9
Branches
Tags
No related merge requests found
...@@ -31,7 +31,7 @@ def get_devices(db: Session, skip: int = 0, limit: int = 100): ...@@ -31,7 +31,7 @@ def get_devices(db: Session, skip: int = 0, limit: int = 100):
return db.query(Models.Device).offset(skip).limit(limit).all() return db.query(Models.Device).offset(skip).limit(limit).all()
def get_device(db: Session, deviceId: int): def get_device(db: Session, deviceId: int) -> Models.Device:
return db.query(Models.Device).filter(Models.Device.id == deviceId).first() return db.query(Models.Device).filter(Models.Device.id == deviceId).first()
...@@ -48,6 +48,15 @@ def create_device(db: Session, device: Schemas.DeviceCreate): ...@@ -48,6 +48,15 @@ def create_device(db: Session, device: Schemas.DeviceCreate):
return dbDevice return dbDevice
@notify_backup_service(BACKUP_SERVICE)
def update_device(db: Session, deviceId: int, device: Schemas.DeviceCreate):
existingDevice = get_device(db, deviceId)
existingDevice.name = device.name
db.commit()
db.refresh(existingDevice)
return existingDevice
@notify_backup_service(BACKUP_SERVICE) @notify_backup_service(BACKUP_SERVICE)
def delete_device(db: Session, device: Schemas.Device): def delete_device(db: Session, device: Schemas.Device):
db.delete(device) db.delete(device)
......
...@@ -40,6 +40,17 @@ async def create_device(device: Schemas.DeviceCreate, db: Session = Depends(get_ ...@@ -40,6 +40,17 @@ async def create_device(device: Schemas.DeviceCreate, db: Session = Depends(get_
return Crud.create_device(db=db, device=device) return Crud.create_device(db=db, device=device)
@router.put('/{deviceId}', response_model=Schemas.Device,
summary='Updates a device',
responses={404: {'description': 'Device not found'}},
dependencies=[Depends(check_api_key)])
async def update_device(deviceId: int, device: Schemas.DeviceCreate, db: Session = Depends(get_database)):
createdDevice = Crud.get_device_by_name(db, device.name)
if createdDevice:
raise HTTPException(status_code=404, detail='Device not found')
return Crud.update_device(db=db, deviceId=deviceId, device=device)
@router.delete('/{deviceId}', response_model=Status, @router.delete('/{deviceId}', response_model=Status,
summary='Deletes a specific device', summary='Deletes a specific device',
description='All corresponding sensors and measurements will be deleted too.', description='All corresponding sensors and measurements will be deleted too.',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment