Groovy web console

subscribe to the feed Subscribe
to this
site

Untitled

Published 3 months ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def expected = [otherPhotos:[
    [photoURI:'blubb2.jpg', reviewDate:'31.12.2019'],
    [photoURI:'blubb3.jpg', reviewDate:'31.12.2019'],
    [photoURI:'blubb4.jpg', reviewDate:'31.12.2019']
], 
firstPhoto:[photoURI:'blubb1.jpg', reviewDate:'31.12.2019']]

def firstPhoto      
def otherPhotos = []
// def sortedPhotos = ProfilePhoto.findAllByProfile(profile,[sort: "position", order: "asc"])
def sortedPhotos = [1,2,3,4]
sortedPhotos.eachWithIndex { photo, int index ->
    def photoInfo = [                    
        photoURI  : "blubb${index + 1}.jpg",
        reviewDate: '31.12.2019'           
    ]
    if(index==0) {    
        firstPhoto = photoInfo    
    } else {    
        otherPhotos.add(photoInfo)      
    }     
}
assert otherPhotos == expected.otherPhotos
assert firstPhoto == expected.firstPhoto


def head = sortedPhotos.head()
firstPhoto = [
     photoURI  : "blubb1.jpg",
     reviewDate: '31.12.2019'           
]

otherPhotos = sortedPhotos.tail().collect {
    [                    
        photoURI  : "blubb${it}.jpg",
        reviewDate: '31.12.2019'           
    ]
}

assert otherPhotos == expected.otherPhotos
assert firstPhoto == expected.firstPhoto