Skip to content

Instantly share code, notes, and snippets.

View Tonel's full-sized avatar

Antonello Zanini Tonel

View GitHub Profile
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.3.1.RELEASE"
id("io.spring.dependency-management") version "1.0.9.RELEASE"
kotlin("jvm") version "1.3.72"
kotlin("plugin.spring") version "1.3.72"
kotlin("kapt") version "1.3.72"
}
@Tonel
Tonel / Author.kt
Last active March 10, 2021 20:09
Domain model objects
class Author {
var id: Int? = null
var name: String? = null
var surname: String? = null
var birthDate: Date? = null
val books: MutableList<Book> = ArrayList()
@Tonel
Tonel / AuthorDto.kt
Last active March 10, 2021 20:09
DTOs
class AuthorDto {
@JsonProperty("id")
var id: Int? = null
@JsonProperty("name")
var name: String? = null
@JsonProperty("surname")
var surname: String? = null
class SpecialAuthorDto {
var name: String? = null
var surname: String? = null
// no birthDate
var firstBook : BookDto? = null
}
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
interface AuthorMapper {
fun authorToAuthorDto(
author : Author
) : AuthorDto
fun authorsToAuthorDtos(
authors : List<Author>
) : List<AuthorDto>
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-06-28T09:24:47+0200",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 13.0.2 (Oracle Corporation)"
)
@Component
public class AuthorMapperImpl implements AuthorMapper {
@Override
public AuthorDto authorToAuthorDto(Author author) {
@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
@RestController
@RequestMapping("/authors")
class AuthorController {
@Autowired
lateinit var authorRepository: AuthorRepository
@Autowired
lateinit var authorMapper: AuthorMapper
@Autowired
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "title")
@Entity
@Table(name = "post")
data class Post(
@get:Id
@get:GeneratedValue
@get:Column(name = "id")
val id: Long,
@get:Column(name = "title")
val title: String,