Пример #1
0
def replace_boat(event, context):
    boat = BoatModel.get(hash_key=event['pathParameters']['id'])
    boat_data = json.loads(event['body'])
    if 'name' not in boat_data:
        logging.error('Validation Failed')
        return {
            'statusCode': 422,
            'body':
            json.dumps({'error_message': 'Couldn\'t create the new boat.'})
        }

    if not boat_data['name']:
        logging.error('Validation Failed - boat name was empty. %s', boat_data)
        return {
            'statusCode':
            422,
            'body':
            json.dumps(
                {'error_message': 'Couldn\'t create the boat. Name was empty'})
        }

    if 'name' in boat_data:
        boat.name = boat_data['name']
    if 'type' in boat_data:
        boat.type = boat_data['type']
    if 'length' in boat_data:
        boat.length = boat_data['length']
    # No 'at_sea'

    # write the boat to the database
    boat.save()

    # create a response
    return {'statusCode': 201, 'body': json.dumps(dict(boat))}
Пример #2
0
def get_boat(event, context):
    try:
        boat = BoatModel.get(hash_key=event['pathParameters']['id'])
    except DoesNotExist:
        return {
            'statusCode': 404,
            'body': json.dumps({'error_message': 'BOAT was not found'})
        }

    # Print boat
    return {'statusCode': 200, 'body': json.dumps(dict(boat))}
Пример #3
0
def delete_boat(event, context):
    boat = BoatModel(hash_key=event['pathParameters']['id'])
    bid = boat.id
    if boat.at_sea == True:
        slip = SlipModel.scan(
            filter_condition=(SlipModel.current_boat == '/boat/' + boat.id),
            limit=1)
        for s in slip:
            s.update(actions=[
                SlipModel.current_boat.remove(),
                SlipModel.arrival_date.remove()
            ])

    boat.delete()
    return {
        'statusCode':
        200,
        'body':
        json.dumps({
            'success': 'Boat {0} deleted'.format(bid),
            'boat_id': bid
        })
    }
Пример #4
0
def boat_arrival(event, context):
    try:  # get boat
        boat = BoatModel.get(hash_key=event['pathParameters']['id'])
    except DoesNotExist:  # if not found then 'not found'
        return {
            'statusCode': 404,
            'body': json.dumps({'error_message': 'BOAT was not found'})
        }

    # sc = EntityType.count(hash_key='entity_type', filter_condition=SlipModel.current_boat.does_not_exist() & (SlipModel.entity_type == 'slip'))
    # sc = SlipModel.e_type.count(hash_key='entity_type', filter_condition=SlipModel.current_boat.does_not_exist() & (SlipModel.entity_type == 'slip'))
    try:
        # scan for empty slip. Only need one so limited to one result
        # slip = SlipModel.query(filter_condition=SlipModel.current_boat.does_not_exist() & (SlipModel.entity_type == 'slip'), limit=1)

        slip = SlipModel.scan(
            filter_condition=SlipModel.current_boat.does_not_exist() &
            (SlipModel.entity_type == 'slip'),
            limit=1)
    except DoesNotExist:
        return {
            'statusCode': 403,
            'body': json.dumps({'error message': 'No slips found'})
        }

    boat.update(actions=[BoatModel.at_sea.set(False)
                         ])  # set boat at_sea to false
    for s in slip:
        if s.count > 0:
            s.update(actions=[
                SlipModel.current_boat.set('/boat/' + boat.id),
                SlipModel.arrival_date.set(str(datetime.now()))
            ])
            b = path + boat.id
            return {
                'statusCode':
                200,
                'body':
                json.dumps({
                    'success':
                    'Boat {0} arrived in slip {1}'.format(b, s.id),
                    'slip':
                    dict(s)
                })
            }
        else:
            return {
                'statusCode': 403,
                'body': json.dumps({'error message': 'No empty slips'})
            }
Пример #5
0
def get_boats(event, context):
    try:
        boats = BoatModel.scan(
            filter_condition=(BoatModel.entity_type == 'boat'))
        # for b in BoatModel.scan(filter_condition=(BoatModel.entity_type ==))
    except DoesNotExist:
        return {
            'statusCode': 404,
            'body': json.dumps({'error_message': 'No boats in table'})
        }
    # Print all boats
    return {
        'statusCode': 200,
        'body': json.dumps({'boats': [dict(boat) for boat in boats]})
    }
