diff --git a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/EmailAddress.kt b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/EmailAddress.kt new file mode 100644 index 000000000..7361e1524 --- /dev/null +++ b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/EmailAddress.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.mongodb.people + +/** + * @author Mark Paluch + */ +@JvmInline +value class EmailAddress(val address: String) { + +} diff --git a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt index 4b5d6d0ac..d56ebed07 100644 --- a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt +++ b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt @@ -23,6 +23,16 @@ import org.springframework.data.annotation.PersistenceCreator * * @author Mark Paluch */ -data class Person @PersistenceCreator constructor(@Id val id: String?, val firstname: String? = "Walter", val lastname: String = "") { - constructor(firstname: String?, lastname: String) : this(null, firstname, lastname); +data class Person @PersistenceCreator constructor( + @Id val id: String?, + val firstname: String? = "Walter", + val lastname: String = "", + val email: EmailAddress? +) { + constructor(firstname: String?, lastname: String) : this( + null, + firstname, + lastname, + null + ); } diff --git a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt index 0aea533bf..eaa727a32 100644 --- a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt +++ b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt @@ -38,4 +38,6 @@ interface PersonRepository : CrudRepository { * Query method requiring a result. Throws [org.springframework.dao.EmptyResultDataAccessException] if no result is found. */ fun findOneByFirstname(firstname: String): Person + + fun findOneByEmail(emailAddress: EmailAddress): Person } diff --git a/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt b/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt index 2b854913f..de847e428 100644 --- a/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt +++ b/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt @@ -68,6 +68,23 @@ class RepositoryTests { assertThat(walter.lastname).isEqualTo("White") } + @Test + fun `should find one person by email`() { + + repository.save( + Person( + null, + "Walter", + "White", + EmailAddress("heisenberg@gmail.com") + ) + ) + + val walter = repository.findOneByEmail(EmailAddress("heisenberg@gmail.com")) + + assertThat(walter.email?.address).isEqualTo("heisenberg@gmail.com") + } + @Test fun `should return null if no person found`() {