Skip to content

Commit

Permalink
Use non-null as much as possible in entities
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBestPessimist committed Sep 6, 2021
1 parent 9de62ae commit b2404f2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
18 changes: 8 additions & 10 deletions src/main/kotlin/com/shortener/domain/UrlEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,35 @@ package com.shortener.domain
import org.springframework.format.annotation.DateTimeFormat
import java.time.LocalDateTime
import javax.persistence.*
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size

@Table(name = "URL_ENTRIES" )
@Table(name = "URL_ENTRIES")
@Entity
class UrlEntry {
class UrlEntry(
@Column(name = "long_url")
@Size(max = 2048)
var longUrl: String,
) {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
var id: Long? = null

@Column(name = "long_url")
@Size(max = 2048)
@NotBlank
var longUrl: String? = ""

@OneToOne(cascade = [CascadeType.ALL])
@JoinColumn(name = "sequence", referencedColumnName = "sequence")
var encodedSequence: EncodedSequence? = null

@Column(name = "created_at")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@NotNull
var createdAt: LocalDateTime? = null
var createdAt: LocalDateTime = LocalDateTime.now()

@Column(name = "expires_at")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@NotNull
var expiresAt: LocalDateTime? = null
lateinit var expiresAt: LocalDateTime

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/com/shortener/service/UrlServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ class UrlServiceImpl(
throw InvalidInputUrl("The URL you provided is invalid")
}

val urlEntry = UrlEntry()
urlEntry.longUrl = fixedRequestUrl
urlEntry.createdAt = LocalDateTime.now()
urlEntry.expiresAt = urlEntry.createdAt?.plusHours(getNoUserUrlExpiresDays())
val urlEntry = UrlEntry(fixedRequestUrl).apply {
expiresAt = createdAt.plusHours(getNoUserUrlExpiresDays())
}

val persistedUrlEntry = urlEntryRepository.save(urlEntry)
val encodedId = idEncoder.encode(persistedUrlEntry.id!!)
Expand Down
15 changes: 6 additions & 9 deletions src/test/kotlin/com/shortener/service/UrlServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,11 @@ class UrlServiceTest(
assertThat(actualMessage).contains(expectedMessage)
}

private fun createUrlEntry(longUrl: String, encodedSequence: String, expired: Boolean): UrlEntry {
val entry = UrlEntry()
entry.longUrl = longUrl
entry.encodedSequence = EncodedSequence().apply { sequence = encodedSequence }
entry.createdAt = LocalDateTime.now().minusDays(3)
entry.expiresAt = (if (expired) LocalDateTime.now().minusDays(1) else LocalDateTime.now().plusDays(4))

return entry
}
private fun createUrlEntry(longUrl: String, encodedSequenceStr: String, expired: Boolean): UrlEntry =
UrlEntry(longUrl).apply {
encodedSequence = EncodedSequence().apply { sequence = encodedSequenceStr }
createdAt = LocalDateTime.now().minusDays(3)
expiresAt = (if (expired) LocalDateTime.now().minusDays(1) else LocalDateTime.now().plusDays(4))
}

}

0 comments on commit b2404f2

Please sign in to comment.