Skip to content

Commit

Permalink
[update] howard12721#1
Browse files Browse the repository at this point in the history
  • Loading branch information
howard12721 committed Sep 2, 2023
1 parent fa4af63 commit 6de0d8d
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 44 deletions.
15 changes: 14 additions & 1 deletion src/main/kotlin/jp/xhw/noticebox/NoticeBoxPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@ import dev.jorel.commandapi.CommandAPI
import dev.jorel.commandapi.CommandAPIBukkitConfig
import jp.xhw.noticebox.application.external.Economy
import jp.xhw.noticebox.application.external.ItemClaimLogic
import jp.xhw.noticebox.application.service.StoreUserFirstJoinService
import jp.xhw.noticebox.infrastructure.dao.AnnounceOpens
import jp.xhw.noticebox.infrastructure.dao.AnnounceRewards
import jp.xhw.noticebox.infrastructure.dao.Announces
import jp.xhw.noticebox.infrastructure.dao.Users
import jp.xhw.noticebox.infrastructure.external.ItemClaimLogicImpl
import jp.xhw.noticebox.infrastructure.external.VaultEconomy
import jp.xhw.noticebox.infrastructure.repository.AnnounceSqliteRepository
import jp.xhw.noticebox.infrastructure.repository.UserSqliteRepository
import jp.xhw.noticebox.presenter.command.Command
import jp.xhw.noticebox.presenter.listener.EventListener
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.plugin.java.JavaPlugin
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import java.io.File
import java.sql.Connection
import java.time.LocalDateTime
import java.util.logging.Level

