Пример #1
0
def lambda_handler(event, context):
    """
    Handler function for Lambda events
    Returns all of the media in the database for a specified car and tag as JSON
    """
    # Get the carid
    carId = cc.get_queryString(event, "carid")
    try:
        if not carId.isdigit():
            raise Exception
    except Exception as e:
        # Return an error to the client if a carId is not specified correctly
        msg = f"'carid' must be specified in the queryStringParameters header and must be an integer."
        reason = {"reason": msg}
        return cc.response_bad_request(json.dumps(reason))
    
    # Get the tagid
    tagId = cc.get_queryString(event, "tagid")
    try:
        if not tagId.isdigit():
            raise Exception
    except Exception as e:
        # Return an error to the client if a tagId is not specified correctly
        msg = f"'tagid' must be specified in the queryStringParameters header and must be an integer."
        reason = {"reason": msg}
        return cc.response_bad_request(json.dumps(reason))
    
    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                           os.environ["dbHost"], os.environ["dbPass"])
    
    # Return the list of media
    if(conn != None):
        return cc.response_ok(json.dumps(get_media_by_car_tag(conn, carId, tagId)))
Пример #2
0
def lambda_handler(event, context):
    """
    Handler function for Lambda events
    Deletes a media entry from the database
    """
    # Get the mediaId
    mediaId = cc.get_queryString(event, "mediaid")
    try:
        if not mediaId.isdigit():
            raise Exception
    except Exception as e:
        # Return an error to the client if a mediaId is not specified correctly
        msg = f"'mediaid' must be specified in the queryStringParameters header and must be an integer."
        reason = {"reason": msg}
        return cc.response_bad_request(json.dumps(reason))
    
    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                           os.environ["dbHost"], os.environ["dbPass"])
    
    # Delete the media entry
    try:
        with conn, conn.cursor() as cursor:
            cursor.execute(sql.SQL("DELETE FROM {} WHERE mediaid = %s;").format(sql.Identifier("media")), [mediaId])
    except psycopg2.Error as e:
        print(f"Error: {e}")
        return cc.response_bad_request(json.dumps(json.dumps("An error occured when removing the media entry.")))
    
    return cc.response_ok(json.dumps(f'Removed the media entry for mediaid: {mediaId}.'))
Пример #3
0
def lambda_handler(event, context):

    tags = json.loads(event)
    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                         os.environ["dbHost"], os.environ["dbPass"])
    insertTags(conn, tags)
Пример #4
0
def lambda_handler(event, context):
    """
    Handler function for Lambda events
    Returns all of the available AR buttons
    """
    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                         os.environ["dbHost"], os.environ["dbPass"])

    # Return the list of ar features and tags
    if (conn != None):
        return cc.response_ok(json.dumps(getArButtons(conn)))
Пример #5
0
def lambda_handler(event, context):

    car = tuple(event)

    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                         os.environ["dbHost"], os.environ["dbPass"])
    
    if(conn != None):
        response = insertCar(conn, car)
        return response
    else:
        return "Error connecting to database."
Пример #6
0
def lambda_handler(event, context):
    """
    Handler function for Lambda events
    Removes a specific car and all associated data from the database
    """
    # Get the carId
    carId = cc.get_queryString(event, "carid")
    try:
        if not carId.isdigit():
            raise Exception
    except Exception as e:
        # Return an error to the client if a carId is not specified correctly
        msg = f"'carid' must be specified in the queryStringParameters header and must be an integer."
        reason = {"reason": msg}
        return cc.response_bad_request(json.dumps(reason))

    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                         os.environ["dbHost"], os.environ["dbPass"])

    # Delete the car and all its data
    try:
        with conn, conn.cursor() as cursor:
            # Remove all the media for the car
            cursor.execute(
                sql.SQL("DELETE FROM {} WHERE carid = %s;").format(
                    sql.Identifier("media")), [carId])

            # Remove all ar_tag entries for the car
            cursor.execute(
                sql.SQL(
                    "DELETE FROM {} USING ar WHERE ar.carid = %s AND ar.arid = ar_tag.arid;"
                ).format(sql.Identifier("ar_tag")), [carId])

            # Remove all the tags for the car
            cursor.execute("DELETE FROM tag WHERE carid = %s", [carId])

            # Remove all the ar tags for the car
            cursor.execute("DELETE FROM ar WHERE carid = %s", [carId])

            # Remove the car
            cursor.execute("DELETE FROM car WHERE carid = %s", [carId])
    except psycopg2.Error as e:
        print(f"Error: {e}")
        return cc.response_bad_request(
            json.dumps(json.dumps("An error occured when removing the car.")))

    return cc.response_ok(
        json.dumps(f'Removed the car and all data for carid: {carId}.'))
