From ffdf34e934dfb03baa5a88ddf8432fd3cfeff4dc Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 26 Jan 2024 21:22:41 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Notification=E3=81=ABRepository?= =?UTF-8?q?=E3=81=A8Exposed=E5=AE=9F=E8=A3=85=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExposedNotificationRepository.kt | 81 +++++++++++++++++++ .../notification/NotificationRepository.kt | 8 ++ 2 files changed, 89 insertions(+) create mode 100644 src/main/kotlin/dev/usbharu/hideout/core/domain/model/notification/ExposedNotificationRepository.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/core/domain/model/notification/NotificationRepository.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/notification/ExposedNotificationRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/notification/ExposedNotificationRepository.kt new file mode 100644 index 00000000..71e8acee --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/notification/ExposedNotificationRepository.kt @@ -0,0 +1,81 @@ +package dev.usbharu.hideout.core.domain.model.notification + +import dev.usbharu.hideout.application.service.id.IdGenerateService +import dev.usbharu.hideout.core.infrastructure.exposedrepository.AbstractRepository +import dev.usbharu.hideout.core.infrastructure.exposedrepository.Actors +import dev.usbharu.hideout.core.infrastructure.exposedrepository.Posts +import dev.usbharu.hideout.core.infrastructure.exposedrepository.Reactions +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.javatime.timestamp +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Repository + +@Repository +class ExposedNotificationRepository(private val idGenerateService: IdGenerateService) : NotificationRepository, + AbstractRepository() { + override val logger: Logger + get() = Companion.logger + + override suspend fun generateId(): Long = idGenerateService.generateId() + + override suspend fun save(notification: Notification): Notification = query { + val singleOrNull = Notifications.select { + Notifications.id eq notification.id + }.forUpdate().singleOrNull() + if (singleOrNull == null) { + Notifications.insert { + it[id] = notification.id + it[userId] = notification.userId + it[sourceActorId] = notification.sourceActorId + it[postId] = notification.postId + it[text] = notification.text + it[reactionId] = notification.reactionId + it[createdAt] = notification.createdAt + } + } else { + Notifications.update({ Notifications.id eq notification.id }) { + it[userId] = notification.userId + it[sourceActorId] = notification.sourceActorId + it[postId] = notification.postId + it[text] = notification.text + it[reactionId] = notification.reactionId + it[createdAt] = notification.createdAt + } + } + notification + } + + override suspend fun findById(id: Long): Notification? = query { + Notifications.select { Notifications.id eq id }.singleOrNull()?.toNotifications() + } + + override suspend fun deleteById(id: Long) { + Notifications.deleteWhere { Notifications.id eq id } + } + + companion object { + private val logger = LoggerFactory.getLogger(ExposedNotificationRepository::class.java) + } +} + +fun ResultRow.toNotifications() = Notification( + this[Notifications.id], + this[Notifications.userId], + this[Notifications.sourceActorId], + this[Notifications.postId], + this[Notifications.text], + this[Notifications.reactionId], + this[Notifications.createdAt], +) + +object Notifications : Table("notifications") { + val id = long("id") + val userId = long("user_id").references(Actors.id) + val sourceActorId = long("source_actor_id").references(Actors.id).nullable() + val postId = long("post_id").references(Posts.id).nullable() + val text = varchar("text", 3000).nullable() + val reactionId = long("reaction_id").references(Reactions.id).nullable() + val createdAt = timestamp("created_at") +} diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/notification/NotificationRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/notification/NotificationRepository.kt new file mode 100644 index 00000000..97f99fa9 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/notification/NotificationRepository.kt @@ -0,0 +1,8 @@ +package dev.usbharu.hideout.core.domain.model.notification + +interface NotificationRepository { + suspend fun generateId(): Long + suspend fun save(notification: Notification): Notification + suspend fun findById(id: Long): Notification? + suspend fun deleteById(id: Long) +}