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)))
Beispiel #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}.'))
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)))
Beispiel #4
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}.'))
Beispiel #5
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)))