def lambda_handler()

in source/backend/lambda_schema_app/lambda_schema_app.py [0:0]


def lambda_handler(event, context):
    print(event)
    if event['httpMethod'] == 'GET':
        resp = schema_table.get_item(Key={'schema_name' : 'app'})
        if 'Item' in resp:
            item = resp['Item']
            return {'headers': {'Access-Control-Allow-Origin': '*'},
                    'body': json.dumps(item)}
        else:
            return {'headers': {'Access-Control-Allow-Origin': '*'}, 'body': json.dumps([])}

    elif event['httpMethod'] == 'POST':
        try:
            body = json.loads(event['body'])
            if "app_id" in body:
                return {'headers': {'Access-Control-Allow-Origin': '*'},
                        'statusCode': 400, 'body': "You cannot create app_id schema, this is managed by the system"}
        except:
            return {'headers': {'Access-Control-Allow-Origin': '*'},
                    'statusCode': 400, 'body': 'malformed json input'}

        resp = schema_table.put_item(

            Item={
                'schema_name': 'app',
                'attributes' : body
            }
        )
        return {'headers': {'Access-Control-Allow-Origin': '*'},
                'body': json.dumps(resp)}

    elif event['httpMethod'] == 'PUT':
        try:
            body = json.loads(event['body'])
            if "app_id" in body:
                return {'headers': {'Access-Control-Allow-Origin': '*'},
                        'statusCode': 400, 'body': "You cannot create app_id schema, this is managed by the system"}
        except:
            return {'headers': {'Access-Control-Allow-Origin': '*'},
                    'statusCode': 400, 'body': 'malformed json input'}

        attributes = []
        names = []
        resp = schema_table.get_item(Key={'schema_name' : 'app'})
        if 'Item' in resp:
            attributes = resp['Item']['attributes']
        for attr in attributes:
            if 'name' in attr:
                names.append(attr['name'])
        if 'event' in body:
            if body['event'] == 'DELETE':
               if "name" not in body:
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: name is required"}
               for attr in attributes:
                   if attr['name'] == body['name']:
                       attributes.remove(attr)
            if body['event'] == 'PUT':
                if "update" not in body:
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: update is required"}
                if "name" not in body:
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: name is required"}
                if body['update']['type'] is '':
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: 'Type' cannot be empty"}
                if body['update']['description'] is '':
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: 'Description' cannot be empty"}
                if body['update']['name'] is '':
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: 'Name' can not be empty"}
                if body['update']['type'] == 'list':
                    if 'listvalue' in body['update']:
                        if body['update']['listvalue'] is '':
                            return {'headers': {'Access-Control-Allow-Origin': '*'},
                                    'statusCode': 400, 'body': "Attribute Name: 'List Value' can not be empty"}
                    else:
                            return {'headers': {'Access-Control-Allow-Origin': '*'},
                                    'statusCode': 400, 'body': "Attribute Name: 'List Value' can not be empty"}
                if body['update']['name'] in names and body['name'] != body['update']['name']:
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Name: " + body['update']['name'] + " already exist"}
                check = False
                for attr in attributes:
                   if attr['name'] == body['name']:
                       if body['update']['type'] != 'list':
                          if 'listvalue' in body['update']:
                              del body['update']['listvalue']
                       check = True
                       index = attributes.index(attr)
                       attributes.remove(attr)
                       attributes.insert(index,body['update'])
                if check == False:
                      return {'headers': {'Access-Control-Allow-Origin': '*'},
                              'statusCode': 400, 'body': "Attribute Name: " + body['name'] +" does not exist"}
            if body['event'] == 'POST':
                if "new" not in body:
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: new is required"}
                if "name" not in body['new']:
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: name is required"}
                if body['new']['name'] in names:
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Name: " + body['new']['name'] + " already exist"}
                if body['new']['name'] == "":
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name can not be empty"}
                if 'description' not in body['new']:
                    return {'headers': {'Access-Control-Allow-Origin': '*'},
                            'statusCode': 400, 'body': "Attribute Name: 'Description' cannot be empty"}
                else:
                    if body['new']['description'] is '':
                        return {'headers': {'Access-Control-Allow-Origin': '*'},
                                'statusCode': 400, 'body': "Attribute Name: 'Description' cannot be empty"}
                if 'type' not in body['new']:
                        return {'headers': {'Access-Control-Allow-Origin': '*'},
                                'statusCode': 400, 'body': "Attribute Name: 'Type' cannot be empty"}
                else:
                    if body['new']['type'] is '':
                        return {'headers': {'Access-Control-Allow-Origin': '*'},
                                'statusCode': 400, 'body': "Attribute Name: 'Type' cannot be empty"}
                if body['new']['type'] == 'list':
                    if 'listvalue' in body['new']:
                        if body['new']['listvalue'] is '':
                            return {'headers': {'Access-Control-Allow-Origin': '*'},
                                    'statusCode': 400, 'body': "Attribute Name: 'List Value' can not be empty"}
                    else:
                            return {'headers': {'Access-Control-Allow-Origin': '*'},
                                    'statusCode': 400, 'body': "Attribute Name: 'List Value' can not be empty"}
                attributes.append(body['new'])
        else:
            return {'headers': {'Access-Control-Allow-Origin': '*'},
                    'statusCode': 400, 'body': "Attribute Name: event is required"}
        resp = schema_table.put_item(

            Item={
                'schema_name': 'app',
                'attributes' : attributes
            }
        )
        return {'headers': {'Access-Control-Allow-Origin': '*'},
                'body': json.dumps(resp)}