async def deleteUser(token: str) -> bool: email = getEmailFromJWTToken(token) if await UserDao.deleteUser(email): return True raise HTTPException(status_code=401, detail="User already deleted")
async def createLicencePlate(licencePlate: LicencePlate, token: str) -> LicencePlate: email = getEmailFromJWTToken(token) user = await getUserByEmail(email) licencePlate.userID = user["userID"] if not await licencePlateExists(licencePlate.plateRegistration): licencePlate.registrationID = await LicencePlateDao.createLicencePlate(licencePlate) return licencePlate else: raise HTTPException(status_code=HTTP_409_CONFLICT, detail="Duplicate Licence plate")
async def createCamera(camera: Camera, token: str) -> Camera: email = getEmailFromJWTToken(token) user = await getUserByEmail(email) camera.userID = user["userID"] if not await cameraExists(camera): camera.cameraID = await CameraDao.createCamera(camera) sendMessage(routing_key='testingDetection', body={"ID": camera.cameraID, "action": "add"}) return camera else: raise HTTPException(status_code=HTTP_409_CONFLICT, detail="Duplicate Camera")
async def deleteLicencePlate(token: str, licencePlateID: int) -> bool: email = getEmailFromJWTToken(token) user = await getUserByEmail(email) licencePlate = await getLicencePlateByID(licencePlateID) if licencePlate["userID"] == user["userID"]: if await LicencePlateDao.deleteLicencePlate(licencePlateID): return True raise HTTPException(status_code=HTTP_500_INTERNAL_SERVER_ERROR, detail="Could not delete licence plate") raise HTTPException(status_code=HTTP_409_CONFLICT, detail="Licence plate does not belong to current user")
async def deleteCamera(token: str, cameraID: int) -> bool: email = getEmailFromJWTToken(token) user = await getUserByEmail(email) camera = await getCameraByID(cameraID) if camera["userID"] == user["userID"]: if await CameraDao.deleteCamera(cameraID): sendMessage(routing_key='testingDetection', body={"ID": cameraID, "action": "kill"}) return True raise HTTPException(status_code=HTTP_500_INTERNAL_SERVER_ERROR, detail="Could not delete camera") raise HTTPException(status_code=HTTP_409_CONFLICT, detail="Camera does not belong to current user")
async def updateCamera(token: str, cameraID: int, camera: Camera) -> Camera: email = getEmailFromJWTToken(token) user = await getUserByEmail(email) currentCamera = await getCameraByID(cameraID) if currentCamera["userID"] == user["userID"]: if camera.userID == None: camera.userID = user["userID"] if await CameraDao.updateCamera(camera, cameraID): camera.cameraID = cameraID return camera raise HTTPException(status_code=HTTP_500_INTERNAL_SERVER_ERROR, detail="Could not update camera") raise HTTPException(status_code=HTTP_409_CONFLICT, detail="Camera does not belong to current user")
async def updateLicencePlate(token: str, licencePlateID: int, licencePlate: LicencePlate) -> LicencePlate: email = getEmailFromJWTToken(token) user = await getUserByEmail(email) currentLicencePlate = await getLicencePlateByID(licencePlateID) if currentLicencePlate["userID"] == user["userID"]: if licencePlate.userID == None: licencePlate.userID = currentLicencePlate["userID"] if await LicencePlateDao.updateLicencePlate(licencePlate, licencePlateID): licencePlate.registrationID = licencePlateID return licencePlate raise HTTPException(status_code=HTTP_500_INTERNAL_SERVER_ERROR, detail="Could not update licence plate") raise HTTPException(status_code=HTTP_409_CONFLICT, detail="Licence plate does not belong to current user")
async def updateUser(token: str, user: User, id: int) -> User: email = getEmailFromJWTToken(token) currentUser = await getUserByEmail(email) if currentUser["userID"] == id: if verifyPassword(user.password, currentUser["password"]): if user.newPassword != None: # set new password user.password = getHashedPassword(user.newPassword) else: # use current password user.password = currentUser["password"] if await UserDao.updateUser(user, id): user.userID = id return user raise HTTPException(status_code=HTTP_500_INTERNAL_SERVER_ERROR, detail="Could not update user") raise HTTPException(status_code=HTTP_409_CONFLICT, detail="User password does not match match") raise HTTPException(status_code=HTTP_409_CONFLICT, detail="A user can only edit their own details")
async def getUser(token: str) -> dict: email = getEmailFromJWTToken(token) return await getUserByEmail(email)