Skip to content

Commit

Permalink
calculate inception datetime only once.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvorhauer committed Feb 19, 2024
1 parent 01bbb7c commit f8846db
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
16 changes: 9 additions & 7 deletions src/main/kotlin/blog/model/Note.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package blog.model

import java.time.ZoneId
import java.time.ZonedDateTime
import akka.Done
import akka.actor.typed.ActorRef
import akka.pattern.StatusReply
import org.owasp.encoder.Encode
import java.time.LocalDateTime
import java.time.ZoneId
import akka.actor.typed.Scheduler
import akka.actor.typed.javadsl.AskPattern
import akka.pattern.StatusReply
import org.owasp.encoder.Encode
import io.hypersistence.tsid.TSID
import io.ktor.http.*
import io.ktor.server.application.*
Expand Down Expand Up @@ -58,18 +58,20 @@ data class Note(
val user: String,
val title: String,
val slug: String,
val body: String
val body: String,
val created: ZonedDateTime = TSID.from(id).instant.atZone(ZoneId.of("CET")),
): Entity {
constructor(id: String, user: String, title: String, body: String): this(id, user, title, slugify(title), body)
fun update(nu: NoteUpdated): Note = this.copy(title = nu.title ?: this.title, body = nu.body ?: this.body)
fun toResponse() = NoteResponse(id, user, DTF.format(LocalDateTime.ofInstant(TSID.from(id).instant, ZoneId.of("CET"))), title, body)
fun update(nu: NoteUpdated): Note = this.copy(title = nu.title ?: this.title, slug = slugify(nu.title ?: this.title), body = nu.body ?: this.body)
fun toResponse() = NoteResponse(id, user, DTF.format(created), title, slug, body)
}

data class NoteResponse(
val id: String,
val user: String,
val created: String,
val title: String,
val slug: String,
val body: String
)

Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/blog/model/Task.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package blog.model

import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import akka.Done
import akka.actor.typed.ActorRef
import akka.actor.typed.Scheduler
import akka.actor.typed.javadsl.AskPattern.ask
import akka.pattern.StatusReply
import io.hypersistence.tsid.TSID
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
Expand All @@ -27,7 +30,8 @@ data class Task(
val body: String,
val due: LocalDateTime,
val status: TaskStatus = TaskStatus.TODO,
val private: Boolean = true
val private: Boolean = true,
val created: ZonedDateTime = TSID.from(id).instant.atZone(ZoneId.of("CET"))
) : Entity {
fun update(tu: TaskUpdated): Task = this.copy(
title = tu.title ?: this.title,
Expand All @@ -36,7 +40,7 @@ data class Task(
due = tu.due ?: this.due,
status = tu.status ?: this.status
)
fun toResponse() = TaskResponse(id, user, title, body, DTF.format(due), status.name)
fun toResponse() = TaskResponse(id,DTF.format(created), user, title, body, DTF.format(due), status.name)

companion object {
val clazz = Task::class
Expand Down Expand Up @@ -105,6 +109,7 @@ data class TaskDeleted(val id: String): Event

data class TaskResponse(
val id: String,
val created: String,
val user: String,
val title: String,
val body: String,
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/blog/model/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blog.model
import java.time.Duration
import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime
import akka.actor.typed.ActorRef
import akka.actor.typed.Scheduler
import akka.actor.typed.javadsl.AskPattern.ask
Expand Down Expand Up @@ -67,13 +68,14 @@ data class User(
val email: String,
val name: String,
val password: String,
val gravatar: String = gravatarize(email)
val gravatar: String = gravatarize(email),
val joined: ZonedDateTime = TSID.from(id).instant.atZone(ZoneId.of("CET"))
) : Entity {
fun toResponse(reader: Reader): UserResponse = UserResponse(
id,
email,
name,
DTF.format(TSID.from(id).instant.atZone(ZoneId.of("CET"))),
DTF.format(joined),
gravatar,
reader.findNotesForUser(id).map(Note::toResponse),
reader.findTasksForUser(id).map(Task::toResponse)
Expand Down

0 comments on commit f8846db

Please sign in to comment.