Skip to content

Instantly share code, notes, and snippets.

@Tonel
Last active March 10, 2021 20:08
Show Gist options
  • Save Tonel/cd057c2687edee7211e3a30a30ee9e43 to your computer and use it in GitHub Desktop.
Save Tonel/cd057c2687edee7211e3a30a30ee9e43 to your computer and use it in GitHub Desktop.
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
abstract class SpecialAuthorMapper {
// author's book property is accessed through java setter
@Mappings(
Mapping(target="firstBook", expression = "java(booksToFirstBook(author.getBooks()))")
)
abstract fun authorToSpecialAuthorDto(
author : Author
) : SpecialAuthorDto
// required in order to convert Book into BookDto
// in booksToFirstBook
abstract fun bookToBookDto(
book : Book
) : BookDto
// converting books into the first released book
fun booksToFirstBook(books : List<Book>) : BookDto {
return bookToBookDto(
books
.sortedWith(Comparator { e1: Book, e2: Book -> e1.releaseDate.compareTo(e2.releaseDate) })
.first()
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment