mirror of https://github.com/usbharu/Hideout.git
feat: NotificationにRepositoryとExposed実装を追加
This commit is contained in:
parent
3099d3ee3a
commit
ffdf34e934
|
@ -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")
|
||||
}
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue