diff --git a/sql_app/crud.py b/sql_app/crud.py index b649817..31ac0f3 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -14,7 +14,7 @@ def get_user(db: Session, user_id: int): return db.query(models.User).get(user_id) def get_iot_entity(db: Session, id: int): - return db.query(models.IotEntity).filter(models.IotEntity.id == id).first() + return db.query(models.IotEntity).get(id) def get_iot_entity_by_description(db: Session, description: str): return db.query(models.IotEntity).filter(models.IotEntity.description == description).first() @@ -41,7 +41,6 @@ def create_user(db: Session, user: schemas.UserCreate): db.refresh(db_user) return db_user - def get_iot_entities(db: Session, skip: int = 0, limit: int = 100): return db.query(models.IotEntity).offset(skip).limit(limit).all() @@ -56,12 +55,24 @@ def create_iot_entity(db: Session, iot_entity: schemas.IotEntityCreate): def create_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int): # Ensure link is not already present and it does not allow duplicates + link = db.query(models.UserAuthToIoTDev).filter(models.UserAuthToIoTDev.user_id == user_id).filter(models.UserAuthToIoTDev.iot_entity_id == iot_dev_id).first() + if link: return True new_link = models.UserAuthToIoTDev(user_id=user_id, iot_entity_id=iot_dev_id) db.add(new_link) db.commit() db.refresh(new_link) return True +def remove_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int): + # Ensure link is not already present and it does not allow duplicates + link = db.query(models.UserAuthToIoTDev).filter(models.UserAuthToIoTDev.user_id == user_id).filter(models.UserAuthToIoTDev.iot_entity_id == iot_dev_id).first() + if not link: return True + db.delete(link) + db.flush() + db.commit() + #db.refresh(link) + return True + def set_open_door_request(db: Session, iot_entity_id: int): device = get_iot_entity(db, iot_entity_id) setattr(device, "open_request", True) diff --git a/sql_app/main.py b/sql_app/main.py index 0a24267..7b50911 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -102,7 +102,6 @@ def read_user(user_id: int, db: Session = Depends(get_db)): raise HTTPException(status_code=404, detail="User not found") return db_user -# TODO: Can duplicate @app.post("/admin/users/allowdevice/id", tags=['Admin']) def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)): user = crud.get_user(db, request.user_id) @@ -117,7 +116,7 @@ def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReques if not res: raise HTTPException(status_code=500, detail="Could not complete operation") - return user + return @app.post("/admin/users/disallowdevice/id", tags=['Admin']) def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)): @@ -129,8 +128,7 @@ def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReq if not iot_entity: raise HTTPException(status_code=404, detail="Iot Entity not found") - #res = crud.create_user_link_to_iot(db, request.user_id, request.iot_entity_id) - # Implement remove link + res = crud.remove_user_link_to_iot(db, request.user_id, request.iot_entity_id) if not res: raise HTTPException(status_code=500, detail="Could not complete operation") @@ -173,7 +171,7 @@ def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: sc user = crud.get_user_by_username(db, current_user.username) return user.authorized_devices -@app.post("/users/open",tags=['Users']) +@app.post("/users/open", tags=['Users']) def issue_open_door_command(command: schemas.OpenDoorRequestBase, db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_active_user)):