Пример #7
0
def lambda_handler(event, context):

    arButtons = list()
    #Try to retrieve the appropriate data from the body. If an error occurs, send a 400 error code as a responset
    try:
        for arButton in event:
            data = list()
            data.append(arButton['feature'])
            data.append(arButton['section'])
            data.append(arButton['image'])
            arButtons.append(tuple(data))
    except:
        return cc.response_bad_request("Invalid body format")

    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                         os.environ["dbHost"], os.environ["dbPass"])
    return insertArButtons(conn, arButtons)
Пример #8
0
def lambda_handler(event, context):
    """
    Handler function for Lambda events
    Returns all of the cars in the database as JSON
    """
    # Get the platform: web or iOS
    platform = cc.get_queryString(event, "platform")
    if not platform in cc.valid_platforms:
        # Return an error to the client if a platform is not specified correctly
        msg = f"'platform' must be specified in the queryStringParameters header with a value from: {cc.valid_platforms}."
        reason = {"reason": msg}
        return cc.response_bad_request(json.dumps(reason))

    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                         os.environ["dbHost"], os.environ["dbPass"])

    # Return the list of cars
    if (conn != None):
        return cc.response_ok(json.dumps(get_car_all(conn, platform)))
Пример #9
0
def lambda_handler(event, context):
    
    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                           os.environ["dbHost"], os.environ["dbPass"])
    
    # Drop all the tables
    try:
        with conn, conn.cursor() as cursor:
            cursor.execute("DROP TABLE IF EXISTS media CASCADE;")
            cursor.execute("DROP TABLE IF EXISTS tag CASCADE;")
            cursor.execute("DROP TABLE IF EXISTS ar_tag CASCADE;")
            cursor.execute("DROP TABLE IF EXISTS car CASCADE;")
    except psycopg2.Error as e:
        print(f"Error: {e}")
    
    return {
        'statusCode': 200,
        'body': json.dumps('Called dropTables.')
    }
Пример #10
0
def lambda_handler(event, context):

    #Try to retrieve the appropriate data from the body. If an error occurs, send a 400 error code as a response
    try:
        body = json.loads(event["body"])
        carId  = body["carId"]
        name = body["name"]
        type = body["type"]
        link = body["link"]
        primaryTag = body["primaryTag"]
        secondaryTag = body["secondaryTag"]
    except:
        return cc.response_bad_request("Invalid body format")
    
    #Create a tuple containing the media data
    media = (carId, name, type, link, primaryTag, secondaryTag)

    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                         os.environ["dbHost"], os.environ["dbPass"])
    return insertMedia(conn, media)
Пример #11
0
def lambda_handler(event, context):
    """
    Handler function for Lambda events
    Returns all of the media in the database for a specified car as JSON
    """
    # Get the carid
    carId = cc.get_queryString(event, "carid")
    try:
        if not carId.isdigit():
            raise Exception
    except Exception as e:
        # Return an error to the client if a carId is not specified correctly
        msg = f"'carid' must be specified in the queryStringParameters header and must be an integer."
        reason = {"reason": msg}
        return cc.response_bad_request(json.dumps(reason))
    
    # Get the optional tagNames
    tagNames = cc.get_queryString(event, "tagnames")
    
    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                           os.environ["dbHost"], os.environ["dbPass"])
    
    # Return the list of media
    if(conn != None):
        if tagNames and tagNames == "true":
            mediaJson = get_media_by_car(conn, carId)
            tagJson = get_tag_by_car(conn, carId)
            
            tagDict = {td["tagid"]:td["name"] for td in tagJson}
            
            for md in mediaJson:
                md["primarytagname"] = tagDict[md["primarytag"]]
                md["secondarytagname"] = "N/A" if md["secondarytag"] == None else tagDict[md["secondarytag"]]
            
            return cc.response_ok(json.dumps(mediaJson))
        
        # Get the media without the tag names
        return cc.response_ok(json.dumps(get_media_by_car(conn, carId)))
Пример #12
0
def lambda_handler(event, context):

    arFeatures = list()
    #Try to retrieve the appropriate data from the body. If an error occurs, send a 400 error code as a response
    try:
        body = json.loads(event["body"])

        #Try to retrieve the appropriate data from the body. If an error occurs, send a 400 error code as a responset
        for ar in body:
            data = list()
            data.append(ar["carId"])
            data.append(ar["enabled"])
            data.append(ar["location"])
            data.append(ar["primaryTag"])
            data.append(ar["secondaryTag"])
            data.append(ar["arButtonId"])
            arFeatures.append(tuple(data))  
    except:
        return cc.response_bad_request("Invalid body format")

    #Create a connection to the database
    conn = cc.db_connect(os.environ["dbName"], os.environ["dbUser"],
                         os.environ["dbHost"], os.environ["dbPass"])
    return insertAr(conn, arFeatures)