@@ -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) {
|
||||
|
||||
}
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -38,4 +38,6 @@ interface PersonRepository : CrudRepository<Person, String> {
|
||||
* 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
|
||||
}
|
||||
|
||||
@@ -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`() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user