class NoticeBoxPlugin : JavaPlugin() {
class NoticeBoxPlugin : JavaPlugin(), Listener {

companion object {
lateinit var plugin: NoticeBoxPlugin
Expand Down Expand Up @@ -57,8 +63,10 @@ class NoticeBoxPlugin : JavaPlugin() {
SchemaUtils.create(Announces)
SchemaUtils.create(AnnounceRewards)
SchemaUtils.create(AnnounceOpens)
SchemaUtils.create(Users)
}

server.pluginManager.registerEvents(this, this)
server.pluginManager.registerEvents(EventListener(), this)

Command().register()
Expand All @@ -67,4 +75,9 @@ class NoticeBoxPlugin : JavaPlugin() {

override fun onDisable() {}

@EventHandler
fun on(event: PlayerJoinEvent) {
StoreUserFirstJoinService(userRepository).store(event.player.uniqueId, LocalDateTime.now())
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jp.xhw.noticebox.application.exception

import java.util.*

class UserNotFoundException(val userId: UUID) : RuntimeException(userId.toString())
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jp.xhw.noticebox.application.service

import jp.xhw.noticebox.application.exception.UserNotFoundException
import jp.xhw.noticebox.domain.model.AnnounceId
import jp.xhw.noticebox.domain.model.UserId
import jp.xhw.noticebox.domain.repository.UserRepository
Expand All @@ -10,7 +11,7 @@ class FetchReadAnnouncesService(private val userRepository: UserRepository) {

fun fetch(userId: UUID): List<UUID> {
return transaction {
userRepository.find(UserId(userId)).openedAnnounceIds.map(AnnounceId::value)
userRepository.find(UserId(userId))?.openedAnnounceIds?.map(AnnounceId::value)?: throw UserNotFoundException(userId)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package jp.xhw.noticebox.application.service

import jp.xhw.noticebox.application.dto.AnnounceSampleDto
import jp.xhw.noticebox.application.exception.UserNotFoundException
import jp.xhw.noticebox.domain.model.User
import jp.xhw.noticebox.domain.model.UserId
import jp.xhw.noticebox.domain.repository.AnnounceRepository
import jp.xhw.noticebox.domain.repository.UserRepository
import org.jetbrains.exposed.sql.transactions.transaction
import java.util.*

class FetchUserAnnounceSamplesService(
private val announceRepository: AnnounceRepository,
private val userRepository: UserRepository
) {
fun fetch(userId: UUID, offset: Long, limit: Int): List<AnnounceSampleDto> {
return transaction {
val user: User = userRepository.find(UserId(userId))?: throw UserNotFoundException(userId)
val announceSampleList = announceRepository.findPagedAnnounceSamples(offset, limit, user.firstJoin)
announceSampleList.map { sample ->
AnnounceSampleDto(
sample.announceId.value,
sample.title.value,
sample.createdAt
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jp.xhw.noticebox.application.service
import jp.xhw.noticebox.domain.repository.AnnounceRepository
import org.jetbrains.exposed.sql.transactions.transaction

class GetNumberOfAnnounceService(private val announceRepository: AnnounceRepository) {
class GetNumberOfAnnouncesService(private val announceRepository: AnnounceRepository) {
fun get(): Long {
return transaction {
announceRepository.count()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jp.xhw.noticebox.application.service

import jp.xhw.noticebox.application.exception.UserNotFoundException
import jp.xhw.noticebox.domain.model.UserId
import jp.xhw.noticebox.domain.repository.AnnounceRepository
import jp.xhw.noticebox.domain.repository.UserRepository
import org.jetbrains.exposed.sql.transactions.transaction
import java.util.UUID

class GetNumberOfUserAnnouncesService(private val announceRepository: AnnounceRepository, private val userRepository: UserRepository) {
fun get(userId: UUID): Long {
return transaction {
val user = userRepository.find(UserId(userId))?: throw UserNotFoundException(userId)
announceRepository.count(user.firstJoin)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jp.xhw.noticebox.application.service

import jp.xhw.noticebox.domain.model.User
import jp.xhw.noticebox.domain.model.UserId
import jp.xhw.noticebox.domain.repository.UserRepository
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.LocalDateTime
import java.util.UUID

class StoreUserFirstJoinService(private val userRepository: UserRepository) {
fun store(userId: UUID, date: LocalDateTime) {
transaction {
val user = userRepository.find(UserId(userId))
if (user == null) {
userRepository.save(User(UserId(userId), mutableListOf(), date))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jp.xhw.noticebox.application.service

import jp.xhw.noticebox.application.dto.RewardDto
import jp.xhw.noticebox.application.exception.UserNotFoundException
import jp.xhw.noticebox.application.external.Economy
import jp.xhw.noticebox.application.external.ItemClaimLogic
import jp.xhw.noticebox.domain.model.AnnounceId
Expand All @@ -18,7 +19,7 @@ class UserOpenAnnounceService(
) {
fun open(userId: UUID, announceId: UUID): RewardDto? {
return transaction {
val opener = userRepository.find(UserId(userId))
val opener = userRepository.find(UserId(userId))?: throw UserNotFoundException(userId)
val announce = announceRepository.findById(AnnounceId(announceId)) ?: return@transaction null

if (opener.isNotOpened(announce)) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/jp/xhw/noticebox/domain/model/User.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package jp.xhw.noticebox.domain.model

import jp.xhw.noticebox.domain.exceptions.AnnounceAlreadyOpenedException
import java.time.LocalDateTime
import java.util.*

class User(val userId: UserId, val openedAnnounceIds: MutableList<AnnounceId>) {
class User(val userId: UserId, val openedAnnounceIds: MutableList<AnnounceId>, val firstJoin: LocalDateTime) {

fun openAnnounce(announce: Announce) {
if (openedAnnounceIds.contains(announce.announceId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import jp.xhw.noticebox.domain.model.Announce
import jp.xhw.noticebox.domain.model.AnnounceId
import jp.xhw.noticebox.domain.model.AnnounceReward
import jp.xhw.noticebox.domain.model.AnnounceSample
import java.time.LocalDateTime

interface AnnounceRepository {

fun findById(announceId: AnnounceId): Announce?

fun findPagedAnnounceSamples(offset: Long, limit: Int, after: LocalDateTime): List<AnnounceSample>

fun findPagedAnnounceSamples(offset: Long, limit: Int): List<AnnounceSample>

fun save(announce: Announce)
Expand All @@ -19,4 +22,6 @@ interface AnnounceRepository {

fun count(): Long

fun count(after: LocalDateTime): Long

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import jp.xhw.noticebox.domain.model.UserId

interface UserRepository {

fun find(userId: UserId): User
fun find(userId: UserId): User?
fun save(user: User)

}
13 changes: 13 additions & 0 deletions src/main/kotlin/jp/xhw/noticebox/infrastructure/dao/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jp.xhw.noticebox.infrastructure.dao

import org.jetbrains.exposed.dao.UUIDEntity
import org.jetbrains.exposed.dao.UUIDEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import java.util.*

class User (id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<User>(Users)

var joinedAt by Users.joinedAt

}
12 changes: 12 additions & 0 deletions src/main/kotlin/jp/xhw/noticebox/infrastructure/dao/Users.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package jp.xhw.noticebox.infrastructure.dao

import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.javatime.datetime
import java.time.LocalDateTime

object Users : UUIDTable() {

val joinedAt: Column<LocalDateTime> = datetime("joined_at")

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import jp.xhw.noticebox.infrastructure.dao.AnnounceRewards
import jp.xhw.noticebox.infrastructure.dao.Announces
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater
import org.jetbrains.exposed.sql.statements.api.ExposedBlob
import java.time.LocalDateTime
import jp.xhw.noticebox.infrastructure.dao.Announce as AnnounceDao
import jp.xhw.noticebox.infrastructure.dao.AnnounceReward as AnnounceRewardDao

Expand All @@ -24,6 +26,15 @@ class AnnounceSqliteRepository : AnnounceRepository {
)
}

override fun findPagedAnnounceSamples(offset: Long, limit: Int, after: LocalDateTime): List<AnnounceSample> {
val announces = Announces
.slice(Announces.id, Announces.title, Announces.createdAt)
.select { Announces.createdAt greater after }
.orderBy(Announces.createdAt, SortOrder.DESC)
.limit(limit, offset = offset)
return announces.map(this::convertToAnnounceSample).toList()
}

override fun findPagedAnnounceSamples(offset: Long, limit: Int): List<AnnounceSample> {
val announces = Announces
.slice(Announces.id, Announces.title, Announces.createdAt)
Expand Down Expand Up @@ -80,6 +91,10 @@ class AnnounceSqliteRepository : AnnounceRepository {
return AnnounceDao.count()
}

override fun count(after: LocalDateTime): Long {
return Announces.select { Announces.createdAt greater after }.count()
}

private fun convertToAnnounceSample(resultRow: ResultRow): AnnounceSample {
val uuid = resultRow[Announces.id].value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ import jp.xhw.noticebox.infrastructure.dao.AnnounceOpens
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.batchInsert
import org.jetbrains.exposed.sql.deleteWhere
import jp.xhw.noticebox.infrastructure.dao.AnnounceOpen as UserDao
import java.time.LocalDateTime
import jp.xhw.noticebox.infrastructure.dao.AnnounceOpen as AnnounceOpensDao
import jp.xhw.noticebox.infrastructure.dao.User as UserDao

class UserSqliteRepository : UserRepository {

override fun find(userId: UserId): User {
override fun find(userId: UserId): User? {
val user = UserDao.findById(userId.value)?: return null
val openedAnnounces = mutableListOf<AnnounceId>()
UserDao
AnnounceOpensDao
.find { AnnounceOpens.userId eq userId.value }
.forEach { announceOpen -> openedAnnounces.add(AnnounceId(announceOpen.announceId)) }
return User(userId, openedAnnounces)
return User(userId, openedAnnounces, user.joinedAt)
}

override fun save(user: User) {
val userDao = UserDao.findById(user.userId.value)
if (userDao == null) {
UserDao.new(user.userId.value) {
this.joinedAt = user.firstJoin
}
}
AnnounceOpens.deleteWhere { userId eq user.userId.value }
val list = mutableListOf<Pair<UserId, AnnounceId>>()
for (announceId in user.openedAnnounceIds) {
Expand Down
Loading

0 comments on commit 6de0d8d

Please sign in to comment.