Пример #6
0
def boat_departure(event, context):
    try:
        boat = BoatModel.get(hash_key=event['pathParameters']['id'])
    except DoesNotExist:
        return {
            'statusCode': 404,
            'body': json.dumps({'error_message': 'BOAT was not found'})
        }

    try:
        # Scan for boat in slip
        slip = SlipModel.scan(
            filter_condition=(SlipModel.current_boat == '/boat/' + boat.id),
            limit=1)
    except ScanError:  # If scan fails return
        return {
            'statusCode': 404,
            'body': json.dumps({'error message': 'Slip not found'})
        }  # Don't need this

    # If not slip return
    # if slip.total_count == 0:
    #     return {'statusCode': 404, 'body': json.dumps({'error message': 'BOAT not in slip'})}

    # Set to 'at sea'
    boat.update(actions=[BoatModel.at_sea.set(True)])
    for s in slip:
        s.update(actions=[
            SlipModel.current_boat.remove(),
            SlipModel.arrival_date.remove()
        ])
        return {
            'statusCode':
            200,
            'body':
            json.dumps({
                'success':
                'Boat {0} departed slip {1}'.format(boat.id, s.id),
                'slip_id':
                '{0}'.format(s.id),
                'boat_id':
                '{0}'.format(boat.id)
            })
        }
Пример #7
0
def create(event, context):
    boat_data = json.loads(event['body'])
    if 'name' not in boat_data:
        logging.error('Validation Failed')
        return {
            'statusCode': 422,
            'body':
            json.dumps({'error_message': 'Couldn\'t create the new boat.'})
        }

    if not boat_data['name']:
        logging.error('Validation Failed - boat name was empty. %s', boat_data)
        return {
            'statusCode':
            422,
            'body':
            json.dumps(
                {'error_message': 'Couldn\'t create the boat. Name was empty'})
        }

    new_boat = BoatModel()
    new_boat.id = create_id()
    if 'name' in boat_data:
        new_boat.name = boat_data['name']
    if 'type' in boat_data:
        new_boat.type = boat_data['type']
    if 'length' in boat_data:
        new_boat.length = boat_data['length']
    if 'at_sea' in boat_data:
        new_boat.at_sea = boat_data['at_sea']

    # write the new boat to the database
    new_boat.save()

    # create a response
    return {'statusCode': 201, 'body': json.dumps(dict(new_boat))}
Пример #8
0
def delete_slip(event, context):
    slip = SlipModel.get(hash_key=event['pathParameters']['id'])
    # If boat contains a slip, set the boat at_sea to true
    if slip.current_boat:
        bid = slip.current_boat[-6:]
        boat = BoatModel.get(hash_key=bid)
        boat.update(actions=[BoatModel.at_sea.set(True)])

    sid = slip.id
    slip.delete()
    return {
        'statusCode':
        200,
        'body':
        json.dumps({
            'success': 'Slip {0} deleted'.format(sid),
            'slip_id': sid
        })
    }
Пример #9
0
def mod_boat(event, context):
    try:
        boat = BoatModel.get(hash_key=event['pathParameters']['id'])
    except DoesNotExist:
        return {
            'statusCode': 404,
            'body': json.dumps({'error_message': 'BOAT was not found'})
        }

    # Decided not to allow at_sea to be changed here
    # at_sea_changed = False
    boat_data = json.loads(event['body'])
    if 'name' in boat_data:
        boat.name = boat_data['name']
    if 'type' in boat_data:
        boat.type = boat_data['type']
    if 'length' in boat_data:
        boat.length = boat_data['length']
    # if 'at_sea' in boat_data:
    #     boat.at_sea = boat_data['at_sea']
    #     at_sea_changed = True

    boat.save()
    # Leaving this out
    # if at_sea_changed:
    #     try:
    #     # scan for empty slip. Only need one so limited to one result
    #     # slip = SlipModel.query('current_boat', SlipModel.current_boat.contains(boat.id))
    #         slip = SlipModel.scan(filter_condition=(SlipModel.current_boat == '/boat/' + boat.id), limit=1)
    #     for s in slip:
    #         s.update(actions=[SlipModel.current_boat.remove(), SlipModel.arrival_date.remove()])
    #         return {'statusCode': 200, 'body': json.dumps(dict(boat)), {'note': 'at_sea attribute changed'}}
    # if at_sea_changed:
    #     return {'statusCode': 200, 'body': json.dumps(dict(boat)), {'WARNING': 'at_sea attribute changed. \
    #                             Use \'DELETE /boat/{id}/slip\' when changing this status. Boat {0} still ' }}
    return {'statusCode': 200, 'body': json.dumps(dict(boat))}