From 4598ca9c55ceea342c4583e730042d0ce91594c2 Mon Sep 17 00:00:00 2001 From: Tor Egil Jacobsen Date: Fri, 24 May 2024 12:13:13 +0200 Subject: [PATCH] functional fix of BlogService.saveOrUpdate --- .../jactor/persistence/entity/BlogEntryEntity.kt | 2 +- .../github/jactor/persistence/service/BlogService.kt | 7 ++++--- .../jactor/persistence/aop/ModifierAspectTest.kt | 10 +++++++++- .../jactor/persistence/service/BlogServiceTest.kt | 6 +++++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/persistence/src/main/kotlin/com/github/jactor/persistence/entity/BlogEntryEntity.kt b/persistence/src/main/kotlin/com/github/jactor/persistence/entity/BlogEntryEntity.kt index 349455ab..f9098a6d 100644 --- a/persistence/src/main/kotlin/com/github/jactor/persistence/entity/BlogEntryEntity.kt +++ b/persistence/src/main/kotlin/com/github/jactor/persistence/entity/BlogEntryEntity.kt @@ -51,7 +51,7 @@ class BlogEntryEntity : PersistentEntity { } constructor(blogEntryDto: BlogEntryDto) { - blog = blogEntryDto.blog?.let { BlogEntity(it) } + blog = BlogEntity(blogDto = blogEntryDto.blog ?: error("Entry must belong to a blog")) entryEmbeddable = EntryEmbeddable(blogEntryDto.notNullableCreator, blogEntryDto.notNullableEntry) id = blogEntryDto.id persistentDataEmbeddable = PersistentDataEmbeddable(blogEntryDto.persistentDto) diff --git a/persistence/src/main/kotlin/com/github/jactor/persistence/service/BlogService.kt b/persistence/src/main/kotlin/com/github/jactor/persistence/service/BlogService.kt index 6a7e79dd..773390a0 100644 --- a/persistence/src/main/kotlin/com/github/jactor/persistence/service/BlogService.kt +++ b/persistence/src/main/kotlin/com/github/jactor/persistence/service/BlogService.kt @@ -52,10 +52,11 @@ class DefaultBlogService( } override fun saveOrUpdate(blogEntryDto: BlogEntryDto): BlogEntryDto { - val userDto = userService.find(username = fetchUsername(blogEntryDto.blog)) - blogEntryDto.blog!!.userInternal = userDto + blogEntryDto.blog?.also { + it.id ?: error("An entry must belong to a persistent blog!") + } ?: error("An entry must belong to a blog!") - val blogEntryEntity = BlogEntryEntity(blogEntryDto) + val blogEntryEntity = BlogEntryEntity(blogEntryDto = blogEntryDto) return blogEntryRepository.save(blogEntryEntity).asDto() } diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/aop/ModifierAspectTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/aop/ModifierAspectTest.kt index ec8d2e0b..a40767ca 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/aop/ModifierAspectTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/aop/ModifierAspectTest.kt @@ -1,6 +1,7 @@ package com.github.jactor.persistence.aop import java.time.LocalDateTime +import java.util.UUID import org.aspectj.lang.JoinPoint import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith @@ -71,7 +72,14 @@ internal class ModifierAspectTest { @Test fun `should modify timestamp on blogEntry when used`() { val blogEntryWithoutId = BlogBuilder.new().withUnchangedEntry( - blogEntryDto = BlogEntryDto(persistentDto, BlogEntryDto(creatorName = "me", entry = "some shit")) + blogEntryDto = BlogEntryDto( + persistentDto = persistentDto, + blogEntry = BlogEntryDto( + blog = BlogDto(persistentDto = PersistentDto(id = UUID.randomUUID())), + creatorName = "me", + entry = "some shit" + ) + ) ).buildBlogEntryEntity() val blogEntry = BlogBuilder.new().withEntry( diff --git a/persistence/src/test/kotlin/com/github/jactor/persistence/service/BlogServiceTest.kt b/persistence/src/test/kotlin/com/github/jactor/persistence/service/BlogServiceTest.kt index e5efa8ed..45e20b6d 100644 --- a/persistence/src/test/kotlin/com/github/jactor/persistence/service/BlogServiceTest.kt +++ b/persistence/src/test/kotlin/com/github/jactor/persistence/service/BlogServiceTest.kt @@ -123,7 +123,11 @@ internal class BlogServiceTest { fun `should save BlogEntryDto as BlogEntryEntity`() { val blogEntryEntitySlot = slot() val blogEntryDto = BlogEntryDto() - blogEntryDto.blog = BlogDto(userInternal = UserInternalDto(username = "itsme")) + blogEntryDto.blog = BlogDto( + persistentDto = PersistentDto(id = UUID.randomUUID()), + userInternal = UserInternalDto(username = "itsme") + ) + blogEntryDto.creatorName = "me" blogEntryDto.entry = "if i where a rich man..."