Rules for the creation of a treasure

 

Here is the lesson code:

 
RuleContext CreateTreasureContext {
    Rule createTreasure {
        CreateTreasure ct = broker.ui.getDataSource()

        if (ct.name != null && ct.name != “” && ct.latitude != null && ct.longitude != null && ct.points.selected().size() > 0) {
            Treasure newTreasure = Treasure()
            newTreasure.name = ct.name
	newTreasure.latitude = ct.latitude
            newTreasure.longitude = ct.longitude
            Integer index = ct.points.selected().get(0)
            newTreasure.points = ct.points.get(index)

            broker.cloudPersistence.save(newTreasure)
             
            broker.ui.showAlert(“Yeah!”, "Thanks for adding a new treasure.")
            
        } else {
            broker.ui.showAlert("Oops", "Please, complete all the fields.")
        }
    }
}

Model Treasure {
	String name
	Double latitude
	Double longitude
	Integer points
	Bool hasBeenFound value(false)
}

Experience CreateTreasure {
    String name label("Treasure name") as TextField
    Double latitude label("latitude") as TextField
    Double longitude label("longitude") as TextField
    Array points value([])  as Select
    Array treasureLocationInMap as MapView

    Decision createNewTreasure action("CreateTreasureContext.createTreasure") label("Create Treasure")

    OnCreate {
        CreateTreasure ct = broker.ui.getDataSource()
        ct.points = treasureScores
        ct.treasureLocationInMap = []
    }
}

>Next