Skip to content

Commit

Permalink
Persistent id is UUID, not Long (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
jactor-rises authored May 20, 2024
2 parents 74da67d + 986c68c commit 37ae3ba
Show file tree
Hide file tree
Showing 67 changed files with 1,076 additions and 723 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import org.springframework.stereotype.Component
class ModifierAspect {
@Before("execution(* com.github.jactor.persistence.repository.*Repository.save(..))")
fun modifyPersistentEntity(joinPoint: JoinPoint): Any? {
joinPoint.args
return joinPoint.args
.filterIsInstance<PersistentEntity<*>>()
.filter { persistentEntity: PersistentEntity<*> -> persistentEntity.id != null }
.forEach { persistentEntity -> persistentEntity.modifiedBy("todo") }

return null
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jactor.persistence.command

import java.util.UUID
import com.github.jactor.persistence.dto.AddressInternalDto
import com.github.jactor.persistence.dto.PersistentDto
import com.github.jactor.persistence.dto.PersonInternalDto
Expand Down Expand Up @@ -54,7 +55,7 @@ data class CreateUserCommand(

private fun fetchAddressDto(): AddressInternalDto? {
return if (zipCode == null) null else AddressInternalDto(
persistentDto = PersistentDto(),
persistentDto = PersistentDto(id = UUID.randomUUID()),
zipCode = zipCode,
addressLine1 = addressLine1,
addressLine2 = addressLine2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jactor.persistence.controller

import java.util.UUID
import com.github.jactor.persistence.dto.BlogDto
import com.github.jactor.persistence.dto.BlogEntryDto
import com.github.jactor.persistence.service.BlogService
Expand Down Expand Up @@ -35,7 +36,7 @@ class BlogController(private val blogService: BlogService) {
]
)
@GetMapping("/{id}")
operator fun get(@PathVariable("id") blogId: Long): ResponseEntity<BlogDto> {
operator fun get(@PathVariable("id") blogId: UUID): ResponseEntity<BlogDto> {
return blogService.find(blogId)?.let { ResponseEntity(it, HttpStatus.OK) }
?: ResponseEntity(HttpStatus.NO_CONTENT)
}
Expand All @@ -52,7 +53,7 @@ class BlogController(private val blogService: BlogService) {
]
)
@GetMapping("/entry/{id}")
fun getEntryById(@PathVariable("id") blogEntryId: Long): ResponseEntity<BlogEntryDto> {
fun getEntryById(@PathVariable("id") blogEntryId: UUID): ResponseEntity<BlogEntryDto> {
return blogService.findEntryBy(blogEntryId)?.let { ResponseEntity(it, HttpStatus.OK) }
?: ResponseEntity(HttpStatus.NO_CONTENT)
}
Expand Down Expand Up @@ -87,7 +88,7 @@ class BlogController(private val blogService: BlogService) {
]
)
@GetMapping("/{id}/entries")
fun findEntriesByBlogId(@PathVariable("id") blogId: Long?): ResponseEntity<List<BlogEntryDto>> {
fun findEntriesByBlogId(@PathVariable("id") blogId: UUID): ResponseEntity<List<BlogEntryDto>> {
val entriesForBlog = blogService.findEntriesForBlog(blogId)

return ResponseEntity(entriesForBlog, if (entriesForBlog.isEmpty()) HttpStatus.NO_CONTENT else HttpStatus.OK)
Expand All @@ -105,7 +106,7 @@ class BlogController(private val blogService: BlogService) {
]
)
@PutMapping("/{blogId}")
fun put(@RequestBody blogDto: BlogDto, @PathVariable blogId: Long): ResponseEntity<BlogDto> {
fun put(@RequestBody blogDto: BlogDto, @PathVariable blogId: UUID): ResponseEntity<BlogDto> {
if (blogDto.id != blogId) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
}
Expand Down Expand Up @@ -147,7 +148,7 @@ class BlogController(private val blogService: BlogService) {
@PutMapping("/entry/{blogEntryId}")
fun putEntry(
@RequestBody blogEntryDto: BlogEntryDto,
@PathVariable blogEntryId: Long
@PathVariable blogEntryId: UUID
): ResponseEntity<BlogEntryDto> {
if (blogEntryDto.id != blogEntryId) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jactor.persistence.controller

import java.util.UUID
import com.github.jactor.persistence.dto.GuestBookDto
import com.github.jactor.persistence.dto.GuestBookEntryDto
import com.github.jactor.persistence.service.GuestBookService
Expand Down Expand Up @@ -34,7 +35,7 @@ class GuestBookController(private val guestBookService: GuestBookService) {
]
)
@GetMapping("/{id}")
operator fun get(@PathVariable("id") id: Long): ResponseEntity<GuestBookDto> {
operator fun get(@PathVariable("id") id: UUID): ResponseEntity<GuestBookDto> {
return guestBookService.find(id)?.let { ResponseEntity(it, HttpStatus.OK) }
?: ResponseEntity(HttpStatus.NO_CONTENT)
}
Expand All @@ -51,7 +52,7 @@ class GuestBookController(private val guestBookService: GuestBookService) {
]
)
@GetMapping("/entry/{id}")
fun getEntry(@PathVariable("id") id: Long): ResponseEntity<GuestBookEntryDto> {
fun getEntry(@PathVariable("id") id: UUID): ResponseEntity<GuestBookEntryDto> {
return guestBookService.findEntry(id)?.let { ResponseEntity(it, HttpStatus.OK) }
?: ResponseEntity(HttpStatus.NO_CONTENT)
}
Expand Down Expand Up @@ -88,7 +89,7 @@ class GuestBookController(private val guestBookService: GuestBookService) {
]
)
@PutMapping("/{guestBookId}")
fun put(@RequestBody guestBookDto: GuestBookDto, @PathVariable guestBookId: Long): ResponseEntity<GuestBookDto> {
fun put(@RequestBody guestBookDto: GuestBookDto, @PathVariable guestBookId: UUID): ResponseEntity<GuestBookDto> {
if (guestBookDto.id != guestBookId) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
}
Expand Down Expand Up @@ -130,7 +131,7 @@ class GuestBookController(private val guestBookService: GuestBookService) {
@PutMapping("/entry/{guestBookEntryId}")
fun putEntry(
@RequestBody guestBookEntryDto: GuestBookEntryDto,
@PathVariable guestBookEntryId: Long
@PathVariable guestBookEntryId: UUID
): ResponseEntity<GuestBookEntryDto> {
if (guestBookEntryDto.id != guestBookEntryId) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jactor.persistence.controller

import java.util.UUID
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
Expand Down Expand Up @@ -46,7 +47,7 @@ class UserController(private val userService: UserService) {
]
)
@GetMapping("/{id}")
operator fun get(@PathVariable("id") id: Long): ResponseEntity<UserDto> {
operator fun get(@PathVariable("id") id: UUID): ResponseEntity<UserDto> {
return userService.find(id)?.let { ResponseEntity(it.toUserDto(), HttpStatus.OK) }
?: ResponseEntity(HttpStatus.NOT_FOUND)
}
Expand Down Expand Up @@ -78,7 +79,7 @@ class UserController(private val userService: UserService) {
]
)
@PutMapping("/{userId}")
fun put(@RequestBody userDto: UserDto, @PathVariable userId: Long?): ResponseEntity<UserDto> {
fun put(@RequestBody userDto: UserDto, @PathVariable userId: UUID?): ResponseEntity<UserDto> {
return userService.update(UserInternalDto(userDto.copy(userId)))?.let {
ResponseEntity(it.toUserDto(), HttpStatus.ACCEPTED)
} ?: ResponseEntity(HttpStatus.BAD_REQUEST)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.jactor.persistence.dto

import java.util.UUID

open class PersistentData(open val persistentDto: PersistentDto) {
var id: Long?
var id: UUID?
get() = persistentDto.id
set(value) {
persistentDto.id = value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.github.jactor.persistence.dto

import java.time.LocalDateTime
import java.util.UUID

data class PersistentDto(
var id: Long? = null,
var id: UUID? = null,
var createdBy: String = "todo: #3",
var timeOfCreation: LocalDateTime = LocalDateTime.now(),
var modifiedBy: String = "todo: #3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.jactor.persistence.entity

import java.util.UUID
import com.github.jactor.persistence.dto.AddressInternalDto

internal object AddressBuilder {
fun new(addressInternalDto: AddressInternalDto) = AddressData(
addressInternalDto = addressInternalDto.copy(
persistentDto = addressInternalDto.persistentDto.copy(id = UUID.randomUUID())
)
)

fun unchanged(addressInternalDto: AddressInternalDto): AddressData = AddressData(
addressInternalDto = addressInternalDto
)

@JvmRecord
data class AddressData(val addressInternalDto: AddressInternalDto) {
fun build(): AddressEntity = AddressEntity(addressInternalDto = addressInternalDto)
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package com.github.jactor.persistence.entity

import java.time.LocalDateTime
import java.util.Objects
import java.util.UUID
import org.apache.commons.lang3.builder.ToStringBuilder
import org.apache.commons.lang3.builder.ToStringStyle
import com.github.jactor.persistence.dto.AddressInternalDto
import jakarta.persistence.AttributeOverride
import jakarta.persistence.Column
import jakarta.persistence.Embedded
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.SequenceGenerator
import jakarta.persistence.Table
import org.apache.commons.lang3.builder.ToStringBuilder
import org.apache.commons.lang3.builder.ToStringStyle
import java.time.LocalDateTime
import java.util.Objects

@Entity
@Table(name = "T_ADDRESS")
class AddressEntity : PersistentEntity<AddressEntity?> {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "addressSeq")
@SequenceGenerator(name = "addressSeq", sequenceName = "T_ADDRESS_SEQ", allocationSize = 1)
override var id: Long? = null
override var id: UUID? = null

@Embedded
@AttributeOverride(name = "createdBy", column = Column(name = "CREATED_BY"))
Expand Down Expand Up @@ -131,10 +127,4 @@ class AddressEntity : PersistentEntity<AddressEntity?> {
get() = persistentDataEmbeddable.modifiedBy
override val timeOfModification: LocalDateTime
get() = persistentDataEmbeddable.timeOfModification

companion object {
fun anAddress(addressInternalDto: AddressInternalDto): AddressEntity {
return AddressEntity(addressInternalDto)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.jactor.persistence.entity

import java.util.UUID
import com.github.jactor.persistence.dto.BlogDto
import com.github.jactor.persistence.dto.BlogEntryDto
import com.github.jactor.persistence.dto.PersistentDto

internal object BlogBuilder {
fun new(blogDto: BlogDto = BlogDto()): BlogData = BlogData(
blogDto = blogDto.copy(persistentDto = blogDto.persistentDto.copy(id = UUID.randomUUID()))
)

fun unchanged(blogDto: BlogDto = BlogDto()): BlogData = BlogData(
blogDto = blogDto
)

@JvmRecord
data class BlogData(
val blogDto: BlogDto,
val blogEntryDto: BlogEntryDto? = null,
) {
fun withEntry(blogEntryDto: BlogEntryDto): BlogData = copy(
blogEntryDto = blogEntryDto.copy(
persistentDto = PersistentDto(id = UUID.randomUUID()),
blog = blogDto,
)
)

fun withUnchangedEntry(blogEntryDto: BlogEntryDto): BlogData = copy(
blogEntryDto = blogEntryDto,
)

fun buildBlogEntity(): BlogEntity = BlogEntity(blogDto = blogDto)

fun buildBlogEntryEntity(): BlogEntryEntity = BlogEntryEntity(
blogEntryDto = blogEntryDto ?: error("No blog entry dto"),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.jactor.persistence.entity
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.Objects
import java.util.UUID
import org.apache.commons.lang3.builder.ToStringBuilder
import org.apache.commons.lang3.builder.ToStringStyle
import com.github.jactor.persistence.dto.BlogDto
Expand All @@ -12,22 +13,17 @@ import jakarta.persistence.Column
import jakarta.persistence.Embedded
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToMany
import jakarta.persistence.SequenceGenerator
import jakarta.persistence.Table

@Entity
@Table(name = "T_BLOG")
class BlogEntity : PersistentEntity<BlogEntity?> {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "blogSeq")
@SequenceGenerator(name = "blogSeq", sequenceName = "T_BLOG_SEQ", allocationSize = 1)
override var id: Long? = null
override var id: UUID? = null

@Embedded
@AttributeOverride(name = "createdBy", column = Column(name = "CREATED_BY"))
Expand Down Expand Up @@ -126,11 +122,4 @@ class BlogEntity : PersistentEntity<BlogEntity?> {
fun getEntries(): Set<BlogEntryEntity> {
return entries
}

companion object {
@JvmStatic
fun aBlog(blogDto: BlogDto): BlogEntity {
return BlogEntity(blogDto)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.jactor.persistence.entity

import java.time.LocalDateTime
import java.util.Objects
import java.util.UUID
import org.apache.commons.lang3.builder.ToStringBuilder
import org.apache.commons.lang3.builder.ToStringStyle
import com.github.jactor.persistence.dto.BlogDto
Expand All @@ -11,21 +12,16 @@ import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Embedded
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.SequenceGenerator
import jakarta.persistence.Table

@Entity
@Table(name = "T_BLOG_ENTRY")
class BlogEntryEntity : PersistentEntity<BlogEntryEntity?> {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "blogEntrySeq")
@SequenceGenerator(name = "blogEntrySeq", sequenceName = "T_BLOG_ENTRY_SEQ", allocationSize = 1)
override var id: Long? = null
override var id: UUID? = null

@Embedded
@AttributeOverride(name = "createdBy", column = Column(name = "CREATED_BY"))
Expand Down Expand Up @@ -130,11 +126,4 @@ class BlogEntryEntity : PersistentEntity<BlogEntryEntity?> {
get() = entryEmbeddable.notNullableCreator
val entry: String
get() = entryEmbeddable.notNullableEntry

companion object {
@JvmStatic
fun aBlogEntry(blogEntryDto: BlogEntryDto): BlogEntryEntity {
return BlogEntryEntity(blogEntryDto)
}
}
}
Loading

0 comments on commit 37ae3ba

Please sign in to comment.