def replace_slip(event, context): try: slip = SlipModel.get(hash_key=event['pathParameters']['id']) except DoesNotExist: return { 'statusCode': 404, 'body': json.dumps({'error_message': 'SLIP was not found'}) } slip_data = json.loads(event['body']) if 'number' not in slip_data: return { 'statusCode': 422, 'body': json.dumps({ 'error_message': '\'number\' is only replaceable item. \'number\' requred.' }) } if 'number' in slip_data: slip.number = slip_data['number'] slip.save() return {'statusCode': 201, 'body': json.dumps(dict(slip))}
def get_slip(event, context): try: slip = SlipModel.get(hash_key=event['pathParameters']['id']) except DoesNotExist: return { 'statusCode': 404, 'body': json.dumps({'error_message': 'SLIP was not found'}) } return {'statusCode': 200, 'body': json.dumps(dict(slip))}
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'}) }
def get_slips(event, context): try: slips = SlipModel.scan( filter_condition=(SlipModel.entity_type == 'slip')) except DoesNotExist: return { 'statusCode': 404, 'body': json.dumps({'error_message': 'No slips in table'}) } return { 'statusCode': 200, 'body': json.dumps({'slips': [dict(slip) for slip in slips]}) }
def mod_slip(event, context): try: slip = SlipModel.get(hash_key=event['pathParameters']['id']) except DoesNotExist: return { 'statusCode': 404, 'body': json.dumps({'error_message': 'SLIP was not found'}) } # slip number is the only modifiable thing right now slip_data = json.loads(event['body']) if 'number' in slip_data: slip.number = slip_data['number'] slip.save() return {'statusCode': 200, 'body': json.dumps(dict(slip))}
def create(event, context): slip_data = json.loads(event['body']) new_slip = SlipModel() new_slip.id = create_id() if 'number' in slip_data: new_slip.number = slip_data['number'] else: # If no number provided just generate one new_slip.number = random.randint(1, 100) # write the slip to the database new_slip.save() # create a response return {'statusCode': 201, 'body': json.dumps(dict(new_slip))}
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) }) }
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 }) }
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 }) }