public String removeUnicornFromBasket()

in MonoToMicroLambda/src/main/java/com/monoToMicro/Lambda/UnicornBasketImpl.java [99:148]


	public String removeUnicornFromBasket(UnicornBasket unicornBasket, Context context) {
		
		//Build the DDB instance connection 
		AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();				
		DynamoDBMapper mapper = new DynamoDBMapper(client);
				
		//Get current basket
		UnicornBasket currentBasket = mapper.load(UnicornBasket.class, unicornBasket.getUuid());
		
		//if no basket exist then return an error
		if(currentBasket==null) {						
			return "No basket exist, nothing to delete";
		}
		
		//basket exist, will check if item exist and will remove
		List<Unicorn> currentUnicorns = currentBasket.getUnicorns();		
		List<Unicorn> unicornsToRemove = unicornBasket.getUnicorns();
		
		//Assuming only one will be added but checking for null or empty values
		if(unicornsToRemove!=null && !unicornsToRemove.isEmpty()) {
		
			Unicorn unicornToRemove = unicornsToRemove.get(0);
		
			String unicornToRemoveUuid = unicornToRemove.getUuid();
		
			for (Unicorn currentUnicorn: currentUnicorns) {		
			
				if(currentUnicorn.getUuid().equals(unicornToRemoveUuid)) {
					currentUnicorns.remove(currentUnicorn);
					if(currentUnicorns.isEmpty()) {
						//no more unicrons in basket, will delete the basket
						mapper.delete(currentBasket);
						return "Unicorn was removed and basket was deleted!";
					}else {
						//keeping basket alive as more unicrons are in it
						currentBasket.setUnicorns(currentUnicorns);
						mapper.save(currentBasket);						
						return "Unicorn was removed! Other unicorns are still in basket";	
					}					
				}							
			}
			
			if(currentBasket.getUnicorns()!=null && currentBasket.getUnicorns().isEmpty()) {
				//no unicorn to remove, will try to remove the basket nonetheless
				mapper.delete(currentBasket);
			}	
			return "Didn't find a unicorn to remove";
		}
		return "Are you sure you asked to remove a Unicron?";		